| 1 | package Exobrain::Agent::Foursquare; | ||
| 2 | use Moose::Role; | ||
| 3 | use Method::Signatures; | ||
| 4 | use WWW::Mechanize; | ||
| 5 | |||
| 6 | with 'Exobrain::Agent'; | ||
| 7 | |||
| 8 | # ABSTRACT: Roles for Exobrain Foursquare agents | ||
| 9 | our $VERSION = '0.01'; # VERSION | ||
| 10 | |||
| 11 | |||
| 12 | sub component_name { "Foursquare" } | ||
| 13 | |||
| 14 | |||
| 15 | has mech => (is => 'ro', lazy => 1, builder => '_build_mech'); | ||
| 16 | sub _build_mech { WWW::Mechanize->new( stack_depth => 0, autocheck => 1 ); } | ||
| 17 | |||
| 18 | |||
| 19 | has auth_token => (is => 'ro', isa => 'Str', lazy => 1, builder => '_build_token'); | ||
| 20 | method _build_token() { $self->config->{auth_token} } | ||
| 21 | |||
| 22 | |||
| 23 | has api_base => (is => 'ro', isa => 'Str', default => 'https://api.foursquare.com/v2' ); | ||
| 24 | |||
| 25 | |||
| 26 | method foursquare_api($path, %args) { | ||
| 27 | my $base = $self->api_base; | ||
| 28 | |||
| 29 | my %full_args = ( | ||
| 30 | v => '20130425', | ||
| 31 | oauth_token => $self->auth_token, | ||
| 32 | %args | ||
| 33 | ); | ||
| 34 | |||
| 35 | # Provide a full set of arguments we can just append | ||
| 36 | # Eg: fooboz=42&v=20130425&oauth_token=... | ||
| 37 | my $arguments = join('&', map { "$_=$full_args{$_}" } keys %full_args ); | ||
| 38 | |||
| 39 | # Make our call, and decode the resulting JSON. | ||
| 40 | |||
| 41 | $self->mech->get("$base/$path?$arguments"); | ||
| 42 | return $self->json->decode( $self->mech->content ); | ||
| 43 | |||
| 44 | } | ||
| 45 | |||
| 46 | 1; | ||
| 47 | |||
| 48 | __END__ |