File Coverage

lib/WebService/Tesco.pm
Criterion Covered Total %
statement 21 34 61.7
branch n/a
condition n/a
subroutine 7 9 77.7
pod 2 2 100.0
total 30 45 66.6


line stmt bran cond sub pod time code
1             package WebService::Tesco;
2              
3 1     1   430 use strict;
  1         2  
  1         47  
4 1     1   6 use warnings;
  1         2  
  1         46  
5              
6              
7             my $LOGIN="https://secure.techfortesco.com/tescolabsapi/restservice.aspx?command=LOGIN&email=&password=";
8             my $SEARCH="https://secure.techfortesco.com/tescolabsapi/restservice.aspx?command=PRODUCTSEARCH";
9              
10              
11 1     1   680 use Data::Dumper;
  1         5762  
  1         64  
12 1     1   412 use LWP::Simple;
  1         49311  
  1         8  
13 1     1   340 use JSON;
  1         2  
  1         7  
14 1     1   88 use URI::Escape;
  1         1  
  1         43  
15 1     1   3 use Encode;
  1         1  
  1         182  
16              
17             =head1 NAME
18              
19             WebService::Tesco
20              
21             =head1 VERSION
22              
23             0.1
24              
25             Simple wrapper for the Tesco API documented here:
26              
27             http://www.tescolabs.com/?p=7171
28              
29             =cut
30              
31             =head1 methods
32              
33             =over
34              
35             =item new
36              
37              
38             Parameters:
39             my $tesco = WebService::Tesco->new({
40             dev_key => $dev_key,
41             app_key => $app_key,
42             });
43              
44             =cut
45              
46             sub new {
47 0     0 1   my ($class, $params) = @_ ;
48 0           my $obj = bless {}, $class ;
49 0           $obj->{cfg} = $params;
50 0           $obj->{session} = $obj->auth($obj->{cfg});
51 0           return $obj ;
52             }
53              
54             =item auth
55              
56             Called from new. You don't need this.
57              
58             =cut
59              
60             sub auth {
61 0     0 1   my ($self, $params) = @_;
62              
63 0           my $str = $LOGIN .
64             "&developerkey=" . $self->{cfg}->{dev_key} .
65             "&applicationkey=" . $self->{cfg}->{app_key} ;
66              
67 0           warn $str;
68 0           my $ret = get($str);
69              
70 0           $ret = decode_json $ret;
71 0           warn $ret->{SessionKey};
72 0           $self->{session} = $ret->{SessionKey};
73 0           return $ret->{SessionKey} ;
74             }
75              
76             =item search
77              
78             Parameters:
79             my $res = $tesco->search({
80             searchtext => "Chef",
81             page => 1 # defaults to 1
82             });
83              
84             $res is an array of hashrefs:
85              
86             {
87             "StatusCode": 0,
88             "StatusInfo": "Command Processed OK",
89             "PageNumber": 1,
90             "TotalPageCount": 1,
91             "TotalProductCount": 1,
92             "PageProductCount": 1,
93             "Products":
94             [
95             {
96             "BaseProductId": "77534748",
97             "EANBarcode": "5055761903331",
98             "CheaperAlternativeProductId": "",
99             "HealthierAlternativeProductId": "",
100             "ImagePath": "http://img.tesco.com/Groceries/pi/331/5055761903331/IDShot_90x90.jpg",
101             "MaximumPurchaseQuantity": 99,
102             "Name": "Chef Dvd",
103             "OfferPromotion": "",
104             "OfferValidity": "",
105             "OfferLabelImagePath": "",
106             "ShelfCategory": "",
107             "ShelfCategoryName": "",
108             "Price": 10,
109             "PriceDescription": "10.00 each",
110             "ProductId": "285334791",
111             "ProductType": "QuantityOnlyProduct",
112             "UnitPrice": 10,
113             "UnitType": "EACH"
114             }
115             ]
116             }
117              
118             =cut
119              
120             =back
121              
122             sub search {
123             my ($self, $params) = @_ ;
124              
125             warn Dumper($params) ;
126              
127             my $str = $SEARCH .
128             "&sessionkey=" . $self->{session} .
129             "&page=" . $params->{page} .
130             "&searchtext=" . uri_escape("chef dvd");
131             warn $str;
132              
133             my $ret = get($str) ;
134             $ret = decode_json(encode_utf8($ret)) ;
135             warn Dumper($ret);
136              
137             return $ret;
138             }
139              
140              
141              
142             1;