module Twitter::REST::Utils

Constants

DEFAULT_CURSOR

Private Instance Methods

collect_users(users) click to toggle source
# File lib/twitter/rest/utils.rb, line 249
def collect_users(users) # rubocop:disable Metrics/MethodLength
  user_ids = []
  screen_names = []
  users.each do |user|
    case user
    when Integer               then user_ids << user
    when Twitter::User         then user_ids << user.id
    when String                then screen_names << user
    when URI, Addressable::URI then screen_names << user.path.split('/').last
    end
  end
  [user_ids, screen_names]
end
cursor_from_response_with_user(collection_name, klass, path, args) click to toggle source

@param collection_name [Symbol] @param klass [Class] @param path [String] @param args [Array] @return [Twitter::Cursor]

# File lib/twitter/rest/utils.rb, line 176
def cursor_from_response_with_user(collection_name, klass, path, args)
  arguments = Twitter::Arguments.new(args)
  merge_user!(arguments.options, arguments.pop || user_id) unless arguments.options[:user_id] || arguments.options[:screen_name]
  perform_get_with_cursor(path, arguments.options, collection_name, klass)
end
extract_id(object) click to toggle source

Take a URI string or Twitter::Identity object and return its ID

@param object [Integer, String, URI, Twitter::Identity] An ID, URI, or object. @return [Integer]

# File lib/twitter/rest/utils.rb, line 21
def extract_id(object)
  case object
  when ::Integer
    object
  when ::String
    object.split('/').last.to_i
  when URI, Addressable::URI
    object.path.split('/').last.to_i
  when Twitter::Identity
    object.id
  end
end
merge_default_cursor!(options) click to toggle source
# File lib/twitter/rest/utils.rb, line 190
def merge_default_cursor!(options)
  options[:cursor] = DEFAULT_CURSOR unless options[:cursor]
end
merge_user(hash, user, prefix = nil) click to toggle source

Take a user and merge it into the hash with the correct key

@param hash [Hash] @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object. @return [Hash]

# File lib/twitter/rest/utils.rb, line 199
def merge_user(hash, user, prefix = nil)
  merge_user!(hash.dup, user, prefix)
end
merge_user!(hash, user, prefix = nil) click to toggle source

Take a user and merge it into the hash with the correct key

@param hash [Hash] @param user [Integer, String, URI, Twitter::User] A Twitter user ID, screen name, URI, or object. @return [Hash]

# File lib/twitter/rest/utils.rb, line 208
def merge_user!(hash, user, prefix = nil)
  case user
  when Integer
    set_compound_key('user_id', user, hash, prefix)
  when String
    set_compound_key('screen_name', user, hash, prefix)
  when URI, Addressable::URI
    set_compound_key('screen_name', user.path.split('/').last, hash, prefix)
  when Twitter::User
    set_compound_key('user_id', user.id, hash, prefix)
  end
end
merge_users(hash, users) click to toggle source

Take a multiple users and merge them into the hash with the correct keys

@param hash [Hash] @param users [Enumerable<Integer, String, Twitter::User>] A collection of Twitter user IDs, screen_names, or objects. @return [Hash]

# File lib/twitter/rest/utils.rb, line 232
def merge_users(hash, users)
  copy = hash.dup
  merge_users!(copy, users)
  copy
end
merge_users!(hash, users) click to toggle source

Take a multiple users and merge them into the hash with the correct keys

@param hash [Hash] @param users [Enumerable<Integer, String, URI, Twitter::User>] A collection of Twitter user IDs, screen_names, URIs, or objects. @return [Hash]

# File lib/twitter/rest/utils.rb, line 243
def merge_users!(hash, users)
  user_ids, screen_names = collect_users(users)
  hash[:user_id] = user_ids.join(',') unless user_ids.empty?
  hash[:screen_name] = screen_names.join(',') unless screen_names.empty?
end
objects_from_response_with_user(klass, request_method, path, args) click to toggle source

@param klass [Class] @param request_method [Symbol] @param path [String] @param args [Array] @return [Array]

# File lib/twitter/rest/utils.rb, line 142
def objects_from_response_with_user(klass, request_method, path, args)
  arguments = Twitter::Arguments.new(args)
  merge_user!(arguments.options, arguments.pop)
  perform_request_with_objects(request_method, path, arguments.options, klass)
end
parallel_objects_from_response(klass, request_method, path, args) click to toggle source

@param klass [Class] @param request_method [Symbol] @param path [String] @param args [Array] @return [Array]

# File lib/twitter/rest/utils.rb, line 153
def parallel_objects_from_response(klass, request_method, path, args)
  arguments = Twitter::Arguments.new(args)
  pmap(arguments) do |object|
    perform_request_with_object(request_method, path, arguments.options.merge(id: extract_id(object)), klass)
  end
end
parallel_users_from_response(request_method, path, args) click to toggle source

@param request_method [Symbol] @param path [String] @param args [Array] @return [Array<Twitter::User>]

