class Ruboty::HatenaHotentry::Client

Constants

NAMESPACES
URL_FOR_ALL
URL_FOR_CATEGORY

Attributes

options[R]

Public Class Methods

new(options) click to toggle source
# File lib/ruboty/hatena_hotentry/client.rb, line 21
def initialize(options)
  @options = options
end

Public Instance Methods

get() click to toggle source
# File lib/ruboty/hatena_hotentry/client.rb, line 25
def get
  begin
    url = url(@options[:category])
    parse(open(url).read)
  rescue => exception
    Ruboty.logger.error("Error: #{self}##{__method__} - #{exception}")
    nil
  end
end

Private Instance Methods

namespaces() click to toggle source
# File lib/ruboty/hatena_hotentry/client.rb, line 41
def namespaces
  NAMESPACES
end
parse(doc) click to toggle source
# File lib/ruboty/hatena_hotentry/client.rb, line 45
def parse(doc)
  [].tap do |arr|
    Nokogiri::XML(doc).xpath('//xmlns:item').each do |node|
      arr << {
        title: node.xpath('rss:title', namespaces).text,
        link: node.xpath('rss:link', namespaces).text,
        bookmarkcount: node.xpath('hatena:bookmarkcount', namespaces).text
      }
    end
  end
end
url(category) click to toggle source
# File lib/ruboty/hatena_hotentry/client.rb, line 37
def url(category)
  category == "all" ? URL_FOR_ALL : sprintf(URL_FOR_CATEGORY, category: category)
end