Class: Wowr::API

Inherits:
Object
  • Object
show all
Defined in:
lib/wowr.rb

Constant Summary collapse

@@armory_url =
'http://www.wowarmory.com/'
@@eu_armory_url =
'http://eu.wowarmory.com/'
@@search_url =
'search.xml'
@@character_sheet_url =
'character-sheet.xml'
@@character_talents_url =
'character-talents.xml'
@@character_skills_url =
'character-skills.xml'
@@character_reputation_url =
'character-reputation.xml'
@@guild_info_url =
'guild-info.xml'
@@item_info_url =
'item-info.xml'
@@item_tooltip_url =
'item-tooltip.xml'
@@arena_team_url =
'team-info.xml'
@@max_connection_tries =
10
@@cache_directory_path =
'cache/'
@@classes =

Part of the rails plugin stuff? @@profession_icons = { :alchemy => “Trade_Alchemy.png”, :blacksmithing => “Trade_BlackSmithing.png”, :engineering => “Trade_Engineering.png”, :enchanting => “Trade_Engraving.png”, :jewelcrafting => “INV_Hammer_21.png”, :herbalism => “Trade_Herbalism.png”, :leatherworking => “Trade_LeatherWorking.png”, :mining => “Trade_Mining.png”, :tailoring => “Trade_Tailoring.png”, :skinning => “INV_Weapon_ShortBlade_01.png” }

{
	1 => 'Warrior',
	2 => 'Paladin',
	3 => 'Hunter',
	4 => 'Rogue',
	5 => 'Priest',
	#6 => 'Gold Farmer', # there is no class 6
	7 => 'Shaman',
	8 => 'Mage',
	9 => 'Warlock',
	#10 => 'Purveyor of Baked Goods', # there is no class 10
	11 => 'Druid'
}
@@genders =
{
	0 => 'Male',
	1 => 'Female'
}
@@races =
{
	1 => 'Human',
	1 => 'Orc',
	3 => 'Dwarf',
	4 => 'Night Elf',
	5 => 'Undead',
	6 => 'Tauren',
	7 => 'Gnome',
	8 => 'Troll',
	#9 => 'Pandaren', # there is no race 9
	10 => 'Blood Elf',
	11 => 'Draenei'
}
@@search_types =
{
	#:all => 'all',	# TODO: All is too complex at the moment, API doesn't return all results in one query
	:item => 'items',
	:character => 'characters',
	:guild => 'guilds',
	:arena_team => 'arenateams'
}
@@arena_team_sizes =
[2, 3, 5]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {:character_name => nil, :guild_name => nil, :realm => nil, :locale => :us, :caching => false}) ⇒ API

You can set up the API with an optional default guild and realm These will be used in all your API requests unless you specify otherwise For item requests, the locale will not matter in results, but may affect the speed of replies Caching is off by default TODO: are these nil declarations pointless?



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/wowr.rb', line 122

def initialize(options = {:character_name => nil,
													:guild_name => nil,
													:realm => nil,
													:locale => :us,
													:caching => false})
	@character_name = options[:character_name]
	@guild_name			= options[:guild_name]
	@realm					= options[:realm]
	@locale					= options[:locale] #|| :us
	@caching				= options[:caching]
end

Instance Attribute Details

#cachingObject

Returns the value of attribute caching.



115
116
117
# File 'lib/wowr.rb', line 115

def caching
  @caching
end

#character_nameObject

Returns the value of attribute character_name.



115
116
117
# File 'lib/wowr.rb', line 115

def character_name
  @character_name
end

#guild_nameObject

Returns the value of attribute guild_name.



115
116
117
# File 'lib/wowr.rb', line 115

def guild_name
  @guild_name
end

#localeObject

Returns the value of attribute locale.



115
116
117
# File 'lib/wowr.rb', line 115

def locale
  @locale
end

#realmObject

Returns the value of attribute realm.



115
116
117
# File 'lib/wowr.rb', line 115

def realm
  @realm
end

Instance Method Details

#clear_cache(cache_path = @@cache_directory_path) ⇒ Object

Clear the cache, optional filename



281
282
283
284
285
286
287
# File 'lib/wowr.rb', line 281

def clear_cache(cache_path = @@cache_directory_path)
	begin
		FileUtils.remove_dir(cache_path)
	rescue Exception => e 

	end
end

#get_arena_team(options = {:team_name => :nil, :team_size => nil, :realm => @realm, :caching => @caching}) ⇒ Object



265
266
267
268
269
270
271
272
# File 'lib/wowr.rb', line 265

def get_arena_team(options = {:team_name => :nil, :team_size => nil, :realm => @realm, :caching => @caching})
	if !@@arena_team_sizes.include?(options[:team_size])
		raise Wowr::Exceptions::InvalidArenaTeamSize.new("Arena teams size must be: #{@@arena_team_sizes.inspect}")
	end
	
	xml = get_xml(@@arena_team_url, options)
	return Wowr::Classes::ArenaTeam.new(xml%'arenaTeam')
