[cfca761] | 1 | package Facebook::Graph::Publish; |
---|
| 2 | BEGIN { |
---|
| 3 | $Facebook::Graph::Publish::VERSION = '1.0300'; |
---|
| 4 | } |
---|
| 5 | |
---|
| 6 | use Any::Moose; |
---|
| 7 | use Facebook::Graph::Response; |
---|
| 8 | with 'Facebook::Graph::Role::Uri'; |
---|
[9820d55] | 9 | use AnyEvent::HTTP; |
---|
| 10 | use HTTP::Request::Common; |
---|
[83e90de] | 11 | use URI::Encode qw(uri_decode uri_encode); |
---|
[cfca761] | 12 | |
---|
| 13 | has secret => ( |
---|
| 14 | is => 'ro', |
---|
| 15 | required => 0, |
---|
| 16 | predicate => 'has_secret', |
---|
| 17 | ); |
---|
| 18 | |
---|
| 19 | has access_token => ( |
---|
| 20 | is => 'ro', |
---|
| 21 | predicate => 'has_access_token', |
---|
| 22 | ); |
---|
| 23 | |
---|
| 24 | has object_name => ( |
---|
| 25 | is => 'rw', |
---|
| 26 | default => 'me', |
---|
| 27 | ); |
---|
| 28 | |
---|
| 29 | sub to { |
---|
| 30 | my ($self, $object_name) = @_; |
---|
| 31 | $self->object_name($object_name); |
---|
| 32 | return $self; |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | sub get_post_params { |
---|
| 36 | my $self = shift; |
---|
| 37 | my @post; |
---|
| 38 | if ($self->has_access_token) { |
---|
| 39 | push @post, access_token => uri_decode($self->access_token); |
---|
| 40 | } |
---|
| 41 | return \@post; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | sub publish { |
---|
[9820d55] | 45 | my ($self, $cb) = @_; |
---|
[cfca761] | 46 | my $uri = $self->uri; |
---|
| 47 | $uri->path($self->object_name.$self->object_path); |
---|
[83e90de] | 48 | my $request = POST($uri->as_string,$self->get_post_params); |
---|
| 49 | # XXX Do this more elegantly |
---|
[9820d55] | 50 | http_post $uri->as_string, $request->content, sub { |
---|
| 51 | my ($response, $headers) = @_; |
---|
| 52 | my %params = ( |
---|
| 53 | response => $response, |
---|
| 54 | headers => $headers, |
---|
| 55 | uri => $uri->as_string |
---|
| 56 | ); |
---|
| 57 | if ($self->has_secret) { |
---|
| 58 | $params{secret} = $self->secret; |
---|
| 59 | } |
---|
| 60 | $cb->(Facebook::Graph::Response->new(%params)); |
---|
[c104b43] | 61 | }; |
---|
| 62 | () # return nothing |
---|
[cfca761] | 63 | } |
---|
| 64 | |
---|
| 65 | no Any::Moose; |
---|
| 66 | __PACKAGE__->meta->make_immutable; |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | =head1 NAME |
---|
| 70 | |
---|
| 71 | Facebook::Graph::Publish - A base class for publishing various things to facebook. |
---|
| 72 | |
---|
| 73 | =head1 VERSION |
---|
| 74 | |
---|
| 75 | version 1.0300 |
---|
| 76 | |
---|
| 77 | =head1 DESCRIPTION |
---|
| 78 | |
---|
| 79 | This module shouldn't be used by you directly for any purpose. |
---|
| 80 | |
---|
| 81 | =head1 LEGAL |
---|
| 82 | |
---|
| 83 | Facebook::Graph is Copyright 2010 Plain Black Corporation (L<http://www.plainblack.com>) and is licensed under the same terms as Perl itself. |
---|
| 84 | |
---|
[9820d55] | 85 | =cut |
---|