wowr 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README CHANGED
@@ -19,12 +19,14 @@ When initialising the library, it is possible to set a number of parameters to b
19
19
 
20
20
  # all parameters optional
21
21
  api = Wowr::API.new(:character_name => 'Foo',
22
- :guild_name => 'Bar',
23
- :realm => 'Baz',
24
- :locale => :eu) # defaults to US
22
+ :guild_name => 'Bar',
23
+ :realm => 'Baz',
24
+ :locale => 'eu', # defaults to US www
25
+ :lang => 'fr_fr', # remove for locale default language
26
+ :caching => true # defaults to false)
25
27
 
26
28
  # Character requests
27
- my_char = api.get_character_sheet
29
+ my_char = api.get_character_sheet # gets character with API default values
28
30
  char_boo = api.get_character_sheet(:character_name => 'Boo')
29
31
  chars = api.search_characters(:search => 'Cake')
30
32
 
@@ -15,17 +15,22 @@ require 'cgi'
15
15
  require 'fileutils' # for making directories :S
16
16
 
17
17
  # TODO: what does this do?
18
- #$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
18
+ $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
19
+
20
+ # require 'lib/wowr/exceptions.rb'
21
+ # require 'lib/wowr/extensions.rb'
22
+ # require 'lib/wowr/classes.rb'
23
+
24
+ require 'wowr/exceptions.rb'
25
+ require 'wowr/extensions.rb'
26
+ require 'wowr/classes.rb'
19
27
 
20
- require 'lib/wowr/exceptions.rb'
21
- require 'lib/wowr/extensions.rb'
22
- require 'lib/wowr/classes.rb'
23
28
 
24
29
  module Wowr
25
30
  class API
26
31
 
27
- @@armory_url = 'http://www.wowarmory.com/'
28
- @@eu_armory_url = 'http://eu.wowarmory.com/'
32
+ @@armory_url_base = 'wowarmory.com/'
33
+ # @@eu_armory_url = 'http://eu.wowarmory.com/'
29
34
 
30
35
  @@search_url = 'search.xml'
31
36
 
@@ -112,30 +117,49 @@ module Wowr
112
117
 
113
118
  @@arena_team_sizes = [2, 3, 5]
114
119
 
115
- attr_accessor :character_name, :guild_name, :realm, :locale, :caching
120
+ attr_accessor :character_name, :guild_name, :realm, :locale, :lang, :caching, :debug
116
121
 
117
122
  # You can set up the API with an optional default guild and realm
118
123
  # These will be used in all your API requests unless you specify otherwise
119
124
  # For item requests, the locale will not matter in results, but may affect the speed of replies
120
125
  # Caching is off by default
121
126
  # TODO: are these nil declarations pointless?
122
- def initialize(options = {:character_name => nil,
123
- :guild_name => nil,
124
- :realm => nil,
125
- :locale => :us,
126
- :caching => false})
127
+ # :character_name => nil,
128
+ # :guild_name => nil,
129
+ # :realm => nil,
130
+ # :locale => 'us',
131
+ # :caching => false,
132
+ # :debug => false
133
+ def initialize(options = {})
127
134
  @character_name = options[:character_name]
128
135
  @guild_name = options[:guild_name]
129
136
  @realm = options[:realm]
130
- @locale = options[:locale] #|| :us
137
+ @locale = options[:locale] || :us
131
138
  @caching = options[:caching]
139
+
140
+ if (options[:lang].nil?)
141
+ @lang = 'default'
142
+ else
143
+ @lang = options[:lang]
144
+ end
145
+
146
+ @debug = options[:debug]
132
147
  end
133
148
 
134
149
 
150
+ # def self.test
151
+ # puts @debug
152
+ # puts @caching
153
+ # puts @locale
154
+ # end
155
+
156
+
135
157
  # General-purpose search
136
158
  # All specific searches are wrappers around this method.
137
159
  # Caching is disabled for searching
138
160
  def search(options = {:search => nil, :type => nil})
161
+ options = merge_defaults(options)
162
+
139
163
  if @@search_types.include? options[:type]
140
164
  raise Wowr::Exceptions::InvalidSearchType.new
