Bump version and update Changes
[www-backpacktf.git] / lib / WWW / BackpackTF.pm
1 package WWW::BackpackTF;
2
3 use 5.014000;
4 use strict;
5 use warnings;
6 use parent qw/Exporter/;
7 our $VERSION = '0.000_003';
8 our @EXPORT_OK = qw/TF2 DOTA2/;
9
10 use constant +{
11 TF2 => 440,
12 DOTA2 => 570,
13 QUALITIES => [qw/Normal Genuine rarity2 Vintage rarity3 Unusual Unique Community Valve Self-Made Customized Strange Completed Haunted Collector's/],
14 };
15
16 BEGIN {
17 my @qualities = @{QUALITIES()};
18 for (0 .. $#qualities) {
19 my $name = uc $qualities[$_];
20 $name =~ y/A-Z0-9//cd;
21 constant->import($name, $_)
22 }
23 }
24
25 use JSON::MaybeXS qw/decode_json/;
26 use LWP::Simple qw/get/;
27 use PerlX::Maybe;
28 use WWW::BackpackTF::Currency;
29 use WWW::BackpackTF::Item;
30 use WWW::BackpackTF::User;
31
32 sub request {
33 my ($self, $url, %params) = @_;
34 $params{key} = $self->{key} if $self->{key};
35 $url = $self->{base} . $url;
36 $url .= "&$_=$params{$_}" for keys %params;
37 my $response = decode_json(get $url)->{response};
38 die $response->{message} unless $response->{success};
39 $response
40 }
41
42 sub new{
43 my ($class, %args) = @_;
44 $args{base} //= 'http://backpack.tf/api/';
45 bless \%args, $class
46 }
47
48 sub get_prices {
49 my ($self, $appid, $raw) = @_;
50 my $response = $self->request('IGetPrices/v4/?compress=1', maybe appid => $appid, maybe raw => $raw);
51 map { WWW::BackpackTF::Item->new($_, $response->{items}{$_}) } keys %{$response->{items}}
52 }
53
54 sub get_users {
55 my ($self, @users) = @_;
56 my $response = $self->request('IGetUsers/v3/?compress=1', steamids => join ',', @users);
57 @users = map { WWW::BackpackTF::User->new($_) } values %{$response->{players}};
58 wantarray ? @users : $users[0]
59 }
60
61 sub get_currencies {
62 my ($self, $appid) = @_;
63 my $response = $self->request('IGetCurrencies/v1/?compress=1', maybe appid => $appid);
64 map { WWW::BackpackTF::Currency->new($_, $response->{currencies}{$_}) } keys %{$response->{currencies}};
65 }
66
67 1;
68 __END__
69
70 =head1 NAME
71
72 WWW::BackpackTF - interface to the backpack.tf trading service
73
74 =head1 SYNOPSIS
75
76 use WWW::BackpackTF;
77 my $api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
78 my $user_id = <STDIN>;
79 my $bp = WWW::BackpackTF->new($api_key);
80 my $user = $bp->get_users($user_id);
81 print 'This user is named ', $user->name, ' and has ', $user->notifications, ' unread notification(s)';
82 my @all_items_in_dota2 = $bp->get_prices(WWW::BackpackTF::DOTA2);
83 my @currencies = $bp->get_currencies;
84 print 'The first currency is ', $currencies[0]->name;
85
86 =head1 DESCRIPTION
87
88 WWW::BackpackTF is an interface to the backpack.tf Team Fortress 2/Dota 2 trading service.
89
90 =head2 METHODS
91
92 =over
93
94 =item B<new>([key => I<$api_key>], [base => I<$base_url>])
95
96 Create a new WWW::BackpackTF object. Takes a hash of parameters. Possible parameters:
97
98 =over
99
100 =item B<key>
101
102 The API key. Defaults to nothing. Most methods require an API key.
103
104 =item B<base>
105
106 The base URL. Defaults to http://backpack.tf/api/.
107
108 =back
109
110 =item B<get_prices>([I<$appid>, [I<$raw>]])
111
112 Get price information for all items. Takes two optional parameters. The first parameter is the appid and defaults to WWW::BackpackTF::TF2. The second (if true) adds a value_raw property to prices and defaults to false. Returns a list of L<WWW::BackpackTF::Item> objects.
113
114 =item B<get_users>(I<@users>)
115
116 Get profile information for a list of users. Takes any number of 64-bit Steam IDs as arguments and returns a list of L<WWW::BackpackTF::User> objects. This method does not require an API key. Dies with an error message if the operation is unsuccessful.
117
118 =item B<get_currencies>([I<$appid>])
119
120 Get currency information. Takes one optional parameter, the appid, which defaults to WWW::BackpackTF::TF2. Returns a list of L<WWW::BackpackTF::Currency> objects.
121
122 =back
123
124 =head2 EXPORTS
125
126 None by default.
127
128 =over
129
130 =item B<TF2>
131
132 Constant (440) representing Team Fortress 2.
133
134 =item B<DOTA2>
135
136 Constant (570) representing Dota 2.
137
138 =item B<NORMAL>
139
140 The Normal item quality (0).
141
142 =item B<GENUINE>
143
144 The Genuine item quality (1).
145
146 =item B<RARITY2>
147
148 The unused rarity2 item quality (2).
149
150 =item B<VINTAGE>
151
152 The Vintage item quality (3).
153
154 =item B<RARITY3>
155
156 The unused rarity3 item quality (4).
157
158 =item B<UNUSUAL>
159
160 The Unusual item quality (5).
161
162 =item B<UNIQUE>
163
164 The Unique item quality (6).
165
166 =item B<COMMUNITY>
167
168 The Community item quality (7).
169
170 =item B<VALVE>
171
172 The Valve item quality (8).
173
174 =item B<SELFMADE>
175
176 The Self-Made item quality (9).
177
178 =item B<CUSTOMIZED>
179
180 The unused Customized item quality (10).
181
182 =item B<STRANGE>
183
184 The Strange item quality (11).
185
186 =item B<COMPLETED>
187
188 The Completed item quality (12).
189
190 =item B<HAUNTED>
191
192 The Haunted item quality (13).
193
194 =item B<COLLECTORS>
195
196 The Collector's item quality (14).
197
198 =back
199
200 =head1 SEE ALSO
201
202 L<http://backpack.tf/>, L<http://backpack.tf/api>
203
204 =head1 AUTHOR
205
206 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
207
208 =head1 COPYRIGHT AND LICENSE
209
210 Copyright (C) 2014 by Marius Gavrilescu
211
212 This library is free software; you can redistribute it and/or modify
213 it under the same terms as Perl itself, either Perl version 5.18.2 or,
214 at your option, any later version of Perl 5 you may have available.
215
216
217 =cut
This page took 0.031171 seconds and 4 git commands to generate.