File Coverage

blib/lib/WoW/Armory/API.pm
Criterion Covered Total %
statement 66 92 71.7
branch 4 18 22.2
condition 1 3 33.3
subroutine 19 24 79.1
pod 7 7 100.0
total 97 144 67.3


line stmt bran cond sub pod time code
1             package WoW::Armory::API;
2              
3 2     2   76613 use strict;
  2         6  
  2         82  
4 2     2   12 use warnings;
  2         4  
  2         88  
5              
6             our $VERSION = '1.031';
7              
8 2     2   10 use base 'Exporter';
  2         9  
  2         317  
9              
10             our @EXPORT = qw(
11             WOW_CHARACTER_FIELDS WOW_GUILD_FIELDS
12             WOW_ARENA_TEAM_2 WOW_ARENA_TEAM_3 WOW_ARENA_TEAM_5
13             );
14              
15 2     2   1972 use URI::Escape;
  2         5406  
  2         176  
16 2     2   2730 use LWP::UserAgent;
  2         149342  
  2         126  
17 2     2   3991 use JSON::XS;
  2         17937  
  2         262  
18              
19 2         381 use constant WOW_REGIONS =>
20             {
21             us => { api_host => 'us.battle.net' , locales => [qw(en_US es_MX pt_BR)] },
22             eu => { api_host => 'eu.battle.net' , locales => [qw(en_GB es_ES fr_FR ru_RU de_DE pt_PT it_IT)] },
23             kr => { api_host => 'kr.battle.net' , locales => [qw(ko_KR)] },
24             tw => { api_host => 'tw.battle.net' , locales => [qw(zh_TW)] },
25             cn => { api_host => 'www.battlenet.com.cn', locales => [qw(zh_CN)] },
26 2     2   20 };
  2         4  
27              
28 2         162 use constant WOW_API_METHODS =>
29             [
30             ['achievement' , 1],
31             ['auction/data' , 1],
32             ['battlePet/ability' , 1],
33             ['battlePet/species' , 1],
34             ['battlePet/stats' , 1, [qw(level breedId qualityId)]],
35             ['challenge' , 1],
36             ['challenge/region' , 0],
37             ['character' , 2, [qw(fields)]],
38             ['item' , 1],
39             ['item/set' , 1],
40             ['guild' , 2, [qw(fields)]],
41             ['arena' , 3],
42             ['pvp/arena' , 2, [qw(page size asc)]],
43             ['pvp/ratedbg/ladder' , 0, [qw(page size asc)]],
44             ['quest' , 1],
45             ['realm/status' , 0],
46             ['recipe' , 1],
47             ['spell' , 1],
48             ['data/battlegroups' , 0],
49             ['data/character/races' , 0],
50             ['data/character/classes' , 0],
51             ['data/character/achievements' , 0],
52             ['data/guild/rewards' , 0],
53             ['data/guild/perks' , 0],
54             ['data/guild/achievements' , 0],
55             ['data/item/classes' , 0],
56             ['data/talents' , 0],
57             ['data/pet/types' , 0],
58 2     2   12 ];
  2         3  
59              
60 2         208 use constant WOW_CHARACTER_FIELDS => [qw(achievements appearance feed guild hunterPets items mounts pets petSlots
61 2     2   10 professions progression pvp quests reputation stats talents titles)];
  2         4  
62              
63 2     2   19 use constant WOW_GUILD_FIELDS => [qw(members achievements news challenge)];
  2         3  
  2         93  
64              
65 2     2   9 use constant WOW_ARENA_TEAM_2 => '2v2';
  2         2  
  2         73  
66 2     2   47 use constant WOW_ARENA_TEAM_3 => '3v3';
  2         4  
  2         106  
67 2     2   11 use constant WOW_ARENA_TEAM_5 => '5v5';
  2         3  
  2         175  
