twitter_with_auto_pagination 0.8.8 → 0.8.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6d767009c6d195d95d8ef07d1f7dae2d2c7ebe23
4
- data.tar.gz: 8a6b624c9ec6ad652aa8613ca8f99f24e20ce368
3
+ metadata.gz: 1c58523412b513e538fc1b184026dbe03a3c8d66
4
+ data.tar.gz: 7ec3c80b9615c90a04d01a39b1550afda3d9115d
5
5
  SHA512:
6
- metadata.gz: 0511c5f3a28c8a2d70a5372bac83c11ee54667928726e4eded3c762a3efc5c31c7bc6d0fc08a75275f0456997342ad37a48079f6960d70b78ca881564220d068
7
- data.tar.gz: aa4a35b82774fc8e74c5ea619e68a815ccfdd2700680c686d1bd46042600da9f3a5897371a2a714585f222fa23faeb37f35f321fc4bf8fa59bdb1affdf0456de
6
+ metadata.gz: 6258c081d6c2745cbc5e917e9f4e39886eca6fb1b8aa2aad9e2f91e6c2a7b77555092a15eba50fc8279223251614e62b3ec4e04c85dc8ae610c29584ee89f837
7
+ data.tar.gz: fa959db4a3e3eec7e9a43501185d42a1e7f39807ffc7cd9b2177928c92909db660aa00029bf4203f32e1cfd4b708630fb6e509fdf059a5f4ca9317e78f731e5e
@@ -5,77 +5,131 @@ module TwitterWithAutoPagination
5
5
  module Uncategorized
6
6
  include TwitterWithAutoPagination::REST::Utils
7
7
 
8
- def usage_stats_wday_series_data(times)
9
- wday_count = times.each_with_object((0..6).map { |n| [n, 0] }.to_h) do |time, memo|
10
- memo[time.wday] += 1
11
- end
12
- wday_count.map { |k, v| [I18n.t('date.abbr_day_names')[k], v] }.map do |key, value|
13
- {name: key, y: value, drilldown: key}
8
+ EVERY_DAY = (0..6)
9
+ WDAY_COUNT = EVERY_DAY.map { |n| [n, 0] }.to_h
10
+ WDAY_NIL_COUNT = EVERY_DAY.map { |n| [n, nil] }.to_h
11
+
12
+ EVERY_HOUR = (0..23)
13
+ HOUR_COUNT = EVERY_HOUR.map { |n| [n, 0] }.to_h
14
+ HOUR_NIL_COUNT = EVERY_HOUR.map { |n| [n, nil] }.to_h
15
+
16
+ def count_wday(times)
17
+ times.each_with_object(WDAY_COUNT.dup) { |time, memo| memo[time.wday] += 1 }
18
+ end
19
+
20
+ def count_hour(times)
21
+ times.each_with_object(HOUR_COUNT.dup) { |time, memo| memo[time.hour] += 1 }
22
+ end
23
+
24
+ # [
25
+ # {:name=>"Sun", :y=>111, :drilldown=>"Sun"},
26
+ # {:name=>"Mon", :y=>95, :drilldown=>"Mon"},
27
+ # {:name=>"Tue", :y=>72, :drilldown=>"Tue"},
28
+ # {:name=>"Wed", :y=>70, :drilldown=>"Wed"},
29
+ # {:name=>"Thu", :y=>73, :drilldown=>"Thu"},
30
+ # {:name=>"Fri", :y=>81, :drilldown=>"Fri"},
31
+ # {:name=>"Sat", :y=>90, :drilldown=>"Sat"}
32
+ # ]
33
+ def usage_stats_wday_series_data(times, day_names:)
34
+ count_wday(times).map do |wday, count|
35
+ {name: day_names[wday], y: count, drilldown: day_names[wday]}
14
36
  end
15
37
  end
16
38
 
