File Coverage

blib/lib/Net/Semantics3.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Net::Semantics3;
2              
3 1     1   23804 use strict;
  1         2  
  1         37  
4 1     1   5 use warnings;
  1         2  
  1         30  
5              
6 1     1   374 use Moose;
  0            
  0            
7             use methods;
8             use JSON::XS;
9             use OAuth::Lite::Consumer;
10             use Data::Dumper;
11              
12             use Net::Semantics3::Error;
13              
14             our $VERSION = '0.1';
15              
16             =head1 NAME
17              
18             Net::Semantics3
19              
20             =head1 DESCRIPTION
21              
22             Base for API Client interfacing with the Semantics3 APIs.
23              
24             =cut
25              
26             has 'api_key' => ( is => 'ro', isa => 'Str', required => 1 );
27             has 'api_secret' => ( is => 'ro', isa => 'Str', required => 1 );
28             has 'api_base' => ( is => 'ro', isa => 'Str', lazy_build => 1 );
29             has 'oauth_client' => ( is => 'ro', isa => 'Object', lazy_build => 1 );
30              
31             method _get {
32             my $path = shift;
33             my $jsonParams = shift;
34             return $self->_make_request( 'GET', $path, $jsonParams );
35             }
36              
37             method _make_request {
38             my $reqType = shift;
39             my $reqPath = shift;
40             my $reqParamsJson = shift;
41              
42             my $url = $self->api_base . '/' . $reqPath;
43              
44             my $resp;
45             my $hashRef;
46              
47             my $e = eval{
48             $resp = $self->oauth_client->request(
49             method => $reqType,
50             url => $url,
51             params => {q => $reqParamsJson}
52             );
53             };
54              
55             if($@) {
56             Net::Semantics3::Error->new(
57             type => "OAuth Rqeuest failed: $@",
58             message => Dumper( $resp ),
59             );
60             }
61              
62             if ($resp->code != 200) {
63             Net::Semantics3::Error->new(
64             type => "HTTP Request resulted in error: $@",
65             message => $resp->status_line . " - Error code: " . $resp->code,
66             );
67             }
68              
69             $e = eval { $hashRef = decode_json($resp->content) };
70             if ($@) {
71             Net::Semantics3::Error->new(
72             type => "Could not decode JSON response: $@",
73             message => $resp->status_line . " - " . $resp->content,
74             );
75             }
76             else {
77             return $hashRef;
78             }
79              
80             die "Fatal Error\n";
81             }
82              
83             method _build_api_base { 'https://api.semantics3.com/v1' }
84              
85             method _build_oauth_client {
86             my $ua = LWP::UserAgent->new;
87             $ua->agent( "Semantics3 Perl Lib/$VERSION" );
88              
89             my $oauthClient = OAuth::Lite::Consumer->new(
90             ua => $ua,
91             consumer_key => $self->api_key,
92             consumer_secret => $self->api_secret,
93             );
94              
95             return $oauthClient;
96             }
97              
98              
99             =head1 SEE ALSO
100              
101             L<https://semantics3.com>, L<https://semantics3.com/docs>
102              
103             =head1 AUTHOR
104              
105             Sivamani Varun, varun@semantics3.com
106              
107             =head1 COPYRIGHT AND LICENSE
108              
109             Net-Semantics3 is Copyright (C) 2013 Semantics3 Inc.
110              
111             This software is released under the MIT license cited below.
112              
113             =head2 The "MIT" License
114              
115             Permission is hereby granted, free of charge, to any person obtaining a copy
116             of this software and associated documentation files (the "Software"), to deal
117             in the Software without restriction, including without limitation the rights
118             to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
119             copies of the Software, and to permit persons to whom the Software is
120             furnished to do so, subject to the following conditions:
121              
122             The above copyright notice and this permission notice shall be included in
123             all copies or substantial portions of the Software.
124              
125             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
126             OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
127             FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
128             THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
129             LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
130             FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
131             DEALINGS IN THE SOFTWARE.
132              
133             =cut
134              
135             __PACKAGE__->meta->make_immutable;
136             1;