Class: Adam::Killboard

Inherits:
Object
  • Object
show all
Defined in:
lib/adam/killboard.rb,
lib/adam/killboard/feed_error.rb

Defined Under Namespace

Classes: FeedError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Killboard

Returns a new instance of Killboard.



14
15
16
# File 'lib/adam/killboard.rb', line 14

def initialize(options = {})      
  @uri, @feed_uri = options[:uri], options[:feed_uri]
end

Instance Attribute Details

#feed_uriObject (readonly)

Returns the value of attribute feed_uri.



12
13
14
# File 'lib/adam/killboard.rb', line 12

def feed_uri
  @feed_uri
end

#uriObject (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.

Raises:



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(options = {})
  options = { :week => Date.today.cweek, :year => Date.today.year }.merge!(options)
  
  uri = URI.parse(@feed_uri)
          
  query_string = { 'week' => options[:week], 'year' => options[: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