68              
69             sub _init_package {
70 2     2   4 my $class = shift;
71              
72 2         3 for my $def (@{WOW_API_METHODS()}) {
  2         5  
73 2     2   20 no strict 'refs';
  2         4  
  2         1914  
74 56         353 *{$class.'::Get'.join('', map { ucfirst $_ } split('/', $def->[0]))} = sub {
  112         517  
75             return shift->DoApiCall(
76 0         0 join('/', $def->[0], splice(@_, 0, $def->[1])),
77 0 0   0   0 map { my $t = shift; defined $t ? ($_ => $t) : () } @{$def->[2]||[]},
  0 0       0  
  0         0  
78             );
79 56         205 };
80             }
81             }
82              
83             sub new {
84 1     1 1 16 my $proto = shift;
85              
86 1   33     9 my $class = ref $proto || $proto;
87 1         4 my $self = bless {}, $class;
88              
89 1         6 return $self->_init(@_);
90             }
91              
92             sub _init {
93 1     1   3 my ($self, %opts) = @_;
94              
95 1         12 $self->{ua} = LWP::UserAgent->new;
96              
97 1         3921 $self->SetRegion('us');
98              
99 1 50       4 $self->SetRegion($opts{Region}) if exists $opts{Region};
100 1 50       3 $self->SetLocale($opts{Locale}) if exists $opts{Locale};
101              
102 1         11 return $self;
103             }
104              
105             sub GetRegions {
106 0         0 return {regions => [
107 0     0 1 0 map {{region => $_, %{WOW_REGIONS()->{$_}}}} keys %{WOW_REGIONS()}
  0         0  
  0         0  
108             ]};
109             }
110              
111             sub SetRegion {
112 1     1 1 4 my ($self, $region, $locale) = @_;
113 1 50       8 return if !WOW_REGIONS()->{$region};
114 1         3 $self->{region} = $region;
115 1         4 $self->SetLocale($locale);
116             }
117              
118             sub HasLocale {
119 0     0 1 0 my ($self, $locale) = @_;
120 0         0 return !!grep {$_ eq $locale}
  0         0  
121 0         0 @{WOW_REGIONS()->{$self->{region}}{locales}}
122             }
123              
124             sub SetLocale {
125 1     1 1 2 my ($self, $locale) = @_;
126 1 50       4 if (!defined $locale) {
127 1         4 $self->{locale} = undef;
128 1         3 return;
129             }
130 0 0         return if !$self->HasLocale($locale);
131 0           $self->{locale} = $locale;
132             }
133              
134             sub GetApiHost {
135 0     0 1   my ($self) = @_;
136 0           return WOW_REGIONS()->{$self->{region}}{api_host};
137             }
138              
139             sub DoApiCall {
140 0     0 1   my ($self, $method, @opts) = @_;
141              
142 0 0         push @opts, (locale => $self->{locale}) if $self->{locale};
143 0           push @opts, (rand => rand);
144              
145 0           my $query = join('&', map {
146 0           uri_escape_utf8(shift @opts).'='.uri_escape_utf8(shift @opts)
147             } (1..scalar(@opts)/2));
148              
149 0           my $url = 'http://'.$self->GetApiHost."/api/wow/${method}?${query}";
150 0           my $res = $self->{ua}->get($url);
151              
152             #return undef if !$res->is_success;
153              
154 0           my $data = eval { decode_json $res->decoded_content };
  0            
155              
156 0 0         return $@ ? undef : $data;
157             }
158              
159             __PACKAGE__->_init_package;
160              
161             1;
162              
163             =head1 NAME
164              
165             WoW::Armory::API - Perl interface to WoW API
166              
167             =head1 SYNOPSIS
168              
169             use WoW::Armory::API;
170              
171             $api = WoW::Armory::API->new(Region => 'eu', Locale => 'ru_RU');
172              
173             $char_data = $api->GetCharacter('realm', 'Character', 'items,pets,mounts');
174             $guild_data = $api->GetGuild('realm', 'Guild');
175              
176             print $char_data->{items}{head}{name};
177             print $guild_data->{name};
178              
179             use WoW::Armory::Class::Character;
180             use WoW::Armory::Class::Guild;
181              
182             $char = WoW::Armory::Class::Character->new($char_data);
183             $guild = WoW::Armory::Class::Guild->new($guild_data);
184              
185             print $char->items->head->name;
186             print $guild->name;
187              
188             =head1 METHODS
189              
190             =head2 Constants
191              
192             =head3 WOW_CHARACTER_FIELDS
193              
194             @fields = @{WOW_CHARACTER_FIELDS()};
195             $data = $api->GetCharacter($realmId, $characterName, join(',', @fields));
196              
197             =head3 WOW_GUILD_FIELDS
198              
199             @fields = @{WOW_GUILD_FIELDS()};
200             $data = $api->GetGuild($realmId, $guildName, join(',', @fields));
201              
202             =head3 WOW_ARENA_TEAM_2
203              
204             $data = $api->GetArena($realmId, WOW_ARENA_TEAM_2, $teamName);
205              
206             =head3 WOW_ARENA_TEAM_3
207              
208             $data = $api->GetArena($realmId, WOW_ARENA_TEAM_3, $teamName);
209              
210             =head3 WOW_ARENA_TEAM_5
211              
212             $data = $api->GetArena($realmId, WOW_ARENA_TEAM_5, $teamName);
213              
214             =head2 Constructor
215              
216             =head3 new()
217              
218             $api = WoW::Armory::API->new;
219             $api = WoW::Armory::API->new(Region => $regionId, Locale => $locale);
220              
221             =head2 General
222              
223             =head3 GetRegions()
224              
225             $data = WoW::Armory::API->GetRegions();
226             $data = $api->GetRegions();
227              
228             =head3 SetRegion()
229              
230             $api->SetRegion($regionId);
231             $api->SetRegion($regionId, $locale);
232              
233             =head3 HasLocale()
234              
235             $hasLocale = $api->HasLocale($locale);
236              
237             =head3 SetLocale()
238              
239             $api->SetLocale($locale);
240              
241             =head3 GetApiHost()
242              
243             $host = $api->GetApiHost();
244              
245             =head3 DoApiCall()
246              
247             $data = $api->DoApiCall($method, @params);
248              
249             =head2 WoW API
250              
251             All of these methods return the appropriate data structure or undef.
252             See L for more details.
253              
254             =head3 GetAchievement()
255              
256             $data = $api->GetAchievement($achievementId);
257              
258             =head3 GetAuctionData()
259              
260             $data = $api->GetAuctionData($realmId);
261              
262             =head3 GetBattlePetAbility()
263              
264             $data = $api->GetBattlePetAbility($abilityId);
265              
266             =head3 GetBattlePetSpecies()
267              
268             $data = $api->GetBattlePetSpecies($speciesId);
269              
270             =head3 GetBattlePetStats()
271              
272             $data = $api->GetBattlePetStats($speciesId);
273             $data = $api->GetBattlePetStats($speciesId, $level, $breedId, $qualityId);
274              
275             =head3 GetChallenge()
276              
277             $data = $api->GetChallenge($realmId);
278              
279             =head3 GetChallengeRegion()
280              
281             $data = $api->GetChallengeRegion();
282              
283             =head3 GetCharacter()
284              
285             $data = $api->GetCharacter($realmId, $characterName);
286             $data = $api->GetCharacter($realmId, $characterName, $fields);
287              
288             =head3 GetItem()
289              
290             $data = $api->GetItem($itemId);
291              
292             =head3 GetItemSet()
293              
294             $data = $api->GetItemSet($itemSetId);
295              
296             =head3 GetGuild()
297              
298             $data = $api->GetGuild($realmId, $guildName);
299             $data = $api->GetGuild($realmId, $guildName, $fields);
300              
301             =head3 GetArena()
302              
303             $data = $api->GetArena($realmId, $teamSize, $teamName);
304              
305             =head3 GetPvpArena()
306              
307             $data = $api->GetPvpArena($battleGroup, $teamSize);
308             $data = $api->GetPvpArena($battleGroup, $teamSize, $page, $pageSize, $asc);
309              
310             =head3 GetPvpRatedbgLadder()
311              
312             $data = $api->GetPvpRatedbgLadder();
313             $data = $api->GetPvpRatedbgLadder($page, $pageSize, $asc);
314              
315             =head3 GetQuest()
316              
317             $data = $api->GetQuest($questId);
318              
319             =head3 GetRealmStatus()
320              
321             $data = $api->GetRealmStatus();
322              
323             =head3 GetRecipe()
324              
325             $data = $api->GetRecipe($recipeId);
326              
327             =head3 GetSpell()
328              
329             $data = $api->GetSpell($spellId);
330              
331             =head3 GetDataBattlegroups()
332              
333             $data = $api->GetDataBattlegroups();
334              
335             =head3 GetDataCharacterRaces()
336              
337             $data = $api->GetDataCharacterRaces();
338              
339             =head3 GetDataCharacterClasses()
340              
341             $data = $api->GetDataCharacterClasses();
342              
343             =head3 GetDataCharacterAchievements()
344              
345             $data = $api->GetDataCharacterAchievements();
346              
347             =head3 GetDataGuildRewards()
348              
349             $data = $api->GetDataGuildRewards();
350              
351             =head3 GetDataGuildPerks()
352              
353             $data = $api->GetDataGuildPerks();
354              
355             =head3 GetDataGuildAchievements()
356              
357             $data = $api->GetDataGuildAchievements();
358              
359             =head3 GetDataItemClasses()
360              
361             $data = $api->GetDataItemClasses();
362              
363             =head3 GetDataTalents()
364              
365             $data = $api->GetDataTalents();
366              
367             =head3 GetDataPetTypes()
368              
369             $data = $api->GetDataPetTypes();
370              
371             =head1 SEE ALSO
372              
373             L
374              
375             =head1 REPOSITORY
376              
377             The source code for the WoW::Armory::API is held in a public git repository
378             on Github: L
379              
380             =head1 AUTHOR
381              
382             Aleksandr Aleshin Fsilencer@cpan.orgE>
383              
384             =head1 COPYRIGHT
385              
386             This software is copyright (c) 2012 by Aleksandr Aleshin.
387              
388             This is free software; you can redistribute it and/or modify it under
389             the same terms as the Perl 5 programming language system itself.
390              
391             =cut