source: perl/modules/Facebook/lib/Facebook/Graph/Publish/Post.pm

release-1.10release-1.9
Last change on this file was 2a42248, checked in by Edward Z. Yang <ezyang@mit.edu>, 13 years ago
Add vanilla Facebook::Graph. Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
  • Property mode set to 100644
File size: 10.5 KB
RevLine 
[2a42248]1package Facebook::Graph::Publish::Post;
2BEGIN {
3  $Facebook::Graph::Publish::Post::VERSION = '1.0300';
4}
5
6use Any::Moose;
7extends 'Facebook::Graph::Publish';
8
9use constant object_path => '/feed';
10
11has message => (
12    is          => 'rw',
13    predicate   => 'has_message',
14);
15
16sub set_message {
17    my ($self, $message) = @_;
18    $self->message($message);
19    return $self;
20}
21
22has picture_uri => (
23    is          => 'rw',
24    predicate   => 'has_picture_uri',
25);
26
27sub set_picture_uri {
28    my ($self, $picture) = @_;
29    $self->picture_uri($picture);
30    return $self;
31}
32
33
34has link_uri => (
35    is          => 'rw',
36    predicate   => 'has_link_uri',
37);
38
39sub set_link_uri {
40    my ($self, $source) = @_;
41    $self->link_uri($source);
42    return $self;
43}
44
45
46has link_name => (
47    is          => 'rw',
48    predicate   => 'has_link_name',
49);
50
51sub set_link_name {
52    my ($self, $source) = @_;
53    $self->link_name($source);
54    return $self;
55}
56
57
58has link_caption => (
59    is          => 'rw',
60    predicate   => 'has_link_caption',
61);
62
63sub set_link_caption {
64    my ($self, $source) = @_;
65    $self->link_caption($source);
66    return $self;
67}
68
69
70has link_description => (
71    is          => 'rw',
72    predicate   => 'has_link_description',
73);
74
75sub set_link_description {
76    my ($self, $source) = @_;
77    $self->link_description($source);
78    return $self;
79}
80
81has target_countries => (
82    is          => 'rw',
83    default     => sub {[]},
84    lazy        => 1,
85    isa         => 'ArrayRef',
86    predicate   => 'has_target_countries',
87);
88
89sub set_target_countries {
90    my ($self, $source) = @_;
91    $self->target_countries($source);
92    return $self;
93}
94
95has target_cities => (
96    is          => 'rw',
97    default     => sub {[]},
98    lazy        => 1,
99    isa         => 'ArrayRef',
100    predicate   => 'has_target_cities',
101);
102
103sub set_target_cities {
104    my ($self, $source) = @_;
105    $self->target_cities($source);
106    return $self;
107}
108
109has target_regions => (
110    is          => 'rw',
111    default     => sub {[]},
112    lazy        => 1,
113    isa         => 'ArrayRef',
114    predicate   => 'has_target_regions',
115);
116
117sub set_target_regions {
118    my ($self, $source) = @_;
119    $self->target_regions($source);
120    return $self;
121}
122
123has target_locales => (
124    is          => 'rw',
125    default     => sub {[]},
126    lazy        => 1,
127    isa         => 'ArrayRef',
128    predicate   => 'has_target_locales',
129);
130
131sub set_target_locales {
132    my ($self, $source) = @_;
133    $self->target_locales($source);
134    return $self;
135}
136
137has source => (
138    is          => 'rw',
139    predicate   => 'has_source',
140);
141
142sub set_source {
143    my ($self, $source) = @_;
144    $self->source($source);
145    return $self;
146}
147
148has actions => (
149    is          => 'rw',
150    default     => sub {[]},
151    lazy        => 1,
152    isa         => 'ArrayRef',
153    predicate   => 'has_actions',
154);
155
156sub set_actions {
157    my ($self, $actions) = @_;
158    $self->actions($actions);
159    return $self;
160}
161
162sub add_action {
163    my ($self, $name, $link) = @_;
164    my $actions = $self->actions;
165    push @$actions, { name => $name, link => $link };
166    $self->actions($actions);
167    return $self;
168}
169
170has privacy => (
171    is          => 'rw',
172    predicate   => 'has_privacy',
173);
174
175has privacy_options => (
176    is          => 'rw',
177    isa         => 'HashRef',
178    default     => sub {{}},
179);
180
181sub set_privacy {
182    my ($self, $privacy, $options) = @_;
183    $self->privacy($privacy);
184    $self->privacy_options($options);
185    return $self;
186}
187
188has 'properties' => (
189    is          => 'rw',
190    predicate   => 'has_properties'
191);
192
193sub set_properties {
194    my $self = shift;
195    my $properties = @_ % 2 ? shift @_ : { @_ };
196    $self->properties($properties);
197    return $self;
198}
199
200around get_post_params => sub {
201    my ($orig, $self) = @_;
202    my $post = $orig->($self);
203    if ($self->has_message) {
204        push @$post, message => $self->message;
205    }
206    if ($self->has_link_uri) {
207        push @$post, link => $self->link_uri;
208    }
209    if ($self->has_link_name) {
210        push @$post, name => $self->link_name;
211    }
212    if ($self->has_link_caption) {
213        push @$post, caption => $self->link_caption;
214    }
215    if ($self->has_link_description) {
216        push @$post, description => $self->link_description;
217    }
218    if ($self->has_picture_uri) {
219        push @$post, picture => $self->picture_uri;
220    }
221    if ($self->has_source) {
222        push @$post, source => $self->source;
223    }
224    if ($self->has_actions) {
225        foreach my $action (@{$self->actions}) {
226            push @$post, actions => JSON->new->encode($action);
227        }
228    }
229    if ($self->has_privacy) {
230        my %privacy = %{$self->privacy_options};
231        $privacy{value} = $self->privacy;
232        push @$post, privacy => JSON->new->encode(\%privacy);
233    }
234    if ($self->has_properties) {
235        push @$post, properties => JSON->new->encode($self->properties);
236    }
237    return $post;
238};
239
240
241no Any::Moose;
242__PACKAGE__->meta->make_immutable;
243
244
245=head1 NAME
246
247Facebook::Graph::Publish::Post - Publish to a user's wall.
248
249=head1 VERSION
250
251version 1.0300
252
253=head1 SYNOPSIS
254
255 my $fb = Facebook::Graph->new;
256
257 $fb->add_post
258    ->set_message('I like beer.')
259    ->publish;
260
261 my $response = $fb->add_post
262    ->set_message('I like Perl.')
263    ->set_picture_uri('http://www.perl.org/i/camel_head.png')
264    ->set_link_uri('http://www.perl.org/')
265    ->set_link_name('Perl.org')
266    ->set_link_caption('Perl is a programming language.')
267    ->set_link_description('A link to the Perl web site.')
268    ->publish;
269
270
271=head1 DESCRIPTION
272
273This module gives you quick and easy access to publish to a user's Facebook feed.
274
275B<ATTENTION:> You must have the C<publish_stream> privilege to use this module.
276
277B<TIP:> Facebook seems to use these terms interchangibly: Feed, Post, News, Wall. So if you want to publish to a user's wall, this is the mechanism you use to do that.
278
279=head1 METHODS
280
281=head2 to ( id )
282
283Specify a profile id to post to. Defaults to 'me', which is the currently logged in user.
284
285
286=head2 set_message ( message )
287
288Sets the text to post to the wall.
289
290=head3 message
291
292A string of text.
293
294
295=head2 set_picture_uri ( uri )
296
297Sets the URI of a picture to be displayed in the message.
298
299=head3 uri
300
301A URI to a picture.
302
303
304=head2 set_link_uri ( uri )
305
306Sets the URI of a link that viewers can click on for more information about whatever you posted in C<set_message>.
307
308=head3 uri
309
310A URI to a site.
311
312
313=head2 set_link_name ( name )
314
315If you want to give the link you set in C<set_link_uri> a human friendly name, use this method.
316
317=head2 name
318
319A text string to be used as the name of the link.
320
321
322=head2 set_link_caption ( caption )
323
324Sets a short blurb to be displayed below the link/picture.
325
326=head3 caption
327
328A text string.
329
330
331=head2 set_link_description ( description )
332
333Sets a longer description of the site you're linking to. Can also be a portion of the article you're linking to.
334
335=head3 description
336
337A text string.
338
339
340=head2 set_source ( uri )
341
342Sets a source URI for a flash or video file.
343
344=head3 uri
345
346The URI you wish to set. For example if you wanted to include a YouTube video:
347
348 $post->set_source('http://www.youtube.com/watch?v=efsJRdJ6dog');
349
350
351=head2 set_actions ( actions )
352
353Sets a list of actions (or links) that should go in the post as things people can do with the post. This allows for integration of the post with third party sites.
354
355=head3 actions
356
357An array reference of hash references containing C<name> / C<link> pairs.
358
359 $post->actions([
360    {
361        name    => 'Click Me!',
362        link    => 'http://www.somesite.com/click',
363    },
364    ...
365 ]);
366
367=head2 add_action ( name, link )
368
369Adds an action to the list of actions set either by previously calling C<add_action> or by calling C<set_actions>.
370
371=head3 name
372
373The name of the action (the clickable label).
374
375=head3 link
376
377The URI of the action.
378
379
380
381=head2 set_privacy ( setting, options )
382
383A completely optional privacy setting.
384
385=head2 set_properties ( properties )
386
387"property" values assigned when the post is published. This is typically rendered as a list of links.
388
389    $post->set_properties( { "search engine:" => { "text" => "Google", "href" => "http://www.google.com/" } } );
390
391=head3 setting
392
393The privacy setting. Choose from: EVERYONE, CUSTOM, ALL_FRIENDS, NETWORKS_FRIENDS, and FRIENDS_OF_FRIENDS. See L<http://developers.facebook.com/docs/reference/api/post> for changes in this list.
394
395=head3 options
396
397A hash reference of options to tweak the privacy setting. Some options are required depending on what privacy setting you chose. See L<http://developers.facebook.com/docs/reference/api/post> for details.
398
399 $post->set_privacy('CUSTOM', { friends => 'SOME_FRIENDS', allow => [qw( 119393 993322 )] });
400
401=over
402
403=item friends
404
405A string that must be one of EVERYONE, NETWORKS_FRIENDS, FRIENDS_OF_FRIENDS, ALL_FRIENDS, SOME_FRIENDS, SELF, or NO_FRIENDS.
406
407=item networks
408
409An array reference of network ids.
410
411=item allow
412
413An array reference of user ids.
414
415=item deny.
416
417An array reference of user ids.
418
419=back
420
421
422=head2 set_target_countries ( countries )
423
424Makes a post only available to viewers in certain countries.
425
426 $post->set_target_countries( ['US'] );
427 
428=head3 countries
429
430An array reference of two letter country codes (upper case). You can find a list of country codes in the list of city ids here: L<http://developers.facebook.com/attachment/all_cities_final.csv>.
431
432
433=head2 set_target_regions ( regions )
434
435Makes a post only available to viewers in certain regions.
436
437 $post->set_target_regions( [6,53] );
438 
439=head3 regions
440
441An array reference of region numbers however Facebook defines that. I've got no idea because their documentation sucks. I'm not even sure what a region is. Is it a region of a country? Of a continent? No idea. I do know it is an integer, but that's about it.
442
443
444
445=head2 set_target_cities ( cities )
446
447Makes a post only available to viewers in certain cities.
448
449 $post->set_target_cities( [2547804] );
450 
451=head3 cities
452
453An array reference of cities ids. In the example above I've listed Madison, WI. You can find a list of their cities here: L<http://developers.facebook.com/attachment/all_cities_final.csv>
454
455
456
457=head2 set_target_locales ( locales )
458
459Makes a post only available to viewers in certain locales.
460
461 $post->set_target_locales( [6] );
462 
463=head3 locales
464
465An array reference of locales ids. You can find their list of locales here: L<http://developers.facebook.com/attachment/locales_final.csv>
466
467
468
469=head2 publish ( )
470
471Posts the data and returns a L<Facebook::Graph::Response> object. The response object should contain the id:
472
473 {"id":"1647395831_130068550371568"}
474
475=head1 LEGAL
476
477Facebook::Graph is Copyright 2010 Plain Black Corporation (L<http://www.plainblack.com>) and is licensed under the same terms as Perl itself.
478
479=cut
Note: See TracBrowser for help on using the repository browser.