17
- def usage_stats_wday_drilldown_series(times)
18
- hour_count =
19
- (0..6).each_with_object((0..6).map { |n| [n, nil] }.to_h) do |wday, wday_memo|
20
- wday_memo[wday] =
21
- 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|
22
- hour_memo[hour] += 1
23
- end
39
+ # [
40
+ # {
41
+ # :name=>"Sun",
42
+ # :id=>"Sun",
43
+ # :data=> [ ["0", 7], ["1", 12], ... , ["22", 10], ["23", 12] ]
44
+ # },
45
+ # ...
46
+ # {
47
+ # :name=>"Mon",
48
+ # :id=>"Mon",
49
+ # :data=> [ ["0", 22], ["1", 11], ... , ["22", 9], ["23", 14] ]
50
+ # }
51
+ def usage_stats_wday_drilldown_series(times, day_names:)
52
+ counts =
53
+ EVERY_DAY.each_with_object(WDAY_NIL_COUNT.dup) do |wday, memo|
54
+ memo[wday] = count_hour(times.select { |t| t.wday == wday })
24
55
  end
25
- hour_count.map { |k, v| [I18n.t('date.abbr_day_names')[k], v] }.map do |key, value|
26
- {name: key, id: key, data: value.to_a.map{|a| [a[0].to_s, a[1]] }}
56
+
57
+ counts.map { |wday, hour_count| [day_names[wday], hour_count] }.map do |wday, hour_count|
58
+ {name: wday, id: wday, data: hour_count.map { |hour, count| [hour.to_s, count] }}
27
59
  end
28
60
  end
29
61
 
62
+ # [
63
+ # {:name=>"0", :y=>66, :drilldown=>"0"},
64
+ # {:name=>"1", :y=>47, :drilldown=>"1"},
65
+ # ...
66
+ # {:name=>"22", :y=>73, :drilldown=>"22"},
67
+ # {:name=>"23", :y=>87, :drilldown=>"23"}
68
+ # ]
30
69
  def usage_stats_hour_series_data(times)
31
- hour_count = times.each_with_object((0..23).map { |n| [n, 0] }.to_h) do |time, memo|
32
- memo[time.hour] += 1
33
- end
34
- hour_count.map do |key, value|
35
- {name: key.to_s, y: value, drilldown: key.to_s}
70
+ count_hour(times).map do |hour, count|
71
+ {name: hour.to_s, y: count, drilldown: hour.to_s}
36
72
  end
37
73
  end
38
74
 
39
- def usage_stats_hour_drilldown_series(times)
40
- wday_count =
41
- (0..23).each_with_object((0..23).map { |n| [n, nil] }.to_h) do |hour, hour_memo|
42
- hour_memo[hour] =
43
- 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|
44
- wday_memo[wday] += 1
45
- end
75
+ # [
76
+ # {:name=>"0", :id=>"0", :data=>[["Sun", 7], ["Mon", 22], ["Tue", 8], ["Wed", 9], ["Thu", 9], ["Fri", 6], ["Sat", 5]]},
77
+ # {:name=>"1", :id=>"1", :data=>[["Sun", 12], ["Mon", 11], ["Tue", 5], ["Wed", 5], ["Thu", 0], ["Fri", 8], ["Sat", 6]]},
78
+ # ...
79
+ # ]
80
+ def usage_stats_hour_drilldown_series(times, day_names:)
81
+ counts =
82
+ EVERY_HOUR.each_with_object(HOUR_NIL_COUNT.dup) do |hour, memo|
83
+ memo[hour] = count_wday(times.select { |t| t.hour == hour })
46
84
  end
47
- wday_count.map do |key, value|
48
- {name: key.to_s, id: key.to_s, data: value.to_a.map{|a| [I18n.t('date.abbr_day_names')[a[0]], a[1]] }}
85
+
86
+ counts.map do |hour, wday_count|
87
+ {name: hour.to_s, id: hour.to_s, data: wday_count.map { |wday, count| [day_names[wday], count] }}
49
88
  end
50
89
  end