141
165
  end
@@ -184,12 +208,17 @@ module Wowr
184
208
  # Characters
185
209
  # Note searches go across all realms by default
186
210
  # Caching is disabled for searching
187
- def search_characters(options = {:name => @character_name})
211
+ def search_characters(options = {:name => nil})
188
212
  options.merge!(:type => @@search_types[:character])
189
213
  return search(options)
190
214
  end
191
215
 
192
- def get_character_sheet(options = {:character_name => @character_name, :realm => @realm, :caching => @caching})
216
+ def get_character_sheet(options = {})
217
+
218
+ options = {:character_name => @character_name}.merge(options) if (!@character_name.nil?)
219
+
220
+ options = merge_defaults(options)
221
+
193
222
  xml = get_xml(@@character_sheet_url, options)
194
223
 
195
224
  # resist_types = ['arcane', 'fire', 'frost', 'holy', 'nature', 'shadow']
@@ -198,7 +227,6 @@ module Wowr
198
227
  # @resistances[res] = Wowr::Classes::Resistance.new(xml%'resistances'%res)
199
228
  # end
200
229
  # puts @resistances.to_yaml
201
- puts xml
202
230
  return Wowr::Classes::CharacterSheet.new(xml)
203
231
  end
204
232
 
@@ -207,18 +235,24 @@ module Wowr
207
235
  # Guilds
208
236
  # Note searches go across all realms by default
209
237
  # Caching is disabled for searching
210
- def search_guilds(options = {:search => @guild_name, :locale => @locale})
211
- options.merge!(:type => @@search_types[:guild])
238
+ def search_guilds(options = {:search => nil})
239
+ options.merge!(:type => @@search_types[:guild],
240
+ :caching => false)
241
+
212
242
  return search(options)
213
243
  end
214
244
 
215
- def get_guild(options = {:guild_name => @guild_name, :realm => @realm, :caching => @caching})
245
+ def get_guild(options = {})
246
+ # overwrite default with parameters, but only if the defaults aren't blank
247
+ options = {:guild_name => @guild_name}.merge(options) if (!@guild_name.nil?)
248
+
249
+ options = merge_defaults(options)
250
+
216
251
  xml = get_xml(@@guild_info_url, options)
217
252
  return Wowr::Classes::Guild.new(xml)
218
253
  end
219
254
 
220
255
 
221
-
222
256
  # Items
223
257
  # Items are not realm-specific
224
258
  # Caching is disabled for searching
@@ -233,7 +267,9 @@ module Wowr
233
267
  #return Wowr::Classes::ItemTooltip.new(xml%'itemTooltip')
234
268
  #end
235
269
 
236
- def get_item_info(options = {:item_id => nil, :locale => @locale, :caching => @caching})
270
+ def get_item_info(options = {:item_id => nil})
271
+ options = merge_defaults(options)
272
+
237
273
  xml = get_xml(@@item_info_url, options)
238
274
  if (xml%'itemInfo'%'item')
239
275
  return Wowr::Classes::ItemInfo.new(xml%'itemInfo'%'item')
@@ -242,7 +278,9 @@ module Wowr
242
278
  end
243
279
  end
244
280
 
245
- def get_item_tooltip(options = {:item_id => nil, :caching => @caching})
281
+ def get_item_tooltip(options = {:item_id => nil})
282
+ options = merge_defaults(options)
283
+
246
284
  xml = get_xml(@@item_tooltip_url, options)
247
285
 
248
286
  # tooltip returns empty document when not found
@@ -262,7 +300,9 @@ module Wowr
262
300
  return search(options)
263
301
  end
264
302
 
265
- def get_arena_team(options = {:team_name => :nil, :team_size => nil, :realm => @realm, :caching => @caching})
303
+ def get_arena_team(options = {})
304
+ options = merge_defaults(options)
305
+
266
306
  if !@@arena_team_sizes.include?(options[:team_size])
267
307
  raise Wowr::Exceptions::InvalidArenaTeamSize.new("Arena teams size must be: #{@@arena_team_sizes.inspect}")
268
308
  end
@@ -287,10 +327,34 @@ module Wowr
287
327
  end
