Class: Atreides::Tweet

Inherits:
Base
  • Object
show all
Includes:
Extendable
Defined in:
app/models/atreides/tweet.rb

Class Method Summary collapse

Methods inherited from Base

#dom_id, #to_param

Class Method Details

.analytics(since = 1.month.ago) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/atreides/tweet.rb', line 18

def analytics(since = 1.month.ago)
  days = select("count(id) as twitter_id, sum(reach) as reach, DATE(tweeted_at) as tweeted_at").
    where("tweeted_at > ?", since).
    group("DATE(tweeted_at)").order(:twitter_id).map { |t|
      {
        :tweets => t.twitter_id,
        :reach => t.reach,
        :date => t.tweeted_at.to_date
      }
    }
end

.exposure(since = 1.month.ago) ⇒ Object



30
31
32
# File 'app/models/atreides/tweet.rb', line 30

def exposure(since = 1.month.ago)
  where("tweeted_at > ?", since).sum(:reach)
end

.fetch_recent(started_at = nil) ⇒ Object

Get most recent



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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/atreides/tweet.rb', line 39

def fetch_recent(started_at = nil)
  # Twitter mentions
  started_at ||= Time.zone.now
  continue = true
  users = []
  per_page = 100

  while continue do
    # Get most recent tweets
    tweets = twitter[:search].search.json?(
      :q => Settings.twitter.search_for,
      :since_id => Atreides::Tweet.maximum(:twitter_id),
      # :since => (Atreides::Tweet.maximum(:tweeted_at) || 1.month.ago).to_date.to_s,
      # :max_id => self.minimum(:twitter_id, :conditions => ["created_at >= ?", started_at]),
      :rpp => per_page,
      :lang => "all"
    ).results

    if continue = !tweets.size.zero?
      # Get user ids
      user_ids = tweets.map(&:from_user).uniq
      page = 0

      # Don't double fetch user-ids
      unless users.empty?
        user_ids -= users.map(&:screen_name).map(&:downcase)
      end

      continue_u = true
      while continue_u do
        sub_users = user_ids[page*per_page .. (page+1)*per_page] || []
        if continue_u = !sub_users.size.zero?
          page +=1
          results = twitter.users.lookup.json?(:screen_name => sub_users.join(','))
          # Append to list
          users += results
        end
      end

      # Add to DB
      tweets.each do |twt|
        u = users.detect{|u|u.screen_name.downcase==twt.from_user}
        self.create(
          :twitter_id => twt.id,
          :tweeted_at => Time.zone.parse(twt.created_at),
          :text => twt.text,
          :from_user => twt.from_user,
          :to_user => twt.to_user,
          :reach => (u ? u.followers_count : 0),
          :profile_image_url => twt.profile_image_url
        )
      end
    end

    # Return new tweets created
    where("created_at >= ?", started_at).count(:id)
  end
end

.reach(since = 1.month.ago) ⇒ Object



34
35
36
# File 'app/models/atreides/tweet.rb', line 34

def reach(since = 1.month.ago)
  select("distinct(from_user), reach").where("tweeted_at > ?", since).to_a.sum(&:reach)
end