Class: Adam::Killboard
- Inherits:
-
Object
- Object
- Adam::Killboard
- Defined in:
- lib/adam/killboard.rb,
lib/adam/killboard/feed_error.rb
Defined Under Namespace
Classes: FeedError
Instance Attribute Summary collapse
-
#feed_uri ⇒ Object
readonly
Returns the value of attribute feed_uri.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Killboard
constructor
A new instance of Killboard.
-
#killmails(options = {}) ⇒ Object
Parses an EDK-compatible feed and returns an array of killmail strings.
Constructor Details
#initialize(options = {}) ⇒ Killboard
Returns a new instance of Killboard.
14 15 16 |
# File 'lib/adam/killboard.rb', line 14 def initialize( = {}) @uri, @feed_uri = [:uri], [:feed_uri] end |
Instance Attribute Details
#feed_uri ⇒ Object (readonly)
Returns the value of attribute feed_uri.
12 13 14 |
# File 'lib/adam/killboard.rb', line 12 def feed_uri @feed_uri end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
12 13 14 |
# File 'lib/adam/killboard.rb', line 12 def uri @uri end |
Instance Method Details
#killmails(options = {}) ⇒ Object
Parses an EDK-compatible feed and returns an array of killmail strings.
options
is a hash with these keys:
-
week
- An integer determining which week to import. Defaults to current week. -
year
- An integer determining which year to import the week from. Defaults to current year.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/adam/killboard.rb', line 23 def killmails( = {}) = { :week => Date.today.cweek, :year => Date.today.year }.merge!() uri = URI.parse(@feed_uri) query_string = { 'week' => [:week], 'year' => [:year], 'nocache' => Time.now.to_i }.map{ |k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join("&") response = nil connection = Net::HTTP.new(uri.host, uri.port) connection.start do |http| tries = 0 begin response = http.get(uri.path + '?' + uri.query + '&' + query_string) rescue Timeout::Error tries += 1 sleep 10 retry if tries < 3 end end xml = Hpricot.XML(response.body) raise FeedError.new(response.body), "Feed malformed" unless xml.at('/rss') killmails = xml.search("//item/description").collect { |element| element.inner_text } end |