| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Net::Azure::NotificationHubs; | 
| 2 | 5 |  |  | 5 |  | 192302 | use 5.008001; | 
|  | 5 |  |  |  |  | 40 |  | 
| 3 | 5 |  |  | 5 |  | 19 | use strict; | 
|  | 5 |  |  |  |  | 6 |  | 
|  | 5 |  |  |  |  | 66 |  | 
| 4 | 5 |  |  | 5 |  | 16 | use warnings; | 
|  | 5 |  |  |  |  | 7 |  | 
|  | 5 |  |  |  |  | 118 |  | 
| 5 | 5 |  |  | 5 |  | 1725 | use Net::Azure::Authorization::SAS; | 
|  | 5 |  |  |  |  | 52388 |  | 
|  | 5 |  |  |  |  | 107 |  | 
| 6 | 5 |  |  | 5 |  | 1457 | use Net::Azure::NotificationHubs::Request; | 
|  | 5 |  |  |  |  | 11 |  | 
|  | 5 |  |  |  |  | 121 |  | 
| 7 | 5 |  |  | 5 |  | 22 | use JSON; | 
|  | 5 |  |  |  |  | 6 |  | 
|  | 5 |  |  |  |  | 15 |  | 
| 8 | 5 |  |  | 5 |  | 2675 | use HTTP::Tiny; | 
|  | 5 |  |  |  |  | 171010 |  | 
|  | 5 |  |  |  |  | 151 |  | 
| 9 | 5 |  |  | 5 |  | 29 | use URI; | 
|  | 5 |  |  |  |  | 9 |  | 
|  | 5 |  |  |  |  | 71 |  | 
| 10 | 5 |  |  | 5 |  | 20 | use Carp; | 
|  | 5 |  |  |  |  | 8 |  | 
|  | 5 |  |  |  |  | 192 |  | 
| 11 | 5 |  |  | 5 |  | 21 | use String::CamelCase qw/camelize wordsplit/; | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 201 |  | 
| 12 |  |  |  |  |  |  | use Class::Accessor::Lite ( | 
| 13 | 5 |  |  |  |  | 40 | new => 0, | 
| 14 |  |  |  |  |  |  | ro  => [qw[ | 
| 15 |  |  |  |  |  |  | agent | 
| 16 |  |  |  |  |  |  | serializer | 
| 17 |  |  |  |  |  |  | api_version | 
| 18 |  |  |  |  |  |  | authorizer | 
| 19 |  |  |  |  |  |  | apns_expiry | 
| 20 |  |  |  |  |  |  | hub_name | 
| 21 |  |  |  |  |  |  | ]], | 
| 22 | 5 |  |  | 5 |  | 72 | ); | 
|  | 5 |  |  |  |  | 8 |  | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | our $VERSION             = "0.07"; | 
| 25 |  |  |  |  |  |  | our $DEFAULT_API_VERSION = "2015-04"; | 
| 26 |  |  |  |  |  |  | our $DEFAULT_TIMEOUT     = 60; | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | sub new { | 
| 29 | 9 |  |  | 9 | 1 | 12911 | my ($class, %param) = @_; | 
| 30 |  |  |  |  |  |  |  | 
| 31 | 9 |  |  |  |  | 71 | $param{agent}         = HTTP::Tiny->new(agent => sprintf('%s/%s', $class, $VERSION)); | 
| 32 | 9 |  |  |  |  | 619 | $param{serializer}    = JSON->new->utf8(1); | 
| 33 | 9 |  | 100 |  |  | 46 | $param{api_version} ||= $DEFAULT_API_VERSION; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 9 | 100 |  |  |  | 22 | if (!defined $param{authorizer}) { | 
| 36 | 8 |  |  |  |  | 10 | $param{authorizer} = eval { | 
| 37 | 8 |  |  |  |  | 41 | Net::Azure::Authorization::SAS->new(connection_string => $param{connection_string}); | 
| 38 |  |  |  |  |  |  | }; | 
| 39 | 8 | 100 |  |  |  | 1355 | if ($@) { | 
| 40 | 1 |  |  |  |  | 6 | croak $@; | 
| 41 |  |  |  |  |  |  | }; | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 8 |  |  |  |  | 51 | bless {%param}, $class; | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | sub _uri { | 
| 48 | 8 |  |  | 8 |  | 8260 | my ($self, $path, %params) = @_; | 
| 49 | 8 |  | 100 |  |  | 23 | $path ||= '/'; | 
| 50 | 8 |  |  |  |  | 22 | my $uri = URI->new($self->authorizer->endpoint); | 
| 51 | 8 |  |  |  |  | 13893 | $uri->scheme('https'); | 
| 52 | 8 |  |  |  |  | 8335 | $uri->path($path); | 
| 53 | 8 |  |  |  |  | 253 | $uri->query_form(%params); | 
| 54 | 8 |  |  |  |  | 384 | $uri; | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | sub _req { | 
| 58 | 8 |  |  | 8 |  | 9929 | my ($self, $path, $payload, %params) = @_; | 
| 59 | 8 | 100 |  |  |  | 31 | croak 'path is reuired'        if !defined $path; | 
| 60 | 7 | 100 |  |  |  | 16 | croak 'payload is required'    if !defined $payload; | 
| 61 | 6 | 100 |  |  |  | 21 | croak 'payload is not hashref' if ref($payload) ne 'HASH'; | 
| 62 | 5 |  | 100 |  |  | 32 | $params{api_version} ||= $self->api_version; | 
| 63 | 5 |  |  |  |  | 35 | my $uri  = $self->_uri($path, %params); | 
| 64 | 5 |  |  |  |  | 15 | my $auth = $self->authorizer->token($uri->as_string); | 
| 65 | 5 |  |  |  |  | 956 | my $data = $self->serializer->encode($payload); | 
| 66 | 5 |  |  |  |  | 74 | my $req  = Net::Azure::NotificationHubs::Request->new( | 
| 67 |  |  |  |  |  |  | POST => $uri->as_string, | 
| 68 |  |  |  |  |  |  | { | 
| 69 |  |  |  |  |  |  | 'Authorization' => $auth, | 
| 70 |  |  |  |  |  |  | 'Content-Type'  => 'application/atom+xml;charset=utf-8', | 
| 71 |  |  |  |  |  |  | }, | 
| 72 |  |  |  |  |  |  | $data, | 
| 73 |  |  |  |  |  |  | ); | 
| 74 | 5 |  |  |  |  | 267 | $req->agent($self->agent); | 
| 75 | 5 |  |  |  |  | 54 | $req; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | sub send { | 
| 79 | 2 |  |  | 2 | 1 | 4415 | my ($self, $payload, %param) = @_; | 
| 80 | 2 |  |  |  |  | 8 | my $path = sprintf "/%s/messages/", $self->hub_name; | 
| 81 | 2 |  |  |  |  | 18 | my $req = $self->_req($path, $payload); | 
| 82 | 2 |  |  |  |  | 5 | for my $key (keys %param) { | 
| 83 | 3 | 50 |  |  |  | 6 | next if !defined $param{$key}; | 
| 84 | 3 |  |  |  |  | 8 | my $header_name = join('-', 'ServiceBusNotification', wordsplit(camelize($key))); | 
| 85 | 3 |  |  |  |  | 52 | $req->header($header_name => $param{$key}); | 
| 86 |  |  |  |  |  |  | } | 
| 87 | 2 | 100 |  |  |  | 6 | if ($param{format} eq 'apple') { | 
| 88 | 1 |  |  |  |  | 3 | $req->header('ServiceBusNotification-Apns-Expiry' => $self->apns_expiry); | 
| 89 |  |  |  |  |  |  | } | 
| 90 | 2 |  |  |  |  | 5 | $req; | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | 1; | 
| 95 |  |  |  |  |  |  | __END__ |