forked from rspeicher/wowr
/
wowr.rb
executable file
·1282 lines (1022 loc) · 39.2 KB
/
wowr.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Wowr - Ruby library for the World of Warcraft Armory
# http://wowr.rubyforge.org/
# Written by Ben Humphreys
# http://benhumphreys.co.uk/
# Maintained By Peter Wood
# http://narwar.net/
#
# Author:: Ben Humphreys
# Author:: Peter Wood
begin
require 'hpricot' # version 0.6
rescue LoadError
require 'rubygems'
require 'hpricot'
end
require 'net/http'
require 'net/https'
require 'cgi'
require 'fileutils'
require 'json'
$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'wowr/exceptions.rb'
require 'wowr/extensions.rb'
require 'wowr/calendar.rb'
require 'wowr/character.rb'
require 'wowr/achievements.rb'
require 'wowr/guild.rb'
require 'wowr/item.rb'
require 'wowr/arena_team.rb'
require 'wowr/dungeon.rb'
require 'wowr/guild_bank.rb'
module Wowr
class API
VERSION = '0.4.1'
@@armory_base_url = 'wowarmory.com/'
@@login_base_url = 'battle.net/'
@@persistant_cookie = 'COM-warcraft'
@@temporary_cookie = 'JSESSIONID'
@@search_url = 'search.xml'
@@character_sheet_url = 'character-sheet.xml'
@@character_talents_url = 'character-talents.xml'
@@character_reputation_url = 'character-reputation.xml'
@@character_achievements_url = 'character-achievements.xml'
@@calendar_user_url = 'vault/calendar/month-user.json'
@@calendar_world_url = 'vault/calendar/month-world.json'
@@calendar_detail_url = 'vault/calendar/detail.json'
@@guild_info_url = 'guild-info.xml'
@@item_info_url = 'item-info.xml'
@@item_tooltip_url = 'item-tooltip.xml'
@@arena_team_url = 'team-info.xml'
@@guild_bank_contents_url = 'vault/guild-bank-contents.xml'
@@guild_bank_log_url = 'vault/guild-bank-log.xml'
@@login_url = 'login/login.xml'
@@dungeons_url = 'data/dungeons.xml'
@@dungeons_strings_url = 'data/dungeonStrings.xml'
@@max_connection_tries = 10
@@cache_directory_path = 'cache/'
@@user_agent = 'Mozilla/5.0 Gecko/20070219 Firefox/2.0.0.2'
@@default_cache_timeout = (7*24*60*60)
@@failed_cache_timeout = (60*60*24)
@@cache_failed_requests = true # cache requests that resulted in an error from the armory
cattr_accessor :armory_base_url, :search_url,
:character_sheet_url, :character_talents_url, :character_reputation_url,
:guild_info_url,
:item_info_url, :item_tooltip_url,
:arena_team_url,
:guild_bank_contents_url, :guild_bank_log_url,
:login_url,
:dungeons_url, :dungeons_strings_url,
:max_connection_tries,
:cache_directory_path,
:default_cache_timeout, :failed_cache_timeout, :cache_failed_requests,
:calendar_user_url, :calendar_world_url, :calendar_detail_url, :persistant_cookie, :temporary_cookie
@@search_types = {
:item => 'items',
:character => 'characters',
:guild => 'guilds',
:arena_team => 'arenateams'
}
@@arena_team_sizes = [2, 3, 5]
@@calendar_world_types = ['player', 'holiday', 'bg', 'darkmoon', 'raidLockout', 'raidReset', 'holidayWeekly']
@@calendar_user_types = ['raid', 'dungeon', 'pvp', 'meeting', 'other']
attr_accessor :character_name, :guild_name, :realm, :locale, :lang, :caching, :cache_timeout, :debug
# Constructor
# Accepts an optional hash of parameters to create defaults for all API requests
# * options (Hash) - Hash used to set default values for all API requests
def initialize(options = {})
@character_name = options[:character_name]
@guild_name = options[:guild_name]
@realm = options[:realm]
@locale = options[:locale] || 'us'
@lang = options[:lang].nil? ? 'default' : options[:lang]
@caching = options[:caching].nil? ? true : options[:caching]
@cache_timeout = options[:cache_timeout] || @@default_cache_timeout
@debug = options[:debug] || false
end
# General-purpose search
# All specific searches are wrappers around this method. Best to use those instead.
# Returns an array of results of the type requested (Wowr::Classes::SearchCharacter etc.) or an empty array.
# Searches across all realms.
# Caching is disabled for searching.
# * string (String) Search string
# * options (Hash) Optional hash of arguments identical to those used in the API constructor (realm, debug, cache etc.)
def search(string, options = {})
if (string.is_a?(Hash))
options = string
else
options.merge!(:search => string)
end
options = merge_defaults(options)
if options[:search].nil? || options[:search].empty?
raise Wowr::Exceptions::NoSearchString.new
end
if !@@search_types.has_value?(options[:type])
raise Wowr::Exceptions::InvalidSearchType.new(options[:type])
end
options.merge!(:caching => false)
options.delete(:realm) # all searches are across realms
xml = get_xml(@@search_url, options)
results = []
if (xml) && (xml%'armorySearch') && (xml%'armorySearch'%'searchResults')
case options[:type]
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::SearchCharacter.new(char, self)
end
when @@search_types[:guild]
(xml%'armorySearch'%'searchResults'%'guilds'/:guild).each do |guild|
results << Wowr::Classes::SearchGuild.new(guild)
end
when @@search_types[:arena_team]
(xml%'armorySearch'%'searchResults'%'arenaTeams'/:arenaTeam).each do |team|
results << Wowr::Classes::SearchArenaTeam.new(team)
end
end
end
return results
end
# Characters
# Returns an array of results of Wowr::Classes::SearchCharacter or an empty array.
# Searches across all realms.
# Caching is disabled for searching.
# Parameters
# * name (String) Name of the character to search for
# * options (Hash) Optional hash of arguments identical to those used in the API constructor (realm, debug, cache etc.)
def search_characters(name, options = {})
if (name.is_a?(Hash))
options = name
else
options.merge!(:search => name)
end
options.merge!(:type => @@search_types[:character])
return search(options)
end
# Get the full details of a character.
# Requires realm.
# * name (String) Name of the character to get, defaults to that specified in constructor
# * options (Hash) Optional hash of arguments identical to those used in the API constructor (realm, debug, cache etc.)
def get_character(name = @character_name, options = {})
options = character_options(name, options)
character_sheet = get_xml(@@character_sheet_url, options)
character_reputation = get_xml(@@character_reputation_url, options)
# FIXME
if true
return Wowr::Classes::FullCharacter.new(character_sheet,
character_reputation,
self)
else
raise Wowr::Excceptions::CharacterNotFound.new(options[:character_name])
end
end
# DEPRECATED
# See get_character
def get_character_sheet(name = @character_name, options = {})
return get_character(name, options)
end
# Get achievement infos for a character.
# Requires realm.
# * name (String) Name of the character to get, defaults to that specified in constructor
# * options (Hash) Optional hash of arguments identical to those used in the API constructor (realm, debug, cache etc.)
def get_character_achievements(name = @character_name, options = {})
options = character_options(name, options)
character_achievements = get_xml(@@character_achievements_url, options)
return Wowr::Classes::CharacterAchievementsInfo.new(character_achievements, self)
end
# Get details for all achievements in a category for a character.
# Requires realm.
# * achievement_category (Integer) ID of the achievement category
# * name (String) Name of the character to get, defaults to that specified in constructor
# * options (Hash) Optional hash of arguments identical to those used in the API constructor (realm, debug, cache etc.)
def get_character_achievements_category(achievement_category, name = @character_name, options = {})
options = character_options(name, options)
options[:achievement_category] = achievement_category.to_i
character_achievements_category = get_xml(@@character_achievements_url, options)
return Wowr::Classes::AchievementsList.new(character_achievements_category, self)
end
# Find all guilds with the given string, return array of Wowr::Classes::SearchGuild.
# Searches across all realms.
# Caching is disabled for searching.
# * name (String) Name of the guild to search for
# * options (Hash) Optional hash of arguments identical to those used in the API constructor (realm, debug, cache etc.)
def search_guilds(name, options = {})
if (name.is_a?(Hash))
options = name
else
options.merge!(:search => name)
end
options.delete(:realm)
options.merge!(:type => @@search_types[:guild])
return search(options)
end
# Get the guild details.
# Guild name is optional, assuming it's set in the api constructor.
# Requires realm.
# * name (String) Name of the guild to retrieve, defaults to that specified in constructor
# * options (Hash) Optional hash of arguments identical to those used in the API constructor (realm, debug, cache etc.)
def get_guild(name = @guild_name, options = {})
if (name.is_a?(Hash))
options = name
else
options.merge!(:guild_name => name)
end
options = merge_defaults(options)
if options[:guild_name].nil? || options[:guild_name] == ""
raise Wowr::Exceptions::GuildNameNotSet.new
elsif options[:realm].nil? || options[:realm].empty?
raise Wowr::Exceptions::RealmNotSet.new
end
xml = get_xml(@@guild_info_url, options)
if !(xml%'guildInfo').children.empty?
return Wowr::Classes::FullGuild.new(xml)
else
raise Wowr::Exceptions::GuildNotFound.new(options[:guild_name])
end
end
# Search for items with the specified name.
# Returns an array of Wowr::Classes::SearchItem.
# Searches across all realms.
# Can search for items based upon the following options => :source, :dungeon, :item_type ("armor, weapon, etc"), :item_slot (head, shoulders, etc), and :item_sub_type (leather, mail, etc)
# The dungeon param is the dungeon ID
# Caching is disabled for searching.
# * name (String) Name of the item
# * options (Hash) Optional hash of arguments identical to those used in the API constructor (realm, debug, cache etc.).
def search_items(name, options = {})
if (name.is_a?(Hash))
options = name
else
options.merge!(:search => name)
end
options.merge!(:type => @@search_types[:item])
puts options.inspect if options[:debug]
return search(options)
end
# Get the full item details (Wowr::Classes::FullItem) with the given id.
# Composite of Wowr::Classes::ItemInfo and Wowr::Classes::ItemTooltip data.
# Item requests are identical across realms.
# * id (Fixnum) ID of the item
# * options (Hash) Optional hash of arguments identical to those used in the API constructor (realm, debug, cache etc.)
def get_item(id, options = {})
if (id.is_a?(Hash))
options = id
else
options.merge!(:item_id => id)
end
options = merge_defaults(options)
options.delete(:realm)
info = get_xml(@@item_info_url, options)
tooltip = get_xml(@@item_tooltip_url, options)
if (info%'itemInfo'%'item') && !tooltip.nil?
return Wowr::Classes::FullItem.new(info%'itemInfo'%'item', tooltip%'itemTooltip', self)
else
raise Wowr::Exceptions::ItemNotFound.new(options[:item_id])
end
end
# Get the basic item information Wowr::Classes::ItemInfo.
# Item requests are identical across realms.
# * id (Fixnum) ID of the item
# * options (Hash) Optional hash of arguments identical to those used in the API constructor (realm, debug, cache etc.)
def get_item_info(id, options = {})
if (id.is_a?(Hash))
options = id
else
options.merge!(:item_id => id)
end
options = merge_defaults(options)
options.delete(:realm)
xml = get_xml(@@item_info_url, options)
if (xml%'itemInfo'%'item')
return Wowr::Classes::ItemInfo.new(xml%'itemInfo'%'item', self)
else
raise Wowr::Exceptions::ItemNotFound.new(options[:item_id])
end
end
# Get full item details including stats Wowr::Classes::ItemTooltip.
# Item requests are identical across realms.
# * id (Fixnum) ID of the item
# * options (Hash) Optional hash of arguments identical to those used in the API constructor (realm, debug, cache etc.)
def get_item_tooltip(id, options = {})
if (id.is_a?(Hash))
options = id
else
options.merge!(:item_id => id)
end
options = merge_defaults(options)
options.delete(:realm)
xml = get_xml(@@item_tooltip_url, options)
if !xml.nil?
return Wowr::Classes::ItemTooltip.new(xml%'itemTooltip')
else
raise Wowr::Exceptions::ItemNotFound.new(options[:item_id])
end
end
# Search for arena teams with the given name of any size.
# Returns an array of Wowr::Classes::SearchArenaTeam
# Searches across all realms.
# Caching is disabled for searching.
# * name (String) Name of the arena team to seach for
# * options (Hash) Optional hash of arguments identical to those used in the API constructor (realm, debug, cache etc.)
def search_arena_teams(name, options = {})
if (name.is_a?(Hash))
options = name
else
options.merge!(:search => name)
end
options.merge!(:type => @@search_types[:arena_team])
return search(options)
end
# Get the arena team of the given name and size, on the specified realm.
# Returns Wowr::Classes::FullArenaTeam
# Requires realm.
# * name (String) Team arena name
# * size (Fixnum) Must be 2, 3 or 5
# * options (Hash) Optional hash of arguments identical to those used in the API constructor (realm, debug, cache etc.)
def get_arena_team(name, size = nil, options = {})
if name.is_a?(Hash)
options = name
elsif size.is_a?(Hash)
options = size
options.merge!(:team_name => name)
else
options.merge!(:team_name => name, :team_size => size)
end
options = merge_defaults(options)
if options[:team_name].nil? || options[:team_name].empty?
raise Wowr::Exceptions::ArenaTeamNameNotSet.new
end
if options[:realm].nil? || options[:realm].empty?
raise Wowr::Exceptions::RealmNotSet.new
end
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)
if !(xml%'arenaTeam').children.empty?
return Wowr::Classes::ArenaTeam.new(xml%'arenaTeam')
else
raise Wowr::Exceptions::ArenaTeamNotFound.new(options[:team_name])
end
end
# Get the current items within the guild bank.
# Note that the bags and items the user can see is dependent on their privileges.
# Requires realm.
# * cookie (String) Cookie data returned by the login function.
# * guild_name (String) Guild name
# * options (Hash) Optional hash of arguments identical to those used in the API constructor (realm, debug, cache etc.)
def get_guild_bank_contents(cookie, name = @guild_name, options = {})
full_cookie = ' JSESSIONID='+cookie
if (cookie.is_a?(Hash))
options = full_cookie
elsif (guild_name.is_a?(Hash))
options = guild_name
options.merge!(:cookie => full_cookie)
options.merge!(:guild_name => name)
else
options.merge!(:cookie => full_cookie)
options.merge!(:guild_name => name)
end
options = merge_defaults(options)
if options[:cookie].nil? || options[:cookie] == ""
raise Wowr::Exceptions::CookieNotSet.new
elsif options[:guild_name].nil? || options[:guild_name] == ""
raise Wowr::Exceptions::GuildNameNotSet.new
elsif options[:realm].nil? || options[:realm] == ""
raise Wowr::Exceptions::RealmNotSet.new
end
options.merge!(:secure => true)
xml = get_xml(@@guild_bank_contents_url, options)
if !(xml%'guildBank').children.empty?
return Wowr::Classes::GuildBankContents.new(xml, self)
else
raise Wowr::Exceptions::GuildBankNotFound.new(options[:guild_name])
end
end
# Get a particular page of the guild bank transaction log.
# Each page contains up to 1000 transactions, other pages can be specified using :group in the options hash.
# Note that data returned is specific to the logged in user's privileges.
# Requires realm.
# * cookie (String) Cookie data returned by the login function
# * guild_name (String) Guild name
# * options (Hash) Optional hash of arguments identical to those used in the API constructor (realm, debug, cache etc.)
def get_guild_bank_log(cookie, name = @guild_name, options = {})
full_cookie = ' JSESSIONID='+cookie
if (cookie.is_a?(Hash))
options = full_cookie
elsif (name.is_a?(Hash))
options = name
options.merge!(:cookie => full_cookie)
options.merge!(:guild_name => name)
else
options.merge!(:cookie => full_cookie)
options.merge!(:guild_name => name)
end
options = merge_defaults(options)
if options[:cookie].nil? || options[:cookie] == ""
raise Wowr::Exceptions::CookieNotSet.new
elsif options[:guild_name].nil? || options[:guild_name] == ""
raise Wowr::Exceptions::GuildNameNotSet.new
elsif options[:realm].nil? || options[:realm] == ""
raise Wowr::Exceptions::RealmNotSet.new
end
options.merge!(:secure => true)
xml = get_xml(@@guild_bank_log_url, options)
if !(xml%'guildBank').children.empty?
return Wowr::Classes::GuildBankLog.new(xml, self)
else
raise Wowr::Exceptions::GuildBankNotFound.new(options[:guild_name])
end
end
def get_complete_world_calendar(cookie, name = @character_name, realm = @realm, options = {})
full_cookie = ' JSESSIONID='+cookie
if (cookie.is_a?(Hash))
options = full_cookie
elsif (name.is_a?(Hash))
options = name
options.merge!(:cookie => full_cookie)
options.merge!(:character_name => @character_name)
options.merge!(:realm => @realm)
elsif (realm.is_a?(Hash))
options = realm
options.merge!(:cookie => full_cookie)
options.merge!(:character_name => name)
options.merge!(:realm => @realm)
else
options.merge!(:cookie => full_cookie)
options.merge!(:character_name => name)
options.merge!(:realm => realm)
end
options = merge_defaults(options)
events = []
@@calendar_world_types.each do |type|
options.merge!(:calendar_type => type)
events = events.concat(get_world_calendar(options))
end
events.sort! { |a,b| a.start <=> b.start }
return events
end
def get_world_calendar(cookie, name = @character_name, realm = @realm, options = {})
full_cookie = ' JSESSIONID='+cookie
if (cookie.is_a?(Hash))
options = full_cookie
elsif (name.is_a?(Hash))
options = name
options.merge!(:cookie => full_cookie)
options.merge!(:character_name => @character_name)
options.merge!(:realm => @realm)
elsif (realm.is_a?(Hash))
options = realm
options.merge!(:cookie => full_cookie)
options.merge!(:character_name => name)
options.merge!(:realm => @realm)
else
options.merge!(:cookie => full_cookie)
options.merge!(:character_name => name)
options.merge!(:realm => realm)
end
options = merge_defaults(options)
if options[:cookie].nil? || options[:cookie] == ""
raise Wowr::Exceptions::CookieNotSet.new
elsif options[:character_name].nil? || options[:guild_name] == ""
raise Wowr::Exceptions::CharacterNameNotSet.new
elsif options[:realm].nil? || options[:realm] == ""
raise Wowr::Exceptions::RealmNotSet.new
end
options.merge!(:secure => true)
json = get_json(@@calendar_world_url, options)
if (!json["events"])
raise Wowr::Exceptions::EmptyPage
end
events = []
json["events"].each do |event|
events << Wowr::Classes::WorldCalendar.new(event, nil)
end
return events
end
def get_full_user_calendar(cookie, name = @character_name, realm = @realm, options = {})
full_cookie = ' JSESSIONID='+cookie
if (cookie.is_a?(Hash))
options = full_cookie
elsif (name.is_a?(Hash))
options = name
options.merge!(:cookie => full_cookie)
options.merge!(:character_name => @character_name)
options.merge!(:realm => @realm)
elsif (realm.is_a?(Hash))
options = realm
options.merge!(:cookie => full_cookie)
options.merge!(:character_name => name)
options.merge!(:realm => @realm)
else
options.merge!(:cookie => full_cookie)
options.merge!(:character_name => name)
options.merge!(:realm => realm)
end
options = merge_defaults(options)
skel_events = get_user_calendar(options)
full_events = []
skel_events.each do |se|
options.merge!(:event => se.id)
full_events << get_calendar_event(options)
end
full_events.sort! { |a,b| a.start <=> b.start }
return full_events
end
def get_user_calendar(cookie, name = @character_name, realm = @realm, options = {})
full_cookie = ' JSESSIONID='+cookie
if (cookie.is_a?(Hash))
options = full_cookie
elsif (name.is_a?(Hash))
options = name
options.merge!(:cookie => full_cookie)
options.merge!(:character_name => @character_name)
options.merge!(:realm => @realm)
elsif (realm.is_a?(Hash))
options = realm
options.merge!(:cookie => full_cookie)
options.merge!(:character_name => name)
options.merge!(:realm => @realm)
else
options.merge!(:cookie => full_cookie)
options.merge!(:character_name => name)
options.merge!(:realm => realm)
end
options = merge_defaults(options)
if options[:cookie].nil? || options[:cookie] == ""
raise Wowr::Exceptions::CookieNotSet.new
elsif options[:character_name].nil? || options[:guild_name] == ""
raise Wowr::Exceptions::CharacterNameNotSet.new
elsif options[:realm].nil? || options[:realm] == ""
raise Wowr::Exceptions::RealmNotSet.new
end
options.merge!(:secure => true)
json = get_json(@@calendar_user_url, options)
if (!json["events"])
raise Wowr::Exceptions::EmptyPage
end
events = []
json["events"].each do |event|
events << Wowr::Classes::UserCalendar.new(event, nil)
end
return events
end
def get_calendar_event (cookie, event = nil, name = @character_name, realm = @realm, options = {})
full_cookie = ' JSESSIONID='+cookie
if (cookie.is_a?(Hash))
options = full_cookie
elsif (event.is_a?(Hash))
options = event
options.merge!(:cookie => full_cookie)
options.merge!(:event => nil)
options.merge!(:character_name => @character_name)
options.merge!(:realm => @realm)
elsif (name.is_a?(Hash))
options = name
options.merge!(:cookie => full_cookie)
options.merge!(:event => event)
options.merge!(:character_name => @character_name)
options.merge!(:realm => @realm)
elsif (realm.is_a?(Hash))
options = realm
options.merge!(:cookie => full_cookie)
options.merge!(:event => event)
options.merge!(:character_name => name)
options.merge!(:realm => @realm)
else
options.merge!(:cookie => full_cookie)
options.merge!(:event => event)
options.merge!(:character_name => name)
options.merge!(:realm => realm)
end
options = merge_defaults(options)
if options[:cookie].nil? || options[:cookie] == ""
raise Wowr::Exceptions::CookieNotSet.new
elsif options[:character_name].nil? || options[:guild_name] == ""
raise Wowr::Exceptions::CharacterNameNotSet.new
elsif options[:realm].nil? || options[:realm] == ""
raise Wowr::Exceptions::RealmNotSet.new
elsif options[:event].nil? || options[:event] == ""
raise Wowr::Exceptions::EventNotSet.new
end
options.merge!(:secure => true)
json = get_json(@@calendar_detail_url, options)
if (!json.is_a?(Hash))
raise Wowr::Exceptions::EmptyPage
end
return Wowr::Classes::UserDetailCalendar.new(json, nil)
end
# Get complete list of dungeons.
# WARNING: This gets two 6k xml files so it's not that fast
# Takes 0.2s with cache, 2s without
# New approach: Instead of passing the XML around and performing multiple
# search lookups to find the elements, run through each XML file once
# adding data to classes as they appear using hash lookup.
# Went from 14s to 2s :)
# * options (Hash) Optional hash of arguments identical to those used in the API constructor (realm, debug, cache etc.)
def get_dungeons(options = {})
options = merge_defaults(options)
# dungeon_strings contains names for ids
dungeon_xml = get_xml(@@dungeons_url, options)%'dungeons'
dungeon_strings_xml = get_xml(@@dungeons_strings_url, options)
results = {}
# TODO: Pass the correct part of dungeon_strings_xml to each dungeon?
if dungeon_xml && !dungeon_xml.children.empty?
(dungeon_xml/:dungeon).each do |elem|
dungeon = Wowr::Classes::Dungeon.new(elem)
results[dungeon.id] = dungeon if dungeon.id
results[dungeon.key] = dungeon if dungeon.key
end
(dungeon_strings_xml/:dungeon).each do |elem|
id = elem[:id].to_i
key = elem[:key]
if (results[id])
results[id].add_name_data(elem)
elsif (results[key])
results[key].add_name_data(elem)
end
end
else
raise Wowr::Exceptions::InvalidXML.new()
end
return results
end
# Logs the user into the armory using their main world of warcraft username, password and authenticator if given/required.
# Uses SSL to send details to the login page. Both must be set to true in order to recieve the long life cookie as the second value.
#
# short = api.login("username", "password")
# short, long = api.login("username", "password", nil, true)
#
def login(username, password, authenticator = nil, both = false)
# Create the base URL we will be POSTing to.
authentication_url = base_url(@locale, {:secure => true, :login => true}) + @@login_url + "?app=armory"
# Ensure we add the correct bounce point.
if (@locale == "www")
authentication_url += "&ref=http://www.wowarmory.com/index.xml"
else
authentication_url += "&ref=http://#{@locale}.wowarmory.com/index.xml"
end
# Ensure we have no final stage.
redirectstage = nil
# Post the first stage
stage1 = login_http(authentication_url, true, nil, { 'accountName' => username, 'password' => password }, true)
# Check what happened.
if (stage1.code == "200")
# We didn't pass, but we didn't fail yet if we need an authenticator.
stage1doc = Hpricot.XML(stage1.body)
# Check to see if our details were incorrect.
if (stage1doc.at("tas:accountName")['error'])
# We have had an error returned to us with regards to our username or password.
raise Wowr::Exceptions::InvalidLoginDetails
end
# Okey do we require an authenticator?
if (!stage1doc.at("tas:authType")['value'] || stage1doc.at("tas:authType")['value'] != "BA")
# Ummm, we're not invalid nor do we have no clue about the authType required now.
raise Wowr::Exceptions::LoginBroken
end
# Do we have an authenticator code to use?
raise Wowr::Exceptions::LoginRequiresAuthenticator if (!authenticator)
stage1cookie = nil
# Get the *authentication sites* JSESSIONID.
stage1.header['set-cookie'].scan(/JSESSIONID=(.*?);/) {
stage1cookie = $1
}
# Let's post the authenticator and the session for this login.
stage2 = login_http(authentication_url, true, { 'JSESSIONID' => stage1cookie }, { 'authValue' => authenticator }, true)
# So now we check what happened.
if (stage2.code == "200")
# This isn't a good sign, we should have redirected now.
stage2doc = Hpricot.XML(stage2.body)
# Error is obvious
if (stage2doc.at("tas:accountName")['error'])
# We have had an error returned to us with regards to our username or password.
raise Wowr::Exceptions::InvalidLoginDetails
end
# Error isn't obvious, we can't continue.
raise Wowr::Exceptions::LoginBroken
elsif (stage2.code == "302")
redirectstage = stage2
end
elsif (stage1.code == "302")
redirectstage = stage1
end
# We should have been redirected by now.
if (!redirectstage)
raise Wowr::Exceptions::LoginBroken
end
# Time to obtain our next URL and our long term cookie.
long_cookie = nil
redirectstage.header['set-cookie'].scan(/#{@@persistant_cookie}=(.*?);/) {
long_cookie = $1
}
# Let's bounce to our page that will give us our short term cookie, URL has Kerbrose style ticket.
short_cookie = login_final_bounce(redirectstage.header['location'])
# So what does the user want?
if (both)
return short_cookie, long_cookie
else
return short_cookie
end
end
# Reobtains a short term cookie by using the given long life cookie.
def refresh_login(long_life_cookie)
# Create the base URL we will be POSTing to.
authentication_url = base_url(@locale, {:secure => true, :login => true}) + @@login_url + "?app=armory"
# Ensure we add the correct bounce point.
if (@locale == "www")
authentication_url += "&ref=http://www.wowarmory.com/index.xml"
else
authentication_url += "&ref=http://#{@locale}.wowarmory.com/index.xml"
end
# All we need to do is goto the armory login page passing our long life cookie, we should get 302 instantly.
stage1 = login_http(authentication_url, true, { @@persistant_cookie => long_life_cookie })
# Let's see
if (stage1.code == "200")
# It's no good, our cookie doesn't work anymore.
raise Wowr::Exceptions::InvalidLoginDetails
elsif (stage1.code == "302")
# Let's bounce to our page that will give us our short term cookie, URL has Kerbrose style ticket.
return login_final_bounce(stage1.header['location'])
end
# Finally we didn't get 302 or 200?
raise Wowr::Exceptions::LoginBroken
end
# Clear the cache, optional directory name.
# * cache_path (String) Relative path of the cache directory to be deleted
def clear_cache(cache_path = @@cache_directory_path)
begin
FileUtils.remove_dir(cache_path)
rescue Exception => e
end
end
# Return the base url for the armory, e.g. http://eu.wowarmory.com/
# * locale (String) The locale, defaults to that specified in the API constructor
def base_url(locale = @locale, options = {})
str = ""
if (options[:secure] == true)
str += 'https://'
else
str += 'http://'
end
if (locale == 'us')
str += 'www.'
else
str += locale + "."
end
if (options[:login] == true)
str += @@login_base_url
else
str += @@armory_base_url
end
return str
end
protected
# Merge the defaults specified in the constructor with those supplied,
# overriding any defaults with those supplied
def merge_defaults(options = {})
defaults = {}
# defaults[:character_name] = @charater_name if @charater_name
# defaults[:guild_name] = @guild_name if @guild_name
defaults[:realm] = @realm if @realm
defaults[:locale] = @locale if @locale
defaults[:lang] = @lang if @lang
defaults[:caching] = @caching if @caching
defaults[:cache_timeout] = @cache_timeout if @cache_timeout
defaults[:debug] = @debug if @debug
# overwrite defaults with any given options
defaults.merge!(options)
end
# Returns an option array from character_name and defaults
def character_options(name, options = {})
if (name.is_a?(Hash))