Class: ExTwitter::Client

Inherits:
Twitter::REST::Client
  • Object
show all
Includes:
ExistingApi, NewApi, Utils
Defined in:
lib/ex_twitter/client.rb

Constant Summary collapse

INDENT =
4

Constants included from Utils

Utils::PROFILE_SAVE_KEYS, Utils::STATUS_SAVE_KEYS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NewApi

#_count_users_with_two_sided_threshold, #_extract_favorite_users, #_extract_inactive_users, #_extract_screen_names, #_extract_uids, #_extract_users, #_fetch_parallelly, #close_friends, #clusters_assigned_to, #clusters_belong_to, #common_followers, #common_friends, #favorited_by, #favoriting, #followers_parallelly, #friends_and_followers, #friends_followers_and_statuses, #friends_parallelly, #inactive_followers, #inactive_friends, #mutual_friends, #one_sided_followers, #one_sided_following, #removed, #removing, #replied, #replying

Methods included from ExistingApi

#favorites, #follower_ids, #followers, #friend_ids, #friends, #friendship?, #home_timeline, #mentions_timeline, #search, #user, #user?, #user_timeline, #users, #verify_credentials

Methods included from Utils

#__screen_name, #__uid, #__uid_i, #authenticating_user?, #authorized_user?, #call_old_method, #collect_with_cursor, #collect_with_max_id, #decode_json, #encode_json, #fetch_cache_or_call_api, #file_cache_key, #instrument, #namespaced_key, #screen_name, #uid, #uid_or_screen_name?

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ex_twitter/client.rb', line 16

def initialize(options = {})
  @cache = ActiveSupport::Cache::FileStore.new(File.join('tmp', 'api_cache'))
  @call_count = 0

  @uid = options.has_key?(:uid) ? options.delete(:uid).to_i : nil
  @screen_name = options.has_key?(:screen_name) ? options.delete(:screen_name).to_s : nil

  @@logger = @logger =
    if options[:logger]
      options.delete(:logger)
    else
      Dir.mkdir('log') unless File.exists?('log')
      Logger.new('log/ex_twitter.log')
    end

  super
end

Instance Attribute Details

#authenticated_userObject (readonly)

Returns the value of attribute authenticated_user.



39
40
41
# File 'lib/ex_twitter/client.rb', line 39

def authenticated_user
  @authenticated_user
end

#cacheObject (readonly)

Returns the value of attribute cache.



39
40
41
# File 'lib/ex_twitter/client.rb', line 39

def cache
  @cache
end

#call_countObject

Returns the value of attribute call_count.



38
39
40
# File 'lib/ex_twitter/client.rb', line 38

def call_count
  @call_count
end

#loggerObject (readonly)

Returns the value of attribute logger.



39
40
41
# File 'lib/ex_twitter/client.rb', line 39

def logger
  @logger
end

Class Method Details

.loggerObject



34
35
36
# File 'lib/ex_twitter/client.rb', line 34

def self.logger
  @@logger
end

Instance Method Details

#twitter_addiction_series(times) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ex_twitter/client.rb', line 107

def twitter_addiction_series(times)
  five_mins = 5.minutes
  wday_expended_seconds =
    (0..6).each_with_object((0..6).map { |n| [n, nil] }.to_h) do |wday, wday_memo|
      target_times = times.select { |t| t.wday == wday }
      wday_memo[wday] = target_times.empty? ? nil : target_times.each_cons(2).map {|a, b| (a - b) < five_mins ? a - b : five_mins }.sum
    end
  days = times.map{|t| t.to_date.to_s(:long) }.uniq.size
  weeks = (days > 7) ? days / 7.0 : 1.0
  wday_expended_seconds.map { |k, v| [I18n.t('date.abbr_day_names')[k], (v.nil? ? nil : v / weeks / 60)] }.map do |key, value|
    {name: key, y: value}
  end
end

#usage_stats(user, options = {}) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ex_twitter/client.rb', line 121

def usage_stats(user, options = {})
  n_days_ago = options.has_key?(:days) ? options[:days].days.ago : 100.years.ago
  tweets = options.has_key?(:tweets) ? options.delete(:tweets) : user_timeline(user)
  times =
    # TODO Use user specific time zone
    tweets.map { |t| ActiveSupport::TimeZone['Tokyo'].parse(t.created_at.to_s) }.
      select { |t| t > n_days_ago }
  [
    usage_stats_wday_series_data(times),
    usage_stats_wday_drilldown_series(times),
    usage_stats_hour_series_data(times),
    usage_stats_hour_drilldown_series(times),
    twitter_addiction_series(times)
  ]
end

#usage_stats_hour_drilldown_series(times) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ex_twitter/client.rb', line 94

def usage_stats_hour_drilldown_series(times)
  wday_count =
    (0..23).each_with_object((0..23).map { |n| [n, nil] }.to_h) do |hour, hour_memo|
      hour_memo[hour] =
        times.select { |t| t.hour == hour }.map { |t| t.wday }.each_with_object((0..6).map { |n| [n, 0] }.to_h) do |wday, wday_memo|
          wday_memo[wday] += 1
        end
    end
  wday_count.map do |key, value|
    {name: key.to_s, id: key.to_s, data: value.to_a.map{|a| [I18n.t('date.abbr_day_names')[a[0]], a[1]] }}
  end
end

#usage_stats_hour_series_data(times) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/ex_twitter/client.rb', line 85

def usage_stats_hour_series_data(times)
  hour_count = times.each_with_object((0..23).map { |n| [n, 0] }.to_h) do |time, memo|
    memo[time.hour] += 1
  end
  hour_count.map do |key, value|
    {name: key.to_s, y: value, drilldown: key.to_s}
  end
end

#usage_stats_wday_drilldown_series(times) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ex_twitter/client.rb', line 72

def usage_stats_wday_drilldown_series(times)
  hour_count =
    (0..6).each_with_object((0..6).map { |n| [n, nil] }.to_h) do |wday, wday_memo|
      wday_memo[wday] =
        times.select { |t| t.wday == wday }.map { |t| t.hour }.each_with_object((0..23).map { |n| [n, 0] }.to_h) do |hour, hour_memo|
          hour_memo[hour] += 1
        end
    end
  hour_count.map { |k, v| [I18n.t('date.abbr_day_names')[k], v] }.map do |key, value|
    {name: key, id: key, data: value.to_a.map{|a| [a[0].to_s, a[1]] }}
  end
end

#usage_stats_wday_series_data(times) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/ex_twitter/client.rb', line 63

def usage_stats_wday_series_data(times)
  wday_count = times.each_with_object((0..6).map { |n| [n, 0] }.to_h) do |time, memo|
    memo[time.wday] += 1
  end
  wday_count.map { |k, v| [I18n.t('date.abbr_day_names')[k], v] }.map do |key, value|
    {name: key, y: value, drilldown: key}
  end
end