File Coverage

blib/lib/WWW/BackpackTF.pm
Criterion Covered Total %
statement 56 66 84.8
branch 4 8 50.0
condition 1 2 50.0
subroutine 15 17 88.2
pod 4 5 80.0
total 80 98 81.6


line stmt bran cond sub pod time code
1             package WWW::BackpackTF;
2              
3 2     2   45165 use 5.014000;
  2         7  
4 2     2   6 use strict;
  2         2  
  2         36  
5 2     2   6 use warnings;
  2         8  
  2         50  
6 2     2   729 use parent qw/Exporter/;
  2         507  
  2         7  
7             our $VERSION = '0.001001';
8             our @EXPORT_OK = qw/TF2 DOTA2/;
9              
10             use constant +{ ## no critic (Capitalization)
11 2         950 TF2 => 440,
12             DOTA2 => 570,
13             CSGO => 730,
14             QUALITIES => [qw/Normal Genuine rarity2 Vintage rarity3 Unusual Unique Community Valve Self-Made Customized Strange Completed Haunted Collector's/],
15 2     2   141 };
  2         2  
16              
17             BEGIN {
18 2     2   3 my @qualities = @{QUALITIES()};
  2         6  
19 2         6 for (0 .. $#qualities) {
20 30         32 my $name = uc $qualities[$_];
21 30         24 $name =~ y/A-Z0-9//cd;
22 30         470 constant->import($name, $_)
23             }
24             }
25              
26 2     2   744 use JSON::MaybeXS qw/decode_json/;
  2         11449  
  2         92  
27 2     2   1209 use HTTP::Tiny;
  2         67244  
  2         62  
28 2     2   817 use PerlX::Maybe;
  2         2505  
  2         77  
29 2     2   655 use WWW::BackpackTF::Currency;
  2         2  
  2         40  
30 2     2   603 use WWW::BackpackTF::Item;
  2         4  
  2         39  
31 2     2   645 use WWW::BackpackTF::User;
  2         3  
  2         696  
32              
33             my $ht = HTTP::Tiny->new(agent => "WWW-BackpackTF/$VERSION");
34              
35             sub request {
36 1     1 0 2 my ($self, $url, %params) = @_;
37 1 50       6 $params{key} = $self->{key} if $self->{key};
38 1         3 $url = $self->{base} . $url;
39 1         5 $url .= "&$_=$params{$_}" for keys %params;
40 1         27 my $htr = $ht->get($url);
41 1 50       359373 die $htr->{reason} unless $htr->{success}; ## no critic (RequireCarping)
42 1         53 my $response = decode_json($htr->{content})->{response};
43 1 50       6 die $response->{message} unless $response->{success}; ## no critic (RequireCarping)
44 1         10 $response
45             }
46              
47             sub new{
48 1     1 1 10 my ($class, %args) = @_;
49 1   50     6 $args{base} //= 'http://backpack.tf/api/';
50 1         3 bless \%args, $class
51             }
52              
53             sub get_prices {
54 0     0 1 0 my ($self, $appid, $raw) = @_;
55 0         0 my $response = $self->request('IGetPrices/v4/?compress=1', maybe appid => $appid, maybe raw => $raw);
56 0         0 map { WWW::BackpackTF::Item->new($_, $response->{items}{$_}) } keys %{$response->{items}}
  0         0  
  0         0  
57             }
58              
59             sub get_users {
60 1     1 1 5 my ($self, @users) = @_;
61 1         6 my $response = $self->request('IGetUsers/v3/?compress=1', steamids => join ',', @users);
62 1         3 @users = map { WWW::BackpackTF::User->new($_) } values %{$response->{players}};
  1         15  
  1         6  
63 1 50       10 wantarray ? @users : $users[0]
64             }
65              
66             sub get_currencies {
67 0     0 1   my ($self, $appid) = @_;
68 0           my $response = $self->request('IGetCurrencies/v1/?compress=1', maybe appid => $appid);
69 0           map { WWW::BackpackTF::Currency->new($_, $response->{currencies}{$_}) } keys %{$response->{currencies}};
  0            
  0            
70             }
71              
72             1;
73             __END__