line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::Notifo; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
114509
|
use warnings; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
77
|
|
4
|
2
|
|
|
2
|
|
13
|
use strict; |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
68
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
12
|
use Carp; |
|
2
|
|
|
|
|
10
|
|
|
2
|
|
|
|
|
275
|
|
7
|
2
|
|
|
2
|
|
1651
|
use JSON; |
|
2
|
|
|
|
|
43238
|
|
|
2
|
|
|
|
|
12
|
|
8
|
2
|
|
|
2
|
|
3532
|
use Data::Dumper; |
|
2
|
|
|
|
|
20494
|
|
|
2
|
|
|
|
|
186
|
|
9
|
2
|
|
|
2
|
|
2257
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
141428
|
|
|
2
|
|
|
|
|
73
|
|
10
|
2
|
|
|
2
|
|
870
|
use MIME::Base64; |
|
2
|
|
|
|
|
793
|
|
|
2
|
|
|
|
|
168
|
|
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
14
|
use base qw( Exporter ); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
278
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our @EXPORT_OK = qw( notifo ); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 NAME |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
WWW::Notifo - Interface to notifo.com notification service |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 VERSION |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
This document describes WWW::Notifo version 0.09 |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=cut |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
our $VERSION = '0.09'; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head1 SYNOPSIS |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
use WWW::Notifo; |
31
|
|
|
|
|
|
|
my $notifo = WWW::Notifo->new( username => 'foo', secret => 'xabc123' ); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# Subscribe a user... |
34
|
|
|
|
|
|
|
my $status = $notifo->subscribe_user( username => 'bar' ); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# Send a notification |
37
|
|
|
|
|
|
|
my $status = $notifo->send_notification( |
38
|
|
|
|
|
|
|
to => 'someone', |
39
|
|
|
|
|
|
|
msg => 'Hello!', |
40
|
|
|
|
|
|
|
label => 'JAPH', |
41
|
|
|
|
|
|
|
title => 'Boo', |
42
|
|
|
|
|
|
|
uri => 'http://example.com/' |
43
|
|
|
|
|
|
|
); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 DESCRIPTION |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
Notifo (L) is a web based notification service that |
48
|
|
|
|
|
|
|
can send push messages to mobile deviceas. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
From L: |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
What Can I Do With Notifo? |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
If you are a User, you can subscribe to receive notifications from |
55
|
|
|
|
|
|
|
your favorite services that integrate with Notifo. On Notifo's site |
56
|
|
|
|
|
|
|
you can set timers, send yourself messages, set stock alerts, and |
57
|
|
|
|
|
|
|
Google Voice SMS alerts. More built-in services will be released in |
58
|
|
|
|
|
|
|
the near future. |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
If you are a Service, you can integrate with Notifo's API and start |
61
|
|
|
|
|
|
|
sending mobile notifications to your users within a few hours. No need |
62
|
|
|
|
|
|
|
to spend time or resources developing mobile applications just so you |
63
|
|
|
|
|
|
|
can reach your users! |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=cut |
66
|
|
|
|
|
|
|
|
67
|
2
|
|
|
2
|
|
26
|
use constant API => 'https://api.notifo.com/v1'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
132
|
|
68
|
|
|
|
|
|
|
|
69
|
2
|
|
|
2
|
|
1647
|
use accessors::ro qw( username secret last ); |
|
2
|
|
|
|
|
4159
|
|
|
2
|
|
|
|
|
21
|
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
BEGIN { |
72
|
2
|
|
|
2
|
|
17
|
my %meth = ( |
73
|
|
|
|
|
|
|
subscribe_user => { |
74
|
|
|
|
|
|
|
required => [qw( username )], |
75
|
|
|
|
|
|
|
optional => [], |
76
|
|
|
|
|
|
|
}, |
77
|
|
|
|
|
|
|
send_notification => { |
78
|
|
|
|
|
|
|
required => [qw( to msg )], |
79
|
|
|
|
|
|
|
optional => [qw( label title uri )], |
80
|
|
|
|
|
|
|
}, |
81
|
|
|
|
|
|
|
); |
82
|
2
|
|
|
|
|
7
|
for my $m ( keys %meth ) { |
83
|
2
|
|
|
2
|
|
361
|
no strict 'refs'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
142
|
|
84
|
4
|
|
|
|
|
1431
|
*{$m} = sub { |
85
|
6
|
|
|
6
|
|
2405
|
shift->_api( $m, @{ $meth{$m} }{ 'required', 'optional' }, @_ ); |
|
6
|
|
|
|
|
27
|
|
86
|
4
|
|
|
|
|
17
|
}; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub _need { |
91
|
11
|
|
|
11
|
|
29
|
my ( $need, $optional, @args ) = @_; |
92
|
11
|
100
|
|
|
|
169
|
croak "Expected a number of key => value pairs" |
93
|
|
|
|
|
|
|
if @args % 2; |
94
|
10
|
|
|
|
|
64
|
my %args = @args; |
95
|
10
|
|
|
|
|
21
|
my @missing = grep { !defined $args{$_} } @$need; |
|
17
|
|
|
|
|
44
|
|
96
|
10
|
100
|
|
|
|
236
|
croak "Missing options: ", join( ', ', sort @missing ) |
97
|
|
|
|
|
|
|
if @missing; |
98
|
9
|
100
|
|
|
|
26
|
if ( defined $optional ) { |
99
|
8
|
|
|
|
|
12
|
my %ok = map { $_ => 1 } @$need, @$optional; |
|
30
|
|
|
|
|
59
|
|
100
|
8
|
|
|
|
|
20
|
my @extra = grep { !$ok{$_} } keys %args; |
|
22
|
|
|
|
|
43
|
|
101
|
8
|
100
|
|
|
|
380
|
croak "Illegal otions: ", join( ', ', sort @extra ) if @extra; |
102
|
|
|
|
|
|
|
} |
103
|
7
|
|
|
|
|
39
|
return %args; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head2 C<< new >> |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Create a new C object. In common with all methods exposed |
109
|
|
|
|
|
|
|
by the module accepts a number of key => value pairs. The C |
110
|
|
|
|
|
|
|
and C options are mandatory: |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
my $notifo = WWW::Notifo->new( |
113
|
|
|
|
|
|
|
username => 'alice', |
114
|
|
|
|
|
|
|
secret => 'x3122b4c4d3bad5e8d7397f0501b617ce60afe5d' |
115
|
|
|
|
|
|
|
); |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=cut |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub new { |
120
|
4
|
|
|
4
|
1
|
8038
|
my $class = shift; |
121
|
4
|
|
|
|
|
14
|
return bless { _need( [ 'secret', 'username' ], [], @_ ) }, $class; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head2 API Calls |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
API calls provide access to the Notifo API. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
On success they return a reference to a hash containing the response |
129
|
|
|
|
|
|
|
from notifo.com. On errors an exception will be thrown. In the case of |
130
|
|
|
|
|
|
|
an error the response hash can be retrieved by calling C. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head3 C<< subscribe_user >> |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Service providers must call this method when users want to subscribe to |
135
|
|
|
|
|
|
|
notifo alerts. This method will send a confirmation message to the user |
136
|
|
|
|
|
|
|
where they can complete the opt-in process. The service provider will |
137
|
|
|
|
|
|
|
not be able to send notifications to the user until this subscribe/opt- |
138
|
|
|
|
|
|
|
in process has been completed. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Users sending notifications to themselves with their User account do not |
141
|
|
|
|
|
|
|
need to use this method. Since a User account can only send |
142
|
|
|
|
|
|
|
notifications to itself, it is already implicitly subscribed. Only |
143
|
|
|
|
|
|
|
Service accounts need to use this method to subscribe other users. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
my $resp = $notifo->subscribe_user( |
146
|
|
|
|
|
|
|
username => 'hexten' |
147
|
|
|
|
|
|
|
); |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head3 C<< send_notification >> |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Once a user has subscribed to notifo alerts, service providers can call |
152
|
|
|
|
|
|
|
this method to send notifications to specific users. The C and |
153
|
|
|
|
|
|
|
C parameters are required. The C parameter is optional, and |
154
|
|
|
|
|
|
|
can be thought of as a description of the type of notification being |
155
|
|
|
|
|
|
|
sent (almost like the subject of an email). The C parameter is used |
156
|
|
|
|
|
|
|
to specify what URI (web address, app uri, etc) will be loaded when the |
157
|
|
|
|
|
|
|
user opens the notification. If omitted, the default service provider |
158
|
|
|
|
|
|
|
URL is used. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
my $resp = $notifo->send_notification( |
161
|
|
|
|
|
|
|
to => 'hexten', |
162
|
|
|
|
|
|
|
msg => 'Testing...', |
163
|
|
|
|
|
|
|
label => 'Test', |
164
|
|
|
|
|
|
|
title => 'Hoot', |
165
|
|
|
|
|
|
|
uri => 'http://hexten.net/' |
166
|
|
|
|
|
|
|
); |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head3 C<< api >> |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
API entry points other than C and C |
171
|
|
|
|
|
|
|
(of which there are currently none) can be accessed directly by calling |
172
|
|
|
|
|
|
|
C. For example, the above send_notification example can also be |
173
|
|
|
|
|
|
|
written as: |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
my $resp = $notifo->api( |
176
|
|
|
|
|
|
|
'send_notification', |
177
|
|
|
|
|
|
|
to => 'hexten', |
178
|
|
|
|
|
|
|
msg => 'Testing...', |
179
|
|
|
|
|
|
|
label => 'Test', |
180
|
|
|
|
|
|
|
title => 'Hoot', |
181
|
|
|
|
|
|
|
uri => 'http://hexten.net/' |
182
|
|
|
|
|
|
|
); |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head3 C<< last >> |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Get the most recent response (a hash ref). Useful in the case of an HTTP |
187
|
|
|
|
|
|
|
error (which throws an exception). |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=cut |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
sub _api { |
192
|
7
|
|
|
7
|
|
25
|
my ( $self, $method, $need, $optional, @args ) = @_; |
193
|
7
|
|
|
|
|
20
|
my %args = _need( $need, $optional, @args ); |
194
|
6
|
|
|
|
|
18
|
my $resp |
195
|
|
|
|
|
|
|
= $self->_ua->post( join( '/', API, $method ), Content => \%args ); |
196
|
6
|
|
|
|
|
8953
|
my $rd = $self->{last} = eval { JSON->new->decode( $resp->content ) }; |
|
6
|
|
|
|
|
31
|
|
197
|
6
|
|
|
|
|
119
|
my $err = $@; |
198
|
6
|
100
|
|
|
|
22
|
if ( $resp->is_error ) { |
199
|
2
|
100
|
66
|
|
|
23
|
croak join ' ', @{$rd}{ 'response_code', 'response_message' } |
|
1
|
|
|
|
|
111
|
|
200
|
|
|
|
|
|
|
if !$err && $rd->{status} eq 'error'; |
201
|
1
|
|
|
|
|
4
|
croak $resp->status_line; |
202
|
|
|
|
|
|
|
} |
203
|
4
|
100
|
|
|
|
140
|
croak $err if $err; # Only report errors parsing JSON we have a 200 |
204
|
3
|
|
|
|
|
69
|
return $rd; |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
sub api { |
208
|
1
|
|
|
1
|
1
|
6
|
my ( $self, $method, @args ) = @_; |
209
|
1
|
|
|
|
|
4
|
return $self->_api( $method, [], undef, @args ); |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
sub _make_ua { |
213
|
1
|
|
|
1
|
|
2
|
my $self = shift; |
214
|
1
|
|
|
|
|
10
|
my $ua = LWP::UserAgent->new; |
215
|
1
|
|
|
|
|
3667
|
$ua->agent( join ' ', __PACKAGE__, $VERSION ); |
216
|
|
|
|
|
|
|
$ua->add_handler( |
217
|
|
|
|
|
|
|
request_send => sub { |
218
|
6
|
|
|
6
|
|
18687
|
shift->header( Authorization => $self->_auth_header ); |
219
|
|
|
|
|
|
|
} |
220
|
1
|
|
|
|
|
65
|
); |
221
|
1
|
|
|
|
|
34
|
return $ua; |
222
|
|
|
|
|
|
|
} |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
sub _auth_header { |
225
|
6
|
|
|
6
|
|
8
|
my $self = shift; |
226
|
6
|
|
|
|
|
22
|
return 'Basic ' |
227
|
|
|
|
|
|
|
. encode_base64( join( ':', $self->username, $self->secret ), '' ); |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
sub _ua { |
231
|
7
|
|
|
7
|
|
26
|
my $self = shift; |
232
|
7
|
|
66
|
|
|
58
|
return $self->{_ua} ||= $self->_make_ua; |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=head2 Procedural Interface |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
The following convenience subroutine may be exported: |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=head3 C<< notifo >> |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
Send a notification. |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
notifo( |
244
|
|
|
|
|
|
|
username => 'alice', |
245
|
|
|
|
|
|
|
secret => 'x3122b4c4d3bad5e8d7397f0501b617ce60afe5d', |
246
|
|
|
|
|
|
|
to => 'hexten', |
247
|
|
|
|
|
|
|
msg => 'Testing...', |
248
|
|
|
|
|
|
|
label => 'Test', |
249
|
|
|
|
|
|
|
title => 'Hoot', |
250
|
|
|
|
|
|
|
uri => 'http://hexten.net/' |
251
|
|
|
|
|
|
|
); |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=cut |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
sub notifo { |
256
|
0
|
|
|
0
|
1
|
|
my %opt = _need( [], undef, @_ ); |
257
|
0
|
|
|
|
|
|
return WWW::Notifo->new( map { $_ => delete $opt{$_} } |
|
0
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
qw( username secret ) )->send_notification( %opt ); |
259
|
|
|
|
|
|
|
} |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
1; |
262
|
|
|
|
|
|
|
__END__ |