Module: ExTwitter::Utils
- Included in:
- Client
- Defined in:
- lib/ex_twitter/utils.rb
Constant Summary collapse
- PROFILE_SAVE_KEYS =
i( id name screen_name location description url protected followers_count friends_count listed_count favourites_count utc_offset time_zone geo_enabled verified statuses_count lang status profile_image_url_https profile_link_color suspended verified entities created_at )
- STATUS_SAVE_KEYS =
i( created_at id text source truncated coordinates place entities user contributors is_quote_status retweet_count favorite_count favorited retweeted possibly_sensitive lang )
Instance Method Summary collapse
- #__screen_name ⇒ Object
- #__uid ⇒ Object
- #__uid_i ⇒ Object
- #authenticating_user?(target) ⇒ Boolean
- #authorized_user?(target) ⇒ Boolean
- #call_old_method(method_name, *args) ⇒ Object
-
#collect_with_cursor(method_name, *args) ⇒ Object
friends, followers.
-
#collect_with_max_id(method_name, *args) ⇒ Object
user_timeline, search.
-
#decode_json(json_str, caller_name, options = {}) ⇒ Object
decode.
-
#encode_json(obj, caller_name, options = {}) ⇒ Object
encode.
- #fetch_cache_or_call_api(method_name, user, options = {}) ⇒ Object
- #file_cache_key(method_name, user, options = {}) ⇒ Object
- #instrument(operation, key, options = nil) ⇒ Object
- #namespaced_key(method_name, user, options = {}) ⇒ Object
-
#screen_name ⇒ Object
for backward compatibility.
-
#uid ⇒ Object
for backward compatibility.
- #uid_or_screen_name?(object) ⇒ Boolean
Instance Method Details
#__screen_name ⇒ Object
21 22 23 |
# File 'lib/ex_twitter/utils.rb', line 21 def __screen_name screen_name end |
#__uid ⇒ Object
8 9 10 |
# File 'lib/ex_twitter/utils.rb', line 8 def __uid uid end |
#__uid_i ⇒ Object
12 13 14 |
# File 'lib/ex_twitter/utils.rb', line 12 def __uid_i uid.to_i end |
#authenticating_user?(target) ⇒ Boolean
29 30 31 |
# File 'lib/ex_twitter/utils.rb', line 29 def authenticating_user?(target) user.id.to_i == user(target).id.to_i end |
#authorized_user?(target) ⇒ Boolean
33 34 35 36 |
# File 'lib/ex_twitter/utils.rb', line 33 def (target) target_user = user(target) !target_user.protected? || friendship?(user.id.to_i, target_user.id.to_i) end |
#call_old_method(method_name, *args) ⇒ Object
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/ex_twitter/utils.rb', line 44 def call_old_method(method_name, *args) = args. begin self.call_count += 1 = {method_name: method_name, call_count: self.call_count, args: args}.merge() instrument('api call', args[0], ) { send(method_name, *args, ) } rescue Twitter::Error::TooManyRequests => e logger.warn "#{__method__}: call=#{method_name} #{args.inspect} #{e.class} Retry after #{e.rate_limit.reset_in} seconds." raise e rescue Twitter::Error::ServiceUnavailable => e logger.warn "#{__method__}: call=#{method_name} #{args.inspect} #{e.class} #{e.message}" raise e rescue Twitter::Error::InternalServerError => e logger.warn "#{__method__}: call=#{method_name} #{args.inspect} #{e.class} #{e.message}" raise e rescue Twitter::Error::Forbidden => e logger.warn "#{__method__}: call=#{method_name} #{args.inspect} #{e.class} #{e.message}" raise e rescue Twitter::Error::NotFound => e logger.warn "#{__method__}: call=#{method_name} #{args.inspect} #{e.class} #{e.message}" raise e rescue => e logger.warn "#{__method__}: call=#{method_name} #{args.inspect} #{e.class} #{e.message}" raise e end end |
#collect_with_cursor(method_name, *args) ⇒ Object
friends, followers
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/ex_twitter/utils.rb', line 92 def collect_with_cursor(method_name, *args) = args. last_response = call_old_method(method_name, *args, ).attrs return_data = (last_response[:users] || last_response[:ids]) while (next_cursor = last_response[:next_cursor]) && next_cursor != 0 [:cursor] = next_cursor last_response = call_old_method(method_name, *args, ).attrs return_data += (last_response[:users] || last_response[:ids]) end return_data end |
#collect_with_max_id(method_name, *args) ⇒ Object
user_timeline, search
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ex_twitter/utils.rb', line 72 def collect_with_max_id(method_name, *args) = args. call_limit = .delete(:call_limit) || 3 last_response = call_old_method(method_name, *args, ) last_response = yield(last_response) if block_given? return_data = last_response call_count = 1 while last_response.any? && call_count < call_limit [:max_id] = last_response.last.kind_of?(Hash) ? last_response.last[:id] : last_response.last.id last_response = call_old_method(method_name, *args, ) last_response = yield(last_response) if block_given? return_data += last_response call_count += 1 end return_data.flatten end |
#decode_json(json_str, caller_name, options = {}) ⇒ Object
decode
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/ex_twitter/utils.rb', line 248 def decode_json(json_str, caller_name, = {}) obj = json_str.kind_of?(String) ? JSON.parse(json_str) : json_str result = case when obj.nil? obj when obj.kind_of?(Array) && obj.first.kind_of?(Hash) obj.map { |o| Hashie::Mash.new(o) } when obj.kind_of?(Array) && obj.first.kind_of?(Integer) obj when obj.kind_of?(Hash) Hashie::Mash.new(obj) when obj === true || obj === false obj when obj.kind_of?(Array) && obj.empty? obj else raise "#{__method__}: caller=#{caller_name} key=#{options[:key]} obj=#{obj.inspect}" end result end |
#encode_json(obj, caller_name, options = {}) ⇒ Object
encode
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/ex_twitter/utils.rb', line 195 def encode_json(obj, caller_name, = {}) [:reduce] = true unless .has_key?(:reduce) result = case caller_name when :user_timeline, :home_timeline, :mentions_timeline, :favorites # Twitter::Tweet JSON.pretty_generate(obj.map { |o| o.attrs }) when :search # Hash data = if [:reduce] obj.map { |o| o.to_hash.slice(*STATUS_SAVE_KEYS) } else obj.map { |o| o.to_hash } end JSON.pretty_generate(data) when :friends, :followers # Hash data = if [:reduce] obj.map { |o| o.to_hash.slice(*PROFILE_SAVE_KEYS) } else obj.map { |o| o.to_hash } end JSON.pretty_generate(data) when :friend_ids, :follower_ids # Integer JSON.pretty_generate(obj) when :user # Twitter::User JSON.pretty_generate(obj.to_hash.slice(*PROFILE_SAVE_KEYS)) when :users, :friends_parallelly, :followers_parallelly # Twitter::User data = if [:reduce] obj.map { |o| o.to_hash.slice(*PROFILE_SAVE_KEYS) } else obj.map { |o| o.to_hash } end JSON.pretty_generate(data) when :user? # true or false obj when :friendship? # true or false obj else raise "#{__method__}: caller=#{caller_name} key=#{options[:key]} obj=#{obj.inspect}" end result end |
#fetch_cache_or_call_api(method_name, user, options = {}) ⇒ Object
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/ex_twitter/utils.rb', line 276 def fetch_cache_or_call_api(method_name, user, = {}) key = namespaced_key(method_name, user, ) .update(key: key) data = if [:cache] == :read instrument('Cache Read(Force)', key, caller: method_name) { cache.read(key) } else cache.fetch(key, expires_in: 1.hour, race_condition_ttl: 5.minutes) do _d = yield instrument('serialize', key, caller: method_name) { encode_json(_d, method_name, ) } end end instrument('deserialize', key, caller: method_name) { decode_json(data, method_name, ) } end |
#file_cache_key(method_name, user, options = {}) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/ex_twitter/utils.rb', line 108 def file_cache_key(method_name, user, = {}) delim = ':' identifier = case when method_name == :search "str#{delim}#{user.to_s}" when method_name == :mentions_timeline "#{user.kind_of?(Integer) ? 'id' : 'sn'}#{delim}#{user.to_s}" when method_name == :home_timeline "#{user.kind_of?(Integer) ? 'id' : 'sn'}#{delim}#{user.to_s}" when method_name.in?([:users, :replying]) && [:super_operation].present? case when user.kind_of?(Array) && user.first.kind_of?(Integer) "#{options[:super_operation]}-ids#{delim}#{Digest::MD5.hexdigest(user.join(','))}" when user.kind_of?(Array) && user.first.kind_of?(String) "#{options[:super_operation]}-sns#{delim}#{Digest::MD5.hexdigest(user.join(','))}" else raise "#{method_name.inspect} #{user.inspect}" end when user.kind_of?(Integer) "id#{delim}#{user.to_s}" when user.kind_of?(Array) && user.first.kind_of?(Integer) "ids#{delim}#{Digest::MD5.hexdigest(user.join(','))}" when user.kind_of?(Array) && user.first.kind_of?(String) "sns#{delim}#{Digest::MD5.hexdigest(user.join(','))}" when user.kind_of?(String) "sn#{delim}#{user}" when user.kind_of?(Twitter::User) "user#{delim}#{user.id.to_s}" else raise "#{method_name.inspect} #{user.inspect}" end "#{method_name}#{delim}#{identifier}" end |
#instrument(operation, key, options = nil) ⇒ Object
38 39 40 41 42 |
# File 'lib/ex_twitter/utils.rb', line 38 def instrument(operation, key, = nil) payload = {operation: operation, key: key} payload.merge!() if .is_a?(Hash) ActiveSupport::Notifications.instrument('call.ex_twitter', payload) { yield(payload) } end |
#namespaced_key(method_name, user, options = {}) ⇒ Object
142 143 144 |
# File 'lib/ex_twitter/utils.rb', line 142 def namespaced_key(method_name, user, = {}) file_cache_key(method_name, user, ) end |
#screen_name ⇒ Object
for backward compatibility
17 18 19 |
# File 'lib/ex_twitter/utils.rb', line 17 def screen_name user.screen_name end |
#uid ⇒ Object
for backward compatibility
4 5 6 |
# File 'lib/ex_twitter/utils.rb', line 4 def uid user.id end |
#uid_or_screen_name?(object) ⇒ Boolean
25 26 27 |
# File 'lib/ex_twitter/utils.rb', line 25 def uid_or_screen_name?(object) object.kind_of?(String) || object.kind_of?(Integer) end |