# File lib/twitter/rest/utils.rb, line 120
def parallel_users_from_response(request_method, path, args)
  arguments = Twitter::Arguments.new(args)
  pmap(arguments) do |user|
    perform_request_with_object(request_method, path, merge_user(arguments.options, user), Twitter::User)
  end
end
perform_get(path, options = {}) click to toggle source

@param path [String] @param options [Hash]

# File lib/twitter/rest/utils.rb, line 36
def perform_get(path, options = {})
  perform_request(:get, path, options)
end
perform_get_with_cursor(path, options, collection_name, klass = nil) click to toggle source

@param path [String] @param options [Hash] @param collection_name [Symbol] @param klass [Class]

# File lib/twitter/rest/utils.rb, line 104
def perform_get_with_cursor(path, options, collection_name, klass = nil)
  limit = options.delete(:limit)
  if options[:no_default_cursor]
    options.delete(:no_default_cursor)
  else
    merge_default_cursor!(options)
  end

  request = Twitter::REST::Request.new(self, :get, path, options)
  Twitter::Cursor.new(collection_name.to_sym, klass, request, limit)
end
perform_get_with_object(path, options, klass) click to toggle source

@param path [String] @param options [Hash] @param klass [Class]

# File lib/twitter/rest/utils.rb, line 56
def perform_get_with_object(path, options, klass)
  perform_request_with_object(:get, path, options, klass)
end
perform_get_with_objects(path, options, klass) click to toggle source

@param path [String] @param options [Hash] @param klass [Class]

# File lib/twitter/rest/utils.rb, line 79
def perform_get_with_objects(path, options, klass)
  perform_request_with_objects(:get, path, options, klass)
end
perform_post(path, options = {}) click to toggle source

@param path [String] @param options [Hash]

# File lib/twitter/rest/utils.rb, line 42
def perform_post(path, options = {})
  perform_request(:post, path, options)
end
perform_post_with_object(path, options, klass) click to toggle source

@param path [String] @param options [Hash] @param klass [Class]

# File lib/twitter/rest/utils.rb, line 63
def perform_post_with_object(path, options, klass)
  perform_request_with_object(:post, path, options, klass)
end
perform_post_with_objects(path, options, klass) click to toggle source

@param path [String] @param options [Hash] @param klass [Class]

# File lib/twitter/rest/utils.rb, line 86
def perform_post_with_objects(path, options, klass)
  perform_request_with_objects(:post, path, options, klass)
end
perform_request(request_method, path, options = {}, params = nil) click to toggle source

@param request_method [Symbol] @param path [String] @param options [Hash]

# File lib/twitter/rest/utils.rb, line 49
def perform_request(request_method, path, options = {}, params = nil)
  Twitter::REST::Request.new(self, request_method, path, options, params).perform
end
perform_request_with_object(request_method, path, options, klass, params = nil) click to toggle source

@param request_method [Symbol] @param path [String] @param options [Hash] @param klass [Class]

# File lib/twitter/rest/utils.rb, line 71
def perform_request_with_object(request_method, path, options, klass, params = nil)
  response = perform_request(request_method, path, options, params)
  klass.new(response)
end
perform_request_with_objects(request_method, path, options, klass) click to toggle source

@param request_method [Symbol] @param path [String] @param options [Hash] @param klass [Class]

# File lib/twitter/rest/utils.rb, line 94
def perform_request_with_objects(request_method, path, options, klass)
  perform_request(request_method, path, options).collect do |element|
    klass.new(element)
  end
end
perform_requests(request_method, path, ids) click to toggle source

@param request_method [Symbol] @param path [String] @param ids [Array] @return nil

# File lib/twitter/rest/utils.rb, line 164
def perform_requests(request_method, path, ids)
  ids.each do |id|
    perform_request(request_method, path, id: id)
  end
  nil
end
set_compound_key(key, value, hash, prefix = nil) click to toggle source
# File lib/twitter/rest/utils.rb, line 221
def set_compound_key(key, value, hash, prefix = nil)
  compound_key = [prefix, key].compact.join('_').to_sym
  hash[compound_key] = value
  hash
end
user_id() click to toggle source
# File lib/twitter/rest/utils.rb, line 182
def user_id
  @user_id ||= verify_credentials(skip_status: true).id
end
user_id?() click to toggle source
# File lib/twitter/rest/utils.rb, line 186
def user_id?
  instance_variable_defined?(:@user_id)
end
users_from_response(request_method, path, args) click to toggle source

@param request_method [Symbol] @param path [String] @param args [Array] @return [Array<Twitter::User>]

# File lib/twitter/rest/utils.rb, line 131
def users_from_response(request_method, path, args)
  arguments = Twitter::Arguments.new(args)
  merge_user!(arguments.options, arguments.pop || user_id) unless arguments.options[:user_id] || arguments.options[:screen_name]
  perform_request_with_objects(request_method, path, arguments.options, Twitter::User)
end