Class: ActivityMapper::TwitterServiceModule

Inherits:
ServiceModule show all
Defined in:
lib/activity_mapper/service_modules/twitter.rb

Constant Summary collapse

ACTIVITY_MAP =
{
  nil => {
    'activity.occurred_at'      => 'created_at',
    'activity.native_id'        => 'id',
    'activity_object.native_id' => 'id',
    'activity.caption'          => 'text',
    'activity_object.title'     => 'text',
    'activity_object.body'      => 'text'
  }
}
ACCEPTED_HOSTS =
[/twitter\.com/]

Constants inherited from ServiceModule

ServiceModule::COMMON_DIRECTIVES

Instance Method Summary collapse

Methods inherited from ServiceModule

accepts?, all_accepted_hosts, #deep_analysis_on, detect_username, #initialize, klass_for, #shallow_analysis_on, subclasses

Constructor Details

This class inherits a constructor from ActivityMapper::ServiceModule

Instance Method Details

#aggregate_activity!(options = {}) ⇒ Object

TODO use this data: tweet.favorited tweet.source # = what application was used to tweet (Wakoopa?) (tweet.in_reply_to_screen_name) (tweet.in_reply_to_status_id)



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/activity_mapper/service_modules/twitter.rb', line 40

def aggregate_activity!(options = {})
  mapper = ActivityDataMapper.new(ACTIVITY_MAP)
  mapper.fetch!("http://twitter.com/statuses/user_timeline/#{@profile.username}.json", :format => :json)
  mapper.map!
  mapper.entries.sort! { |e2,e1|
    e1['activity.occurred_at'] <=> e2['activity.occurred_at']
  }
  mapper.entries.each do |entry|
    entry['activity_object.url'] = entry['activity.url'] = "http://twitter.com/#{@profile.username}/status/#{entry['activity.native_id']}"
    break if Activity.exists?(@profile.user_id, entry)
    create_activity(entry, ActivityObjectType::STATUS, ActivityVerb::POST) do |activity, activity_object|
    end
  
  end
end

#create_or_update_summary!(options = {}) ⇒ Object

TODO use this data: user.name user.description user.location user.url



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/activity_mapper/service_modules/twitter.rb', line 22

def create_or_update_summary!(options = {})
  attributes = {}
  attributes[:username] = @profile.username || self.class.username_from_url(@profile.url)

  mapper = ActivityDataMapper.new(ACTIVITY_MAP)
  mapper.fetch!("http://twitter.com/statuses/user_timeline/#{attributes[:username]}.json", :format => :json)
  tweets = mapper.data

  attributes[:avatar_url] = tweets.blank? ? nil : tweets.first['user']['profile_image_url']
  attributes[:native_user_id] = tweets.first['user']['id'].to_i
  @profile.update_attributes(attributes)
end