Class: HatebuEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/hatebu_entry.rb,
lib/hatebu_entry/version.rb

Defined Under Namespace

Classes: Command, Entry

Constant Summary collapse

HatenaURI =
"http://b.hatena.ne.jp/entrylist"
VERSION =
"0.0.4"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, sort = 'count') ⇒ HatebuEntry

sort: count, eid or hot



74
75
76
# File 'lib/hatebu_entry.rb', line 74

def initialize(site, sort='count')
  @params = {url: site, sort: sort, of: 0*20}
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



72
73
74
# File 'lib/hatebu_entry.rb', line 72

def params
  @params
end

Instance Method Details

#build_params(params) ⇒ Object



121
122
123
# File 'lib/hatebu_entry.rb', line 121

def build_params(params)
  params.map { |k, v| "#{h k}=#{h v}" } * '&'
end

#build_uri(joint, params = @params) ⇒ Object



117
118
119
# File 'lib/hatebu_entry.rb', line 117

def build_uri(joint, params=@params)
  HatenaURI + joint + build_params(params)
end

#call_hatena_entry_api(api) ⇒ Object



104
105
106
107
# File 'lib/hatebu_entry.rb', line 104

def call_hatena_entry_api(api)
  uri = {jsonp: build_uri('/json?'), html: build_uri('?')}[api]
  get uri
end

#entries(pages = 0) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/hatebu_entry.rb', line 78

def entries(pages=0)
  if pages <= 0
    get_entries(:jsonp) # get 10 entries with jsonp
  else
    # get 20 entries per page with html
    mutex = Mutex.new
    pages.times.map { |i|
      Thread.fork(i) do |_i|
        mutex.synchronize {
          params.update(of: _i*20)
          get_entries(:html)
        }
      end
    }.map(&:value).flatten
  end
end

#get(uri) ⇒ Object



109
110
111
112
113
# File 'lib/hatebu_entry.rb', line 109

def get(uri)
  open(uri).read
rescue => e
  abort "HTTP Access Error: #{e.response}"
end

#get_entries(api) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/hatebu_entry.rb', line 95

def get_entries(api)
  entries = 
    case api
    when :jsonp then parse_jsonp(call_hatena_entry_api :jsonp)
    when :html  then parse_html(call_hatena_entry_api :html)
    end
  entries.map { |h| Entry.new h['link'], Integer(h['count']), h['title'] }
end

#h(str) ⇒ Object



125
126
127
# File 'lib/hatebu_entry.rb', line 125

def h(str)
  CGI.escape(str.to_s)
end

#parse_html(html) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/hatebu_entry.rb', line 133

def parse_html(html)
  entries = []
  doc = Nokogiri::HTML(html)
  doc.css('li.entry-unit').each do |ent|
    count = ent['data-bookmark-count'].to_i
    a = ent.at('.entry-contents a')
    title, href = a['title'], a['href']
    entries << {'link' => href, 'count' => count, 'title' => title}
  end
  entries
end

#parse_jsonp(jsonp) ⇒ Object



129
130
131
# File 'lib/hatebu_entry.rb', line 129

def parse_jsonp(jsonp)
  jsonp.scan(/{\s*\".+?\"\s*}/).map { |data| JSON.parse data }
end