class QiitaPicks::Picker
Constants
- ERB_TEMPLATE_ARTICLE
- ERB_TEMPLATE_LAYOUT
- PDFKIT_OPTIONS
Attributes
path_to_html[R]
path_to_pdf[R]
Public Class Methods
hatebu_bookmark_url(tag=nil, sort=:popular)
click to toggle source
# File lib/qiita_picks/picker.rb, line 28 def self.hatebu_bookmark_url(tag=nil, sort=:popular) uri = URI('http://b.hatena.ne.jp/search/text') uri.query = { q: ['http://qiita.com/', tag].compact.join(' '), date_begin: (Time.now - 60*60*24*30).strftime('%Y-%m-%d'), date_end: Time.now.strftime('%Y-%m-%d'), users: 3, sort: sort, safe: 'off', mode: 'rss' }.to_param doc = Nokogiri::XML open(uri.to_s) doc.xpath("//rdf:Seq/rdf:li").map{|x| x.attr("rdf:resource")} end
Public Instance Methods
build_html_from_items(items=[], title=nil)
click to toggle source
# File lib/qiita_picks/picker.rb, line 50 def build_html_from_items(items=[], title=nil) return if items.empty? @title = title || Time.now.strftime(QiitaPicks::EMAIL_SUBJECT_DEFAULT) @contents ||= build_articles_from_items items @html ||= html end
build_html_from_url(url=[], title=nil)
click to toggle source
# File lib/qiita_picks/picker.rb, line 43 def build_html_from_url(url=[], title=nil) return if url.empty? @title = title || Time.now.strftime(QiitaPicks::EMAIL_SUBJECT_DEFAULT) @contents ||= build_articles_from_url url @html ||= html end
save_as_html(opts={})
click to toggle source
# File lib/qiita_picks/picker.rb, line 67 def save_as_html(opts={}) return if @html.nil? opts[:dest] ||= '.' opts[:basename] ||= Time.now.strftime QiitaPicks::FILE_BASENAME_DEFAULT @path_to_html = File.join(opts[:dest], "#{opts[:basename]}.html") open(@path_to_html, "w+") {|f| f.write @html} puts "[GENERATE] #{@path_to_html}" end
save_as_pdf(opts={})
click to toggle source
# File lib/qiita_picks/picker.rb, line 57 def save_as_pdf(opts={}) return if @html.nil? opts[:dest] ||= '.' opts[:basename] ||= Time.now.strftime QiitaPicks::FILE_BASENAME_DEFAULT opts[:pdf_options] ||= {} @path_to_pdf = File.join(opts[:dest], "#{opts[:basename]}.pdf") PDFKit.new(@html, PDFKIT_OPTIONS.merge(opts[:pdf_options])).to_file @path_to_pdf puts "[GENERATE] #{@path_to_pdf}" end
Private Instance Methods
build_articles_from_items(items=[])
click to toggle source
# File lib/qiita_picks/picker.rb, line 103 def build_articles_from_items(items=[]) items.inject('') {|articles, item| template = BabyErubis::Html.new.from_file(ERB_TEMPLATE_ARTICLE, 'utf-8') context = { url: item['url'], title: item['title'], content: item['rendered_body'], user_name: item['user']['id'], created_at: DateTime.parse(item['created_at']).strftime('%Y/%m/%d') } articles << template.render(context) } end
build_articles_from_url(urls=[])
click to toggle source
# File lib/qiita_picks/picker.rb, line 84 def build_articles_from_url(urls=[]) urls.select {|url| /qiita/ =~ URI.parse(url).host}.uniq.inject('') {|articles, url| if res = fetch(url) uri = res.uri doc = Nokogiri::HTML NKF.nkf('-w', res.body) template = BabyErubis::Html.new.from_file(ERB_TEMPLATE_ARTICLE, 'utf-8') context = { url: "#{uri.scheme}://#{uri.host}#{uri.path}", title: doc.css('title').text, content: doc.css('.markdownContent').inner_html, user_name: doc.css('.itemsShowAuthorInfo_userName').text, created_at: doc.css('ul.itemsShowHeaderTitle_status time').first.text } articles << template.render(context) end articles } end
fetch(url, limit=10)
click to toggle source
# File lib/qiita_picks/picker.rb, line 117 def fetch(url, limit=10) res = Net::HTTP.get_response URI.parse(url) puts "[#{res.code} #{res.message}] #{url}" case res when Net::HTTPSuccess return res when Net::HTTPRedirection return fetch(res['location'], limit - 1) end end
html()
click to toggle source
# File lib/qiita_picks/picker.rb, line 78 def html return if @contents.empty? template = BabyErubis::Html.new.from_file(ERB_TEMPLATE_LAYOUT, 'utf-8') template.render(title: @title, contents: @contents, credit: HTML_FOOTER_CREDIT) end