File Coverage

blib/lib/WebService/OMDB.pm
Criterion Covered Total %
statement 45 49 91.8
branch 7 14 50.0
condition 0 2 0.0
subroutine 11 11 100.0
pod 3 3 100.0
total 66 79 83.5


line stmt bran cond sub pod time code
1 1     1   107640 use 5.010;
  1         4  
  1         43  
2 1     1   6 use strict;
  1         3  
  1         34  
3 1     1   5 use warnings;
  1         2  
  1         62  
4              
5             package WebService::OMDB;
6             {
7             $WebService::OMDB::VERSION = '1.140440';
8             }
9              
10             # ABSTRACT: Interface to http://www.omdbapi.com/
11              
12 1     1   1626 use LWP::UserAgent;
  1         53043  
  1         31  
13 1     1   1011 use JSON;
  1         13257  
  1         6  
14              
15 1     1   136 use constant BASE_URL => 'http://www.omdbapi.com/';
  1         3  
  1         418  
16              
17             sub search {
18 1     1 1 553 my ( $s, $options ) = @_;
19              
20 1 50       5 die "search string is required" unless $s;
21              
22 1         5 my $response = _get( 's', $s, $options );
23 1 50       342612 if ( $response->is_success ) {
24              
25 1         17 my $content = decode_json( $response->content );
26 1         52 return $content->{Search};
27             }
28             else {
29 0         0 die $response->status_line;
30             }
31             }
32              
33             sub id {
34 1     1 1 680 my ( $i, $options ) = @_;
35              
36 1 50       5 die "id is required" unless $i;
37              
38 1         4 my $response = _get( 'i', $i, $options );
39 1 50       236969 if ( $response->is_success ) {
40              
41 1         19 my $content = decode_json( $response->content );
42 1         60 return $content;
43             }
44             else {
45 0         0 die $response->status_line;
46             }
47              
48             }
49              
50             sub title {
51 1     1 1 1806 my ( $t, $options ) = @_;
52              
53 1 50       6 die "title is required" unless $t;
54              
55 1         5 my $response = _get( 't', $t, $options );
56 1 50       237147 if ( $response->is_success ) {
57              
58 1         18 my $content = decode_json( $response->content );
59 1         68 return $content;
60             }
61             else {
62 0         0 die $response->status_line;
63             }
64              
65             }
66              
67             sub _get {
68 3     3   8 my ( $search_param, $search_term, $options ) = @_;
69              
70 3         11 my $url = _generate_url( $search_param, $search_term, $options );
71 3         28 my $ua = LWP::UserAgent->new();
72 3 50       3367 $ua->agent( $options->{user_agent} ) if $options->{user_agent};
73 3         13 return $ua->get($url);
74             }
75              
76             # generates a url from the options
77             sub _generate_url {
78 3     3   6 my ( $search_param, $search_term, $options ) = @_;
79              
80 3         17 my $url = sprintf( "%s?%s=%s", BASE_URL, $search_param, $search_term );
81              
82 3         19 while ( my ( $key, $value ) = each(%$options) ) {
83 0   0     0 $url .= sprintf( "&%s=%s", $key, $value // 0 );
84             }
85              
86 3         9 return $url;
87             }
88              
89             1;
90              
91             __END__