Class: Elekk::Armory
- Inherits:
-
Object
- Object
- Elekk::Armory
- Defined in:
- lib/elekk/armory.rb
Instance Attribute Summary collapse
-
#realm ⇒ Object
Returns the value of attribute realm.
-
#region ⇒ Object
readonly
Returns the value of attribute region.
Class Method Summary collapse
Instance Method Summary collapse
- #base ⇒ Object
- #character(name, realm = @realm) ⇒ Object
- #collate_feeds(feeds) ⇒ Object
- #get_feed(characters, realm = nil) ⇒ Object
- #get_feeds(characters, realm = nil) ⇒ Object
- #get_xml(resource, params = nil) ⇒ Object
-
#initialize(*keywords) ⇒ Armory
constructor
A new instance of Armory.
- #search(query, type = :all) ⇒ Object
- #url(path) ⇒ Object
Constructor Details
#initialize(*keywords) ⇒ Armory
Returns a new instance of Armory.
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/elekk/armory.rb', line 7 def initialize(*keywords) opts = keywords.pop if keywords.last.class == {}.class @realm = keywords.shift if keywords.length > 0 @region = keywords.shift if keywords.length > 0 if opts @realm ||= opts[:realm] @region ||= opts[:region] end @region ||= Elekk::default_region end |
Instance Attribute Details
#realm ⇒ Object
Returns the value of attribute realm.
4 5 6 |
# File 'lib/elekk/armory.rb', line 4 def realm @realm end |
#region ⇒ Object (readonly)
Returns the value of attribute region.
5 6 7 |
# File 'lib/elekk/armory.rb', line 5 def region @region end |
Class Method Details
.icon_url(icon) ⇒ Object
105 106 107 |
# File 'lib/elekk/armory.rb', line 105 def self.icon_url(icon) url "wow-icons/_images/51x51/#{icon}.jpg" end |
Instance Method Details
#base ⇒ Object
24 25 26 |
# File 'lib/elekk/armory.rb', line 24 def base "http://#{@region}.wowarmory.com/" end |
#character(name, realm = @realm) ⇒ Object
20 21 22 |
# File 'lib/elekk/armory.rb', line 20 def character(name, realm=@realm) Character.new name, realm, :region => @region, :armory => self end |
#collate_feeds(feeds) ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/elekk/armory.rb', line 92 def collate_feeds(feeds) coll = {} feeds.each do |e| coll[e[:coll_id]] ||= e coll[e[:coll_id]] << e end coll end |
#get_feed(characters, realm = nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/elekk/armory.rb', line 36 def get_feed(characters, realm=nil) characters = [characters] unless characters.is_a? Array feed_xml = get_xml 'character-feed.atom', :r => (realm || @realm), :cn => characters.sort.join(',') feed = feed_xml.css('entry').collect do |c| item = { :title => c.at_css('title').content, :id => c.at_css('id').content, :link => c.at_css('link').content, :time => Time.parse(c.at_css('published').content), :content => c.at_css('content').content } item[:character] = $1 if item[:title] =~ /(\w+)/ if item[:title] =~ /\[((.*)\s+\w+\s*\(([^(]+)\))\]\s+\d+\s+times/ item[:collator] = $1 item[:boss] = $2 item[:instance] = $3 item[:type] = :boss end if item[:content] =~ /<a href=.*#ach(\d+)">([^<]*)<\/a>/ item[:achievement_id] = $1 item[:achievement_name] = $2 item[:collator] = $2 if item[:title] =~ /step \[([^\]]*)\]/ item[:type] = :step item[:collator] = item[:achievement_name] + " - " + $1 item[:step] = $1 else item[:type] = :achievement end end if item[:content] =~ /item-info\.xml\?i=(\d+).*\[(.*)\]/ item[:item_id] = $1 item[:item_name] = $2 item[:collator] = $2 item[:type] = :item end item[:coll_id] = [item[:type], item[:collator], item[:time]].hash item end end |
#get_feeds(characters, realm = nil) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/elekk/armory.rb', line 76 def get_feeds(characters, realm=nil) # split characters into least set of batches of no more than 5, # no less than 2 batches = characters.sort / 5 if batches.last.length < 2 batches.last.unshift batches[batches.length - 2].pop end feeds = [] batches.collect {|chars| feeds += get_feed chars, realm } feeds.sort {|a,b| b[:time] <=> a[:time]} end |
#get_xml(resource, params = nil) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/elekk/armory.rb', line 28 def get_xml(resource, params=nil) resource+= '.xml' unless resource =~ /\./ response = HTTP.xml(url(resource), params, { :cache_timeout => 24*3600, :user_agent => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9' }) end |
#search(query, type = :all) ⇒ Object
101 102 103 |
# File 'lib/elekk/armory.rb', line 101 def search(query, type=:all) xml = get_xml('search', :searchQuery => query, :searchType => type.to_s) end |
#url(path) ⇒ Object
109 110 111 |
# File 'lib/elekk/armory.rb', line 109 def url(path) base + path end |