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