File Coverage

blib/lib/WWW/Freelancer.pm
Criterion Covered Total %
statement 26 56 46.4
branch 0 6 0.0
condition n/a
subroutine 9 13 69.2
pod 4 4 100.0
total 39 79 49.3


line stmt bran cond sub pod time code
1             package WWW::Freelancer;
2              
3 2     2   58574 use warnings;
  2         6  
  2         88  
4 2     2   16 use strict;
  2         6  
  2         75  
5              
6 2     2   1889 use version; our $VERSION = qv('0.0.1');
  2         5404  
  2         15  
7              
8 2     2   2553 use LWP::UserAgent;
  2         169234  
  2         95  
9 2     2   2665 use JSON qw( from_json );
  2         99581  
  2         17  
10 2     2   3166 use WWW::Freelancer::Project;
  2         7  
  2         298  
11 2     2   3349 use WWW::Freelancer::User;
  2         7  
  2         3524  
12              
13             my $user_agent = LWP::UserAgent->new( 'agent' => __PACKAGE__ . '/' . $VERSION );
14              
15             sub new {
16 1     1 1 1081 my $class = shift;
17 1         6 return bless {}, __PACKAGE__;
18             }
19              
20             sub get_project {
21 1     1 1 1590 my ( $self, $id ) = @_;
22              
23 1         12 my $content = $user_agent->get("http://api.freelancer.com/Project/$id.json")
24             ->decoded_content();
25              
26 1         3418337 return bless from_json($content)->{'project'}, 'WWW::Freelancer::Project';
27             }
28              
29             sub get_user {
30 0     0 1   my ( $self, $id_or_username ) = @_;
31              
32 0           my $query_string = "?id=$id_or_username";
33 0           my $content
34             = $user_agent->get(
35             "http://api.freelancer.com/User/Properties.json$query_string")
36             ->decoded_content();
37              
38 0           return bless from_json($content)->{'profile'}, 'WWW::Freelancer::User';
39             }
40              
41             sub search_project {
42 0     0 1   my ( $self, %parametres ) = @_;
43              
44 0           _move_key_to_api_key(
45             \%parametres,
46             { 'jobs' => 'jobs[]',
47             'maximum_budget' => 'max_budget',
48             'minimum_budget' => 'min_budget',
49             'order_direction' => 'order_dir',
50             }
51             );
52              
53 0 0         if ( defined $parametres{'order'}->{'random'} ) {
54 0           $parametres{'order'}->{'rand'} = $parametres{'order'}->{'random'};
55 0           delete $parametres{'order'}->{'random'};
56             }
57              
58 0           my $query_string = _build_query_string(%parametres);
59 0           my $content
60             = $user_agent->get(
61             "http://api.freelancer.com/Project/Search.json$query_string")
62             ->decoded_content();
63 0           my @projects = @{ from_json($content)->{'projects'}->{'items'} };
  0            
64 0           map { bless $_, 'WWW::Freelancer::Project' } @projects;
  0            
65              
66 0           return @projects;
67             }
68              
69             sub _move_key_to_api_key {
70 0     0     my ( $parametres_ref, $keys_and_api_keys_ref ) = @_;
71              
72 0           for my $key ( keys %{$keys_and_api_keys_ref} ) {
  0            
73 0 0         if ( defined $parametres_ref->{$key} ) {
74 0           $parametres_ref->{ $keys_and_api_keys_ref->{$key} }
75             = $parametres_ref->{$key};
76 0           delete $parametres_ref->{$key};
77             }
78             }
79             }
80              
81             sub _build_query_string {
82 0     0     my %parametres = @_;
83 0           my @query_strings;
84              
85 0           for my $key ( keys %parametres ) {
86 0 0         if ( ref $parametres{$key} eq 'ARRAY' ) {
87 0           for my $element ( @{ $parametres{$key} } ) {
  0            
88 0           push @query_strings, $key . '=' . $element;
89             }
90             }
91             }
92              
93 0           return '?' . join '&', @query_strings;
94             }
95              
96             1;
97              
98             __END__