File Coverage

blib/lib/Net/Semantics3.pm
Criterion Covered Total %
statement 22 24 91.6
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 30 32 93.7


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