Skip to content

Commit

Permalink
- Added get_key API call
Browse files Browse the repository at this point in the history
- Now you can login with email/password
  • Loading branch information
yannk committed Jul 26, 2009
1 parent 645e748 commit cd13445
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 16 deletions.
20 changes: 16 additions & 4 deletions eg/simple.pl
Expand Up @@ -3,9 +3,21 @@
use Find::Lib '../lib'; use Find::Lib '../lib';
use Net::AppNotifications; use Net::AppNotifications;


my $key = shift my ($message, $email, $pass, $key);
or die "usage: $0 <key> <message>";
my $message = shift || "hello";


my $notifier = Net::AppNotifications->new(key => $key); if (@ARGV == 3) {
($email, $pass, $message) = @ARGV;
}
elsif (@ARGV == 2) {
($key, $message) = @ARGV;
}
else {
die "usage: $0 [ <key> | <email> <pass> ] <message>";
}

my $notifier = Net::AppNotifications->new(
key => $key,
email => $email,
pass => $pass,
);
$notifier->send($message); $notifier->send($message);
81 changes: 69 additions & 12 deletions lib/Net/AppNotifications.pm
Expand Up @@ -10,12 +10,17 @@ use URI::Escape 'uri_escape_utf8';
use constant POST_URI => use constant POST_URI =>
q{https://www.appnotifications.com/account/notifications.xml}; q{https://www.appnotifications.com/account/notifications.xml};


# TODO: get key from user/pass use constant KEY_URI =>
q{https://www.appnotifications.com/account/user_session.xml};

sub new { sub new {
my $class = shift; my $class = shift;
my %param = @_; my %param = @_;
my $notifier = bless { %param }, ref $class || $class; my $notifier = bless { %param }, ref $class || $class;
croak "Key is needed" unless $param{key}; unless ($param{key}) {
$param{key} = $class->get_key( %param );
}
croak "Key (or valid email, pass to get it) is needed" unless $param{key};
return $notifier; return $notifier;
} }


Expand Down Expand Up @@ -83,7 +88,13 @@ sub send {
}; };


} }
$notifier->post_request($key, $message, %cbs); my $uri = POST_URI;
my $body = build_body(
'user_credentials' => $key,
'notification[message]' => $message,
);

$notifier->post_request($uri, $body, %cbs);


## wait here for synchronous calls ## wait here for synchronous calls
$finish->(); $finish->();
Expand All @@ -92,10 +103,7 @@ sub send {


sub post_request { sub post_request {
my $notifier = shift; my $notifier = shift;
my ($key, $message, %cbs) = @_; my ($uri, $body, %cbs) = @_;

my $uri = POST_URI;
my $body = build_body($key, $message);


http_request http_request
POST => $uri, POST => $uri,
Expand All @@ -115,11 +123,48 @@ sub post_request {
return return
} }


sub get_key {
my $class = shift;

my $done = AnyEvent->condvar;
my $key;
my $got_key = sub { $key = shift; $done->send };

$class->async_get_key( @_, got_key => $got_key );

$done->recv;
return $key;
}

sub async_get_key {
my $class = shift;
my %param = shift;

my $email = $param{email} or return;
my $password = $param{password};
my $uri = KEY_URI;

my $body = build_body(
'user_session[email]' => $email,
'user_session[password]' => $password,
);
my %cbs = (
on_posted => sub {
my $key = shift;
$param{got_key}->($key);
},
);
$class->post_request($uri, $body, %cbs);
return;
}

sub build_body { sub build_body {
my ($key, $message) = @_; my %param = @_;
return join "&", map { uri_escape_utf8($_) } return
"user_credentials=$key", join "&", map {
"notification[message]=$message"; join "=", $_, uri_escape_utf8($param{$_})
}
keys %param;
} }




Expand All @@ -136,7 +181,13 @@ Net::AppNotifications - send notifications to your iPhone.
=head1 SYNOPSIS =head1 SYNOPSIS
use Net::AppNotifications; $notifier = Net::AppNotifications->new(
email => $registered_email,
password => $password,
);
# - or, prefered -
$notifier = Net::AppNotifications->new( $notifier = Net::AppNotifications->new(
key => $key, key => $key,
); );
Expand All @@ -156,6 +207,12 @@ Net::AppNotifications - send notifications to your iPhone.
); );
$sent->recv; $sent->recv;
## returns undef in case of error
$key = Net::AppNotifications->get_key(
email => $registered_email,
password => $password,
);
=head1 DESCRIPTION =head1 DESCRIPTION
Net::AppNotifications is a wrapper around appnotifications.com. It allows Net::AppNotifications is a wrapper around appnotifications.com. It allows
Expand Down

0 comments on commit cd13445

Please sign in to comment.