end

#get_character_sheet(options = {:character_name => @character_name, :realm => @realm, :caching => @caching}) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/wowr.rb', line 192

def get_character_sheet(options = {:character_name => @character_name, :realm => @realm, :caching => @caching})			
	xml = get_xml(@@character_sheet_url, options)
	
	# resist_types = ['arcane', 'fire', 'frost', 'holy', 'nature', 'shadow']
	# @resistances = {}
	# resist_types.each do |res|
	# 	@resistances[res] = Wowr::Classes::Resistance.new(xml%'resistances'%res)
	# end
	# puts @resistances.to_yaml
	puts xml
	return Wowr::Classes::CharacterSheet.new(xml)
end

#get_guild(options = {:guild_name => @guild_name, :realm => @realm, :caching => @caching}) ⇒ Object



215
216
217
218
# File 'lib/wowr.rb', line 215

def get_guild(options = {:guild_name => @guild_name, :realm => @realm, :caching => @caching})
	xml = get_xml(@@guild_info_url, options)
	return Wowr::Classes::Guild.new(xml)
end

#get_item_info(options = {:item_id => nil, :locale => @locale, :caching => @caching}) ⇒ Object

end



236
237
238
239
240
241
242
243
# File 'lib/wowr.rb', line 236

def get_item_info(options = {:item_id => nil, :locale => @locale, :caching => @caching})
	xml = get_xml(@@item_info_url, options)
	if (xml%'itemInfo'%'item')
		return Wowr::Classes::ItemInfo.new(xml%'itemInfo'%'item')
	else
		return nil
	end
end

#get_item_tooltip(options = {:item_id => nil, :caching => @caching}) ⇒ Object



245
246
247
248
249
250
251
252
253
254
# File 'lib/wowr.rb', line 245

def get_item_tooltip(options = {:item_id => nil, :caching => @caching})
	xml = get_xml(@@item_tooltip_url, options)
	
	# tooltip returns empty document when not found
	if xml.nil?
		return nil
		#raise Wowr::Exceptions::ItemNotFound.new("Item not found with id: #{options[:item_id]}")
	end
	return Wowr::Classes::ItemTooltip.new(xml%'itemTooltip')
end

#search(options = {:search => nil, :type => nil}) ⇒ Object

General-purpose search All specific searches are wrappers around this method. Caching is disabled for searching



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/wowr.rb', line 138

def search(options = {:search => nil, :type => nil})
	if @@search_types.include? options[:type]
		raise Wowr::Exceptions::InvalidSearchType.new
	end
	
	if options[:search].nil?
		raise Wowr::Exceptions::NoSearchString.new
	end
	
	options.merge!(:caching => false)
	
	xml = get_xml(@@search_url, options)
	
	results = []
	
	if (xml) && (xml%'armorySearch') && (xml%'armorySearch'%'searchResults')
		case options[:type]
			
			# TODO: Filter stuff
			when @@search_types[:item]
				(xml%'armorySearch'%'searchResults'%'items'/:item).each do |item|
					results << Wowr::Classes::SearchItem.new(item)
				end
			
			when @@search_types[:character]
				(xml%'armorySearch'%'searchResults'%'characters'/:character).each do |char|
					results << Wowr::Classes::Character.new(char)
				end
			
			when @@search_types[:guild]
				(xml%'armorySearch'%'searchResults'%'guilds'/:guild).each do |guild|
					results << Wowr::Classes::Guild.new(guild)
				end
			
			when @@search_types[:arena_team]
				(xml%'armorySearch'%'searchResults'%'arenaTeams'/:arenaTeam).each do |team|
					results << Wowr::Classes::ArenaTeam.new(team)
				end
		end
	end
	
	return results
end

#search_arena_teams(options = {}) ⇒ Object

Arena Teams Caching is disabled for searching



260
261
262
263
# File 'lib/wowr.rb', line 260

def search_arena_teams(options = {})
	options.merge!(:type => @@search_types[:arena_team])
	return search(options)
end

#search_characters(options = {:name => @character_name}) ⇒ Object

Characters Note searches go across all realms by default Caching is disabled for searching



187
188
189
190
# File 'lib/wowr.rb', line 187

def search_characters(options = {:name => @character_name})
	options.merge!(:type => @@search_types[:character])
	return search(options)
end

#search_guilds(options = {:search => @guild_name, :locale => @locale}) ⇒ Object

Guilds Note searches go across all realms by default Caching is disabled for searching



210
211
212
213
# File 'lib/wowr.rb', line 210

def search_guilds(options = {:search => @guild_name, :locale => @locale})
	options.merge!(:type => @@search_types[:guild])
	return search(options)
end

#search_items(options = {:search => nil}) ⇒ Object

Items Items are not realm-specific Caching is disabled for searching



225
226
227
228
# File 'lib/wowr.rb', line 225

def search_items(options = {:search => nil})
	options.merge!(:type => @@search_types[:item])
	return search(options)
end