File Coverage

blib/lib/Ticketmaster/API.pm
Criterion Covered Total %
statement 35 53 66.0
branch 12 18 66.6
condition 1 9 11.1
subroutine 10 11 90.9
pod 4 5 80.0
total 62 96 64.5


line stmt bran cond sub pod time code
1             package Ticketmaster::API;
2              
3 2     2   33077 use 5.006;
  2         4  
4 2     2   6 use strict;
  2         3  
  2         33  
5 2     2   6 use warnings;
  2         9  
  2         40  
6              
7 2     2   6 use Carp;
  2         2  
  2         112  
8 2     2   1246 use JSON::XS;
  2         11127  
  2         95  
9 2     2   1076 use LWP::UserAgent;
  2         56312  
  2         642  
10              
11             =head1 NAME
12              
13             Ticketmaster::API - start interacting with Ticketmaster's APIs
14              
15             =head1 VERSION
16              
17             Version 0.02
18              
19             =cut
20              
21             our $VERSION = '0.02';
22              
23              
24             =head1 SYNOPSIS
25              
26             Core module to facilitate interacting with Ticketmaster's APIs.
27              
28             Unless you are creating unique way to connect to Ticketmaster's API you probably
29             don't want this module. Please see one of the following:
30              
31             Ticketmaster::API::Discover
32              
33             To be able to interact with Ticketmaster's APIs you'll need to get an API key.
34              
35             General documentation can be found here: http://ticketmaster-api.github.io
36              
37              
38             =head1 NOTES
39              
40             =over 2
41              
42             =item Mozilla::CA
43              
44             Since connections are made via https Mozilla::CA will most likely need to be installed.
45              
46             =back
47              
48              
49             =head1 SUBROUTINES/METHODS
50              
51             =head2 new
52              
53             Create a new instance of Ticketmaster::API.
54              
55             # use the default base_uri and version infromation
56             my $tm_api = Ticketmaster::API->new(api_key => $api_key);
57              
58             my $tm_api = Ticketmaster::API->new(
59             api_key => $api_key,
60             base_uri => 'https://new_api_endpoint.ticketmaster.com',
61             version => 'v2'
62             );
63              
64             =cut
65              
66             sub new {
67 5     5 1 1420 my $self;
68 5         6 my $package = shift;
69 5   33     19 my $class = ref($package) || $package;
70              
71 5         12 $self = {@_};
72 5         5 bless($self, $class);
73              
74 5 100       8 $self->base_uri('https://app.ticketmaster.com') unless $self->base_uri;
75 5 100       8 $self->version('v1') unless $self->version;
76            
77 5 100       9 Carp::croak("No api_key provided") unless $self->api_key();
78              
79 4         9 return $self;
80             }
81              
82             =head2 base_uri
83              
84             Set/Get the base endpoint for the Ticketmaster API.
85              
86             Default: https://app.ticketmaster.com
87              
88             =cut
89             sub base_uri {
90 10     10 1 16 my $self = shift;
91              
92 10 100       17 $self->{base_uri} = shift if @_;
93              
94 10         25 return $self->{base_uri};
95             }
96              
97             =head2 base_uri
98              
99             Set/Get the version of the Ticketmaster API that is currently being hit.
100              
101             Default: v1
102              
103             =cut
104             sub version {
105 13     13 0 41 my $self = shift;
106              
107 13 100       19 $self->{version} = shift if @_;
108              
109 13         33 return $self->{version};
110             }
111              
112             =head2 api_key
113              
114             Set/Get the user's API key to the Ticketmaster API.
115              
116             =cut
117             sub api_key {
118 10     10 1 11 my $self = shift;
119              
120 10 100       18 $self->{api_key} = shift if @_;
121              
122 10         170 return $self->{api_key};
123             }
124              
125             =head2 get_data
126              
127             Connect to the TM API to receive the information being requested.
128              
129             my $res = $tm_api->get_data(method => 'GET', path_template => '/discovery/%s/events');
130              
131             =over 2
132              
133             =item method
134              
135             The REST method to execute on the endpoint. IE: GET
136              
137             =item path_template
138              
139             A sprintf template that will be combined with the base_uri value to generate the final endpoint.
140              
141             Example: /discovery/%s/events
142              
143             The '%s' is the location of the version number of the API being hit.
144              
145             =item parameters
146              
147             A hash reference of any parameters that are to be added to the endpoint
148              
149             =back
150              
151             =cut
152             # Requires: method, path_template (sprintf string), parameters (hash ref)
153             sub get_data {
154 0     0 1   my $self = shift;
155 0           my %args = @_;
156              
157 0   0       my $method = $args{method} || Carp::croak("No method provided (GET)");
158 0   0       my $path_template = $args{path_template} || Carp::croak("No URI template provided");
159 0 0         my %parameters = exists $args{parameters} ? %{$args{parameters}} : ();
  0            
160              
161 0           my $uri = $self->base_uri;
162 0 0         $uri .= '/' unless $uri =~ /\/$/;
163 0           $uri .= sprintf($path_template, $self->version());
164              
165 0           $uri .= '?apikey=' . $self->api_key();
166              
167 0           foreach my $key (keys %parameters) {
168 0           $uri .= '&' . $key . '=' . $parameters{$key};
169             }
170              
171 0           my $ua = LWP::UserAgent->new;
172              
173 0           my $req = HTTP::Request->new($method => $uri);
174              
175 0           my $res = $ua->request($req);
176              
177 0 0         if ($res->is_success) {
178 0           return decode_json($res->content);
179             }
180             else {
181 0           Carp::croak("Error: " . $res->status_line);
182             }
183             }
184              
185             =head1 AUTHOR
186              
187             Erik Tank, C<< >>
188              
189             =head1 BUGS
190              
191             Please report any bugs or feature requests to C, or through
192             the web interface at L. I will be notified, and then you'll
193             automatically be notified of progress on your bug as I make changes.
194              
195              
196             =head1 SUPPORT
197              
198             You can find documentation for this module with the perldoc command.
199              
200             perldoc Ticketmaster::API
201              
202              
203             You can also look for information at:
204              
205             =over 4
206              
207             =item * RT: CPAN's request tracker (report bugs here)
208              
209             L
210              
211             =item * AnnoCPAN: Annotated CPAN documentation
212              
213             L
214              
215             =item * CPAN Ratings
216              
217             L
218              
219             =item * Search CPAN
220              
221             L
222              
223             =back
224              
225              
226             =head1 ACKNOWLEDGEMENTS
227              
228              
229             =head1 LICENSE AND COPYRIGHT
230              
231             Copyright 2016 Erik Tank.
232              
233             This program is free software; you can redistribute it and/or modify it
234             under the terms of the the Artistic License (2.0). You may obtain a
235             copy of the full license at:
236              
237             L
238              
239             Any use, modification, and distribution of the Standard or Modified
240             Versions is governed by this Artistic License. By using, modifying or
241             distributing the Package, you accept this license. Do not use, modify,
242             or distribute the Package, if you do not accept this license.
243              
244             If your Modified Version has been derived from a Modified Version made
245             by someone other than you, you are nevertheless required to ensure that
246             your Modified Version complies with the requirements of this license.
247              
248             This license does not grant you the right to use any trademark, service
249             mark, tradename, or logo of the Copyright Holder.
250              
251             This license includes the non-exclusive, worldwide, free-of-charge
252             patent license to make, have made, use, offer to sell, sell, import and
253             otherwise transfer the Package with respect to any patent claims
254             licensable by the Copyright Holder that are necessarily infringed by the
255             Package. If you institute patent litigation (including a cross-claim or
256             counterclaim) against any party alleging that the Package constitutes
257             direct or contributory patent infringement, then this Artistic License
258             to you shall terminate on the date that such litigation is filed.
259              
260             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
261             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
262             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
263             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
264             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
265             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
266             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
267             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
268              
269              
270             =cut
271              
272             1; # End of Ticketmaster::API