Class: QiitaPicks::Picker

Inherits:
Object
  • Object
show all
Defined in:
lib/qiita_picks/picker.rb

Constant Summary collapse

ERB_TEMPLATE_ARTICLE =
File.expand_path '../templates/article.erb', __FILE__
ERB_TEMPLATE_LAYOUT =
File.expand_path '../templates/layout.erb', __FILE__
PDFKIT_OPTIONS =
{
  encoding:      'UTF-8',
  page_size:     'A4',
  quiet:         false,
  no_background: false
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#path_to_htmlObject (readonly)

Returns the value of attribute path_to_html.



26
27
28
# File 'lib/qiita_picks/picker.rb', line 26

def path_to_html
  @path_to_html
end

#path_to_pdfObject (readonly)

Returns the value of attribute path_to_pdf.



26
27
28
# File 'lib/qiita_picks/picker.rb', line 26

def path_to_pdf
  @path_to_pdf
end

Class Method Details

.hatebu_bookmark_url(tag = nil, sort = :popular) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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

Instance Method Details

#build_html_from_items(items = [], title = nil) ⇒ Object



50
51
52
53
54
55
# 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) ⇒ Object



43
44
45
46
47
48
# 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 = {}) ⇒ Object



67
68
69
70
71
72
73
74
# 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 = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
# 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