Module: Birdwatcher::Concerns::Persistence

Included in:
Birdwatcher::Command, Module
Defined in:
lib/birdwatcher/concerns/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
# File 'lib/birdwatcher/concerns/persistence.rb', line 4

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#save_status(status, user) ⇒ Birdwatcher::Models::Status

Save a Twitter status to the database

The status will be linked to the current workspace. All URLs, hashtags and mentions will automatically be extracted and saved as separate models.

Parameters:

  • status (Twitter::Tweet)
  • user (Birdwatcher::Models::User)

    Author of status

Returns:

  • (Birdwatcher::Models::Status)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/birdwatcher/concerns/persistence.rb', line 20

def save_status(status, user)
  current_workspace = Birdwatcher::Console.instance.current_workspace
  db_status = current_workspace.add_status(
    :user_id            => user.id,
    :twitter_id         => status.id.to_s,
    :text               => Birdwatcher::Util.strip_control_characters(Birdwatcher::Util.unescape_html(status.text)),
    :source             => Birdwatcher::Util.strip_control_characters(Birdwatcher::Util.strip_html(status.source)),
    :retweet            => status.retweet?,
    :geo                => status.geo?,
    :favorite_count     => status.favorite_count,
    :retweet_count      => status.retweet_count,
    :possibly_sensitive => status.possibly_sensitive?,
    :lang               => status.lang,
    :posted_at          => status.created_at,
  )
  if status.geo? && status.geo.coordinates
    db_status.longitude = status.geo.coordinates.first
    db_status.latitude  = status.geo.coordinates.last
  end
  if status.place?
    db_status.place_type         = status.place.place_type
    db_status.place_name         = Birdwatcher::Util.strip_control_characters(status.place.name)
    db_status.place_country_code = Birdwatcher::Util.strip_control_characters(status.place.country_code)
    db_status.place_country      = Birdwatcher::Util.strip_control_characters(status.place.country)
  end
  db_status.save
  if status.hashtags?
    status.hashtags.each do |hashtag|
      tag = Birdwatcher::Util.strip_control_characters(hashtag.text)
      db_hashtag = current_workspace.hashtags_dataset.first(:tag => tag) || current_workspace.add_hashtag(:tag => tag)
      db_status.add_hashtag(db_hashtag)
    end
  end
  if status.user_mentions?
    status.user_mentions.each do |mention|
      screen_name = Birdwatcher::Util.strip_control_characters(mention.screen_name)
      name        = Birdwatcher::Util.strip_control_characters(mention.name)
      db_mention  = current_workspace.mentions_dataset.first(:twitter_id => mention.id.to_s) || current_workspace.add_mention(:twitter_id => mention.id.to_s, :screen_name => screen_name, :name => name)
      db_status.add_mention(db_mention)
    end
  end
  if status.urls?
    status.urls.each do |url|
      expanded_url = Birdwatcher::Util.strip_control_characters(url.expanded_url.to_s)
      db_url = current_workspace.urls_dataset.first(:url => expanded_url) || current_workspace.add_url(:url => expanded_url)
      db_status.add_url(db_url)
    end
  end
  db_status
end

#save_user(user) ⇒ Birdwatcher::Models::User

Save a Twitter user to the database

The user will be linked to the current workspace

Parameters:

  • user (Twitter::User)

Returns:

  • (Birdwatcher::Models::User)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/birdwatcher/concerns/persistence.rb', line 78

def save_user(user)
  Birdwatcher::Console.instance.current_workspace.add_user(
    :twitter_id        => user.id.to_s,
    :screen_name       => Birdwatcher::Util.strip_control_characters(user.screen_name),
    :name              => Birdwatcher::Util.strip_control_characters(user.name),
    :location          => Birdwatcher::Util.strip_control_characters(user.location),
    :description       => Birdwatcher::Util.strip_control_characters(user.description),
    :url               => (user.website_urls.first ? Birdwatcher::Util.strip_control_characters(user.website_urls.first.expanded_url.to_s) : nil),
    :profile_image_url => user.profile_image_url_https.to_s,
    :followers_count   => user.followers_count,
    :friends_count     => user.friends_count,
    :listed_count      => user.listed_count,
    :favorites_count   => user.favorites_count,
    :statuses_count    => user.statuses_count,
    :utc_offset        => user.utc_offset,
    :timezone          => user.time_zone,
    :geo_enabled       => user.geo_enabled?,
    :verified          => user.verified?,
    :lang              => user.lang
  )
end