If the key is not provided, do not send an empty key
[www-backpacktf.git] / lib / WWW / BackpackTF.pm
CommitLineData
6e8c48c3
MG
1package WWW::BackpackTF;
2
3use 5.014000;
4use strict;
5use warnings;
6use parent qw/Exporter/;
de56dfb1 7our $VERSION = '0.000_002';
6e8c48c3
MG
8our @EXPORT_OK = qw/TF2 DOTA2/;
9
011cd8b5
MG
10use 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
16BEGIN {
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}
6e8c48c3 24
1e12c1b3 25use JSON::MaybeXS qw/decode_json/;
6e8c48c3 26use LWP::Simple qw/get/;
011cd8b5
MG
27use PerlX::Maybe;
28use WWW::BackpackTF::Currency;
29use WWW::BackpackTF::Item;
6e8c48c3
MG
30use WWW::BackpackTF::User;
31
011cd8b5
MG
32sub request {
33 my ($self, $url, %params) = @_;
af9ae5ee 34 $params{key} = $self->{key} if $self->{key};
011cd8b5
MG
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
6e8c48c3 42sub new{
011cd8b5
MG
43 my ($class, %args) = @_;
44 $args{base} //= 'http://backpack.tf/api/';
45 bless \%args, $class
46}
47
48sub get_prices {
49 my ($self, $appid, $raw) = @_;
50 my $response = $self->request('IGetPrices/v4/?compress=1', maybe appid => $appid, maybe raw => $raw);
72f329fe 51 map { WWW::BackpackTF::Item->new($_, $response->{items}{$_}) } keys %{$response->{items}}
6e8c48c3
MG
52}
53
011cd8b5 54sub get_users {
6e8c48c3 55 my ($self, @users) = @_;
011cd8b5 56 my $response = $self->request('IGetUsers/v3/?compress=1', steamids => join ',', @users);
72f329fe 57 @users = map { WWW::BackpackTF::User->new($_) } values %{$response->{players}};
6e8c48c3
MG
58 wantarray ? @users : $users[0]
59}
60
011cd8b5
MG
61sub get_currencies {
62 my ($self, $appid) = @_;
63 my $response = $self->request('IGetCurrencies/v1/?compress=1', maybe appid => $appid);
72f329fe 64 map { WWW::BackpackTF::Currency->new($_, $response->{currencies}{$_}) } keys %{$response->{currencies}};
011cd8b5
MG
65}
66
6e8c48c3
MG
671;
68__END__
69
70=head1 NAME
71
72WWW::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)';
011cd8b5
MG
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;
6e8c48c3
MG
85
86=head1 DESCRIPTION
87
88WWW::BackpackTF is an interface to the backpack.tf Team Fortress 2/Dota 2 trading service.
89
6e8c48c3
MG
90=head2 METHODS
91
92=over
93
1115e95d 94=item B<new>([key => I<$api_key>], [base => I<$base_url>])
6e8c48c3 95
1115e95d
MG
96Create a new WWW::BackpackTF object. Takes a hash of parameters. Possible parameters:
97
98=over
99
100=item B<key>
101
102The API key. Defaults to nothing. Most methods require an API key.
103
104=item B<base>
105
106The base URL. Defaults to http://backpack.tf/api/.
107
108=back
6e8c48c3 109
011cd8b5
MG
110=item B<get_prices>([I<$appid>, [I<$raw>]])
111
112Get 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
6e8c48c3
MG
114=item B<get_users>(I<@users>)
115
011cd8b5
MG
116Get 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
120Get currency information. Takes one optional parameter, the appid, which defaults to WWW::BackpackTF::TF2. Returns a list of L<WWW::BackpackTF::Currency> objects.
6e8c48c3
MG
121
122=back
123
124=head2 EXPORTS
125
126None by default.
127
128=over
129
130=item B<TF2>
131
132Constant (440) representing Team Fortress 2.
133
134=item B<DOTA2>
135
136Constant (570) representing Dota 2.
137
011cd8b5
MG
138=item B<NORMAL>
139
140The Normal item quality (0).
141
142=item B<GENUINE>
143
144The Genuine item quality (1).
145
146=item B<RARITY2>
147
148The unused rarity2 item quality (2).
149
150=item B<VINTAGE>
151
152The Vintage item quality (3).
153
154=item B<RARITY3>
155
156The unused rarity3 item quality (4).
157
158=item B<UNUSUAL>
159
160The Unusual item quality (5).
161
162=item B<UNIQUE>
163
164The Unique item quality (6).
165
166=item B<COMMUNITY>
167
168The Community item quality (7).
169
170=item B<VALVE>
171
172The Valve item quality (8).
173
174=item B<SELFMADE>
175
176The Self-Made item quality (9).
177
178=item B<CUSTOMIZED>
179
180The unused Customized item quality (10).
181
182=item B<STRANGE>
183
184The Strange item quality (11).
185
186=item B<COMPLETED>
187
188The Completed item quality (12).
189
190=item B<HAUNTED>
191
192The Haunted item quality (13).
193
194=item B<COLLECTORS>
195
196The Collector's item quality (14).
197
6e8c48c3
MG
198=back
199
200=head1 SEE ALSO
201
202L<http://backpack.tf/>, L<http://backpack.tf/api>
203
204=head1 AUTHOR
205
206Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
207
208=head1 COPYRIGHT AND LICENSE
209
210Copyright (C) 2014 by Marius Gavrilescu
211
212This library is free software; you can redistribute it and/or modify
213it under the same terms as Perl itself, either Perl version 5.18.2 or,
214at your option, any later version of Perl 5 you may have available.
215
216
217=cut
This page took 0.022558 seconds and 4 git commands to generate.