File Coverage

blib/lib/WebService/GialloZafferano.pm
Criterion Covered Total %
statement 39 42 92.8
branch 3 6 50.0
condition n/a
subroutine 13 13 100.0
pod 1 1 100.0
total 56 62 90.3


line stmt bran cond sub pod time code
1             package WebService::GialloZafferano;
2              
3 1     1   24082 use strict;
  1         3  
  1         38  
4 1     1   29 use 5.008_005;
  1         3  
  1         40  
5 1     1   1016 use Mojo::UserAgent;
  1         379874  
  1         16  
6 1     1   39 use Encode;
  1         3  
  1         129  
7 1     1   5 use Mojo::Base -base;
  1         2  
  1         7  
8 1     1   246 use Mojo::DOM;
  1         2  
  1         20  
9 1     1   4 use feature 'say';
  1         1  
  1         29  
10 1     1   4 use List::Util qw( min );
  1         1  
  1         68  
11 1     1   741 use WebService::GialloZafferano::Recipe;
  1         3  
  1         9  
12 1     1   31 use WebService::GialloZafferano::Ingredient;
  1         2  
  1         6  
13              
14             our $VERSION = '0.02';
15              
16             =encoding utf-8
17              
18             =head1 NAME
19              
20             WebService::GialloZafferano - Perl interface to GialloZafferano.it website to find cooking recipes
21              
22             =head1 SYNOPSIS
23              
24             my $Cook = WebService::GialloZafferano->new();
25             my @recipes = $Cook->search("Spaghetti"); # It returns a list of WebService::GialloZafferano::Recipe
26             my $spaghetti_recipe= $recipes[0]->text; #i wanna knew more about that recipe
27             my @Ingredients = $recipes[0]->ingredients(); # i'm not happy, wanna know the ingredients of the next one
28             # @Ingredients is a list of WebService::GialloZafferano::Ingredient
29              
30             =head1 DESCRIPTION
31              
32             WebService::GialloZafferano is a Perl interface to the site GialloZafferano.it, it allows to query the cooking recipes and get the ingredients
33              
34             =head1 METHODS
35              
36             =over
37              
38             =item search
39              
40             Takes input terms and process the GialloZafferano.it research.
41             It returns a list of L.
42              
43             returns undef on error
44              
45             =back
46              
47             =head1 ATTRIBUTES
48              
49             =over
50              
51             =item ua
52              
53             Returns the L instance
54              
55             =back
56              
57             =head1 AUTHOR
58              
59             mudler Emudler@dark-lab.netE
60              
61             =head1 COPYRIGHT
62              
63             Copyright 2014- mudler
64              
65             =head1 LICENSE
66              
67             This library is free software; you can redistribute it and/or modify
68             it under the same terms as Perl itself.
69              
70             =head1 SEE ALSO
71              
72             L, L
73              
74             =cut
75              
76             has 'ua' => sub { Mojo::UserAgent->new };
77              
78             sub search {
79 1     1 1 1290 my $self = shift;
80 1         3 my $term = shift;
81              
82             # Form POST with exception handling
83 1         44 my $tx = $self->ua->max_redirects(2)->post(
84             'http://www.giallozafferano.it/ricerca-ricette/' => form => {
85             'q' => $term,
86             'fakeq' => $term
87             }
88             );
89 1 50       141686 if ( my $res = $tx->success ) {
90             my %ric = $res->dom->find('a[href]')->grep(
91             sub {
92 202 100   202   173971 $_->attr("title")
93             and $_->attr('href')
94             =~ /ricette\.giallozafferano\.it\/.*\.html$/;
95             }
96             )->map(
97             sub {
98 36     36   4268 $_->attr("title") => WebService::GialloZafferano::Recipe->new(
99             ua => $self->ua,
100             title => $_->{'title'},
101             url => $_->{'href'}
102             );
103             }
104 1         110 )->each;
105 1         363 my @ricette = values %ric;
106             return
107             @ricette
108 1         136 ; #returns an array of WebService::GialloZafferano::Recipe objs
109             }
110             else {
111 0           my ( $err, $code ) = $tx->error;
112 0 0         say $code ? "$code response: $err" : "Connection error: $err";
113 0           return undef;
114             }
115             }
116              
117             1;
118             __END__