| 1 | package Exobrain::Agent::Foursquare::Source; | ||
| 2 | use Moose; | ||
| 3 | use Method::Signatures; | ||
| 4 | |||
| 5 | with 'Exobrain::Agent::Foursquare'; | ||
| 6 | with 'Exobrain::Agent::Poll'; | ||
| 7 | |||
| 8 | use constant DEBUG => 0; | ||
| 9 | use constant LAST_CHECK => 'last_check'; | ||
| 10 | |||
| 11 | # ABSTRACT: Send foursquare events to the exobrain bus | ||
| 12 | our $VERSION = '0.01'; # VERSION | ||
| 13 | |||
| 14 | # last_check is an attribute that's backed by our cache. | ||
| 15 | # TODO: It would be cool if Exobrain::Agent supplied a new keyword | ||
| 16 | # for setting up these sorts of attributes. | ||
| 17 | |||
| 18 | has last_check => ( | ||
| 19 | is => 'rw', | ||
| 20 | isa => 'Int', | ||
| 21 | trigger => \&_last_check_update, | ||
| 22 | builder => '_build_last_check', | ||
| 23 | ); | ||
| 24 | |||
| 25 | method _build_last_check() { | ||
| 26 | return $self->cache->compute(LAST_CHECK, undef, sub { time() } ); | ||
| 27 | } | ||
| 28 | |||
| 29 | method _last_check_update($value!, ...) { | ||
| 30 | $self->cache->set(LAST_CHECK, $value); | ||
| 31 | } | ||
| 32 | |||
| 33 | method poll() { | ||
| 34 | # Record when we're making our call. We do this before we make our | ||
| 35 | # API call, because it can be laggy, and we want to know when we | ||
| 36 | # started the API call, not when it returned. | ||
| 37 | my $checktime = time(); | ||
| 38 | |||
| 39 | my $last_check = $self->last_check; | ||
| 40 | |||
| 41 | my $checkins = $self->foursquare_api('checkins/recent', | ||
| 42 | afterTimestamp => $last_check, | ||
| 43 | )->{response}{recent}; | ||
| 44 | |||
| 45 | # Checkins come in most-recent first, so we reverse them and make | ||
| 46 | # them chronological. | ||
| 47 | |||
| 48 | foreach my $checkin (reverse @$checkins) { | ||
| 49 | my $name = ( $checkin->{user}{firstName} // "" ) . " " | ||
| 50 | . ( $checkin->{user}{lastName} // "" ); | ||
| 51 | |||
| 52 | my $time = localtime($checkin->{createdAt}); | ||
| 53 | |||
| 54 | my $user = $checkin->{user}{id}; | ||
| 55 | |||
| 56 | warn "Displaying checkin at $checkin->{venue}{name} by $name\n" | ||
| 57 | if DEBUG; | ||
| 58 | |||
| 59 | $self->exobrain->measure('Geo::Foursquare', | ||
| 60 | checkin => $checkin, | ||
| 61 | ); | ||
| 62 | } | ||
| 63 | |||
| 64 | # Finally, update our check-time. | ||
| 65 | $self->last_check( $checktime ); | ||
| 66 | |||
| 67 | } | ||
| 68 | |||
| 69 | 1; | ||
| 70 | |||
| 71 | __END__ |