51
90
 
52
- def twitter_addiction_series(times)
53
- five_mins = 5.minutes
54
- wday_expended_seconds =
55
- (0..6).each_with_object((0..6).map { |n| [n, nil] }.to_h) do |wday, wday_memo|
91
+ # [
92
+ # {:name=>"Sun", :y=>14.778310502283107},
93
+ # {:name=>"Mon", :y=>12.273439878234399},
94
+ # {:name=>"Tue", :y=>10.110578386605784},
95
+ # {:name=>"Wed", :y=>9.843683409436835},
96
+ # {:name=>"Thu", :y=>10.547945205479452},
97
+ # {:name=>"Fri", :y=>10.61773211567732},
98
+ # {:name=>"Sat", :y=>12.115753424657534}
99
+ # ]
100
+ def twitter_addiction_series(times, day_names:)
101
+ max_duration = 5.minutes
102
+ wday_count =
103
+ EVERY_DAY.each_with_object(WDAY_NIL_COUNT.dup) do |wday, memo|
56
104
  target_times = times.select { |t| t.wday == wday }
57
- wday_memo[wday] = target_times.empty? ? nil : target_times.each_cons(2).map {|a, b| (a - b) < five_mins ? a - b : five_mins }.sum
105
+ memo[wday] =
106
+ if target_times.empty?
107
+ nil
108
+ else
109
+ target_times.each_cons(2).map { |newer, older| (newer - older) < max_duration ? newer - older : max_duration }.sum
110
+ end
58
111
  end
59
- days = times.map{|t| t.to_date.to_s(:long) }.uniq.size
60
- weeks = (days > 7) ? days / 7.0 : 1.0
61
- wday_expended_seconds.map { |k, v| [I18n.t('date.abbr_day_names')[k], (v.nil? ? nil : v / weeks / 60)] }.map do |key, value|
62
- {name: key, y: value}
112
+ days = times.map { |t| t.to_date.to_s(:long) }.uniq.size
113
+ weeks = [days / 7.0, 1.0].max
114
+ wday_count.map do |wday, seconds|
115
+ {name: day_names[wday], y: (seconds.nil? ? nil : seconds / weeks / 60)}
63
116
  end
64
117
  end
65
118
 
66
119
  def usage_stats(user, options = {})
67
120
  n_days_ago = options.has_key?(:days) ? options[:days].days.ago : 100.years.ago
68
121
  tweets = options.has_key?(:tweets) ? options.delete(:tweets) : user_timeline(user)
122
+ day_names = options.has_key?(:day_names) ? options.delete(:day_names) : %w(Sun Mon Tue Wed Thu Fri Sat)
69
123
  times =
70
124
  # TODO Use user specific time zone
71
125
  tweets.map { |t| ActiveSupport::TimeZone['Tokyo'].parse(t.created_at.to_s) }.
72
126
  select { |t| t > n_days_ago }
73
127
  [
74
- usage_stats_wday_series_data(times),
75
- usage_stats_wday_drilldown_series(times),
128
+ usage_stats_wday_series_data(times, day_names: day_names),
129
+ usage_stats_wday_drilldown_series(times, day_names: day_names),
76
130
  usage_stats_hour_series_data(times),
77
- usage_stats_hour_drilldown_series(times),
78
- twitter_addiction_series(times)
131
+ usage_stats_hour_drilldown_series(times, day_names: day_names),
132
+ twitter_addiction_series(times, day_names: day_names)
79
133
  ]
80
134
  end
81
135
  end
@@ -22,5 +22,5 @@ Gem::Specification.new do |spec|
22
22
  spec.required_ruby_version = '>= 2.3'
23
23
  spec.summary = spec.description
24
24
  spec.test_files = Dir.glob('spec/**/*')
25
- spec.version = '0.8.8'
25
+ spec.version = '0.8.9'
26
26
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twitter_with_auto_pagination
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.8
4
+ version: 0.8.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shinohara Teruki