288
328
 
289
329
 
330
+
331
+ def merge_defaults(options = {})
332
+ # defaults[:name] = @charater_name if @charater_name
333
+ # defaults[:name] = @charater_name if @charater_name
334
+ defaults = {}
335
+ defaults[:realm] = @realm if @realm
336
+ defaults[:locale] = @locale if @locale
337
+ defaults[:lang] = @lang if @lang
338
+ defaults[:caching] = @caching if @caching
339
+ defaults[:debug] = @debug if @debug
340
+
341
+ # defaults = {:realm => @realm,
342
+ # :locale => @locale,
343
+ # :lang => @lang,
344
+ # :caching => @caching,
345
+ # :debug => @debug}
346
+
347
+ # overwrite defaults with any given options
348
+ defaults.merge!(options)
349
+ end
350
+
351
+
290
352
  protected
291
353
 
292
354
  # TODO: pretty damn hacky
293
- def get_xml(url, options = {:caching => @caching})
355
+ def get_xml(url, options = {})
356
+ # puts options.to_yaml
357
+
294
358
  # better way of doing this?
295
359
  # Map custom keys to the HTTP request values
296
360
  reqs = {
@@ -315,20 +379,24 @@ module Wowr
315
379
  query = '?' + params.join('&')
316
380
  end
317
381
 
318
- locale = options[:locale] || @locale
382
+ # locale = options[:locale] || @locale
319
383
 
320
- base = self.base_url(locale)
384
+ base = self.base_url(options[:locale])
321
385
  full_query = base + url + query
322
386
 
323
- #puts full_query
387
+ if options[:debug]
388
+ puts full_query
389
+ end
324
390
 
325
391
  if options[:caching]
326
- response = get_cache(full_query)
392
+ response = get_cache(full_query, options)
327
393
  else
328
394
  response = http_request(full_query)
329
395
  end
330
396
 
331
- # puts response
397
+ if options[:debug]
398
+ # puts response
399
+ end
332
400
 
333
401
  doc = Hpricot.XML(response)
334
402
  begin
@@ -354,17 +422,25 @@ module Wowr
354
422
  end
355
423
 
356
424
  # TODO Rename
357
- def http_request(url)
358
- req = Net::HTTP::Get.new(url)
425
+ def http_request(url, options = {})
426
+
427
+ req = Net::HTTP::Get.new(url)#, headers)
359
428
  req["user-agent"] = "Mozilla/5.0 Gecko/20070219 Firefox/2.0.0.2"
429
+ req["cookie"] = "cookieMenu=all; cookieLangId=" + options[:lang] + "; cookies=true;"
360
430
 
361
431
  uri = URI.parse(url)
362
432
 
363
- http = Net::HTTP.new uri.host, uri.port
364
- http.start do
365
- res = http.request req
366
- response = res.body
367
- end
433
+ http = Net::HTTP.new(uri.host, uri.port)
434
+
435
+ begin
436
+
437
+ http.start do
438
+ res = http.request req
439
+ response = res.body
440
+ end
441
+ rescue
442
+ raise Wowr::Exceptions::ServerDoesNotExist.new('Specified server at ' + url + ' does not exist.');
443
+ end
368
444
 
369
445
  # tries = 0
370
446
  # response = case res
@@ -408,17 +484,23 @@ module Wowr
408
484
  end
409
485
 
410
486
 
411
- def get_cache(url)
412
- path = @@cache_directory_path + url_to_filename(url);
487
+ def get_cache(url, options = {})
488
+
489
+ path = @@cache_directory_path + options[:lang] + '/' + url_to_filename(url)
413
490
 
414
491
  # file doesn't exist, make it
415
492
  if !File.exists?(path)
493
+ if options[:debug]
494
+ puts 'Cache doesn\'t exist, making: ' + path
495
+ end
496
+
497
+ FileUtils.mkdir_p(localised_cache_path(options[:lang])) unless File.directory?(localised_cache_path(options[:lang]))
416
498
 
417
499
  # TODO: Hacky
418
- FileUtils.mkdir_p(@@cache_directory_path + 'us/') unless File.directory?(@@cache_directory_path + 'us/')
419
- FileUtils.mkdir_p(@@cache_directory_path + 'eu/') unless File.directory?(@@cache_directory_path + 'eu/')
500
+ # FileUtils.mkdir_p(@@cache_directory_path + 'us/') unless File.directory?(@@cache_directory_path + 'us/')
501
+ # FileUtils.mkdir_p(@@cache_directory_path + 'eu/') unless File.directory?(@@cache_directory_path + 'eu/')
420
502
 
421
- xml_content = http_request(url)
503
+ xml_content = http_request(url, options)
422
504
 
423
505
  # write the cache
424
506
  file = File.open(path, File::WRONLY|File::TRUNC|File::CREAT)
@@ -428,6 +510,10 @@ module Wowr
428
510
 
429
511
  # file exists, return the contents
430
512
  else
513
+ if options[:debug]
514
+ puts 'Cache already exists, read: ' + path
515
+ end
516
+
431
517
  file = File.open(path, 'r')
432
518
  xml_content = file.read
433
519
  file.close
@@ -439,23 +525,33 @@ module Wowr
439
525
 
440
526
  # :nodoc:
441
527
  # remove http://
528
+ # Kind of assuming incoming URL is the same as the current locale
442
529
  def url_to_filename(url)
443
- if (url.include?(@@armory_url))
444
- path = 'us/' + url.gsub(@@eu_armory_url, '')
445
- elsif (url.include?(@@eu_armory_url))
446
- path = 'eu/' + url.gsub(@@eu_armory_url, '')
447
- end
530
+ path = url[7..-1]
448
531
 
532
+
533
+ path = url.gsub(base_url, '')
534
+ # if (url.include?(@@armory_url))
535
+ # path = 'us/' + url.gsub(@@eu_armory_url, '')
536
+ # elsif (url.include?(@@eu_armory_url))
537
+ # path = 'eu/' + url.gsub(@@eu_armory_url, '')
538
+ # end
539
+ #
449
540
  return path
450
541
  end
451
542
 
452
543
 
544
+ def localised_cache_path(lang = @lang)
545
+ @@cache_directory_path + lang
546
+ end
547
+
548
+
453
549
  # :nodoc:
454
550
  def base_url(locale = @locale)
455
- if locale == :eu
456
- @@eu_armory_url
551
+ if locale == :us
552
+ 'http://www.' + @@armory_url_base
457
553
  else
458
- @@armory_url
554
+ 'http://' + locale + '.' + @@armory_url_base
459
555
  end
460
556
  end
461
557
 
@@ -176,7 +176,15 @@ module Wowr
176
176
  @battle_group = elem[:battleGroup]
177
177
 
178
178
  # format is February 11, 2008
179
- @last_modified = elem[:lastModified] == "" ? nil : DateTime.parse(elem[:lastModified])
179
+ # except when it's korean, and then it's 2008년 5월 11일 ()
180
+ # tw is 2008年5月11日 (2008年5月11日)
181
+ # TODO: Datetime doesn't parse other languages nicely
182
+ # Until then, just save it as a string
183
+ begin
184
+ @last_modified = elem[:lastModified] == "" ? nil : DateTime.parse(elem[:lastModified])
185
+ rescue
186
+ @last_modified = elem[:lastModified] == "" ? nil : elem[:lastModified]
187
+ end
180
188
  #@last_modified = elem[:lastModified]#.to_time
181
189
 
182
190
  @arena_teams = []
@@ -14,6 +14,9 @@ module Wowr
14
14
  class EmptyPage < StandardError
15
15
  end
16
16
 
17
+ class ServerDoesNotExist < StandardError
18
+ end
19
+
17
20
  # errCode="noCharacter"
18
21
  class CharacterNotFound < StandardError
19
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wowr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Humphreys
@@ -57,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
57
  requirements: []
58
58
 
59
59
  rubyforge_project: wowr
60
- rubygems_version: 1.0.1
60
+ rubygems_version: 1.1.1
61
61
  signing_key:
62
62
  specification_version: 2
63
63
  summary: An API wrapper for the World of Warcraft Armory.