File Coverage

blib/lib/WebService/GialloZafferano/Recipe.pm
Criterion Covered Total %
statement 21 34 61.7
branch 2 12 16.6
condition 1 6 16.6
subroutine 6 7 85.7
pod 2 2 100.0
total 32 61 52.4


line stmt bran cond sub pod time code
1             package WebService::GialloZafferano::Recipe;
2 1     1   6 use Mojo::Base -base;
  1         1  
  1         6  
3 1     1   605 use WebService::GialloZafferano::Ingredient;
  1         3  
  1         40  
4 1     1   26 use Mojo::UserAgent;
  1         1  
  1         12  
5 1     1   21 use Encode;
  1         1  
  1         732  
6              
7             =encoding utf-8
8              
9             =head1 NAME
10              
11             WebService::GialloZafferano::Recipe - Represent a recipe of GialloZafferano.it website
12              
13             =head1 SYNOPSIS
14              
15             my $Recipe = WebService::GialloZafferano::Recipe->new();
16              
17             =head1 DESCRIPTION
18              
19             WebService::GialloZafferano::Recipe represent a Recipe of the site GialloZafferano.it .
20              
21             =head1 ATTRIBUTES
22              
23             =over
24              
25             =item title
26              
27             $Recipe->title() #gets the title of the recipe
28             $Recipe->title("Spaghetti allo Scoglio") #sets the title of the recipe
29              
30             returns undef on error
31              
32             =item url
33              
34             $Recipe->url() #gets the of the recipe
35             $Recipe->url("http://...") #sets the url
36              
37             returns undef on error
38              
39             =back
40              
41             =head1 METHODS
42              
43             =over
44              
45             =item text
46              
47             $Recipe->text() #gets the text of the recipe
48              
49             returns undef on error
50              
51             =item ingredients
52              
53             $Recipe->ingredients() #gets the ingredients of the recipe
54              
55             It returns an array of L.
56             returns undef on error
57              
58             =back
59              
60             =head1 AUTHOR
61              
62             mudler Emudler@dark-lab.netE
63              
64             =head1 COPYRIGHT
65              
66             Copyright 2014 mudler
67              
68             =head1 LICENSE
69              
70             This library is free software; you can redistribute it and/or modify
71             it under the same terms as Perl itself.
72              
73             =head1 SEE ALSO
74              
75             L, L
76              
77             =cut
78              
79             has 'title';
80             has 'url';
81             has '_ingredients';
82             has '_text';
83             has 'ua' => sub { Mojo::UserAgent->new };
84              
85             sub text {
86 0     0 1 0 my $self = shift;
87 0 0       0 return $self->{'_text'} if exists $self->{'_text'};
88 0   0     0 my $url = shift || $self->url;
89              
90             # Form POST with exception handling
91 0         0 my $tx = $self->ua->max_redirects(2)->get($url);
92 0 0       0 if ( my $res = $tx->success ) {
93 0         0 $self->_text(encode_utf8($res->dom->find("div.steps")->first->text));
94 0         0 return $self->_text;
95             }
96             else {
97 0         0 my ( $err, $code ) = $tx->error;
98 0 0       0 say $code ? "$code response: $err" : "Connection error: $err";
99 0         0 return undef;
100             }
101             }
102              
103             sub ingredients {
104 1     1 1 2769 my $self = shift;
105 1 50       7 return $self->{'_ingredients'} if exists $self->{'_ingredients'};
106 1   33     23 my $url = shift || $self->url;
107             # Form POST with exception handling
108 1         56 my $tx = $self->ua->max_redirects(2)->get( $url );
109 1 50       63195 if ( my $res = $tx->success ) {
110             my %Ingredients = $res->dom->find("dd.ingredient")->map(
111             sub {
112 10     10   125179 $_->find('em')->text =>
113             WebService::GialloZafferano::Ingredient->new(
114             name => $_->find('em')->first->text,
115             quantity => $_->find('span')->first->text
116             );
117             }
118 1         125 )->each;
119 1         1861 $self->_ingredients(values %Ingredients);
120             return
121 1         39 values %Ingredients
122             ; #returns uniques WebService::GialloZafferano::Ingredient
123             }
124             else {
125 0           my ( $err, $code ) = $tx->error;
126 0 0         say $code ? "$code response: $err" : "Connection error: $err";
127 0           return undef;
128             }
129             }
130             1;