Class: Twitter::REST::Request

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/twitter/rest/request.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

BASE_URL =
"https://api.twitter.com".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

flat_pmap, pmap

Constructor Details

#initialize(client, request_method, path, options = {}, params = nil) ⇒ Twitter::REST::Request

Parameters:

  • client (Twitter::Client)
  • request_method (String, Symbol)
  • path (String)
  • options (Hash) (defaults to: {})


26
27
28
29
30
31
32
33
34
35
# File 'lib/twitter/rest/request.rb', line 26

def initialize(client, request_method, path, options = {}, params = nil)
  @client = client
  @uri = Addressable::URI.parse(path.start_with?("http") ? path : BASE_URL + path)
  multipart_options = params || options
  set_multipart_options!(request_method, multipart_options)
  @path = uri.path
  @options = options
  @options_key = {get: :params, json_post: :json, json_put: :json, delete: :params}[request_method] || :form
  @params = params
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



17
18
19
# File 'lib/twitter/rest/request.rb', line 17

def client
  @client
end

#headersObject

Returns the value of attribute headers.



17
18
19
# File 'lib/twitter/rest/request.rb', line 17

def headers
  @headers
end

#optionsObject

Returns the value of attribute options.



17
18
19
# File 'lib/twitter/rest/request.rb', line 17

def options
  @options
end

#pathObject

Returns the value of attribute path.



17
18
19
# File 'lib/twitter/rest/request.rb', line 17

def path
  @path
end

#rate_limitObject

Returns the value of attribute rate_limit.



17
18
19
# File 'lib/twitter/rest/request.rb', line 17

def rate_limit
  @rate_limit
end

#request_methodObject Also known as: verb

Returns the value of attribute request_method.



17
18
19
# File 'lib/twitter/rest/request.rb', line 17

def request_method
  @request_method
end

#uriObject

Returns the value of attribute uri.



17
18
19
# File 'lib/twitter/rest/request.rb', line 17

def uri
  @uri
end

Instance Method Details

#performArray, Hash

Returns:

  • (Array, Hash)


38
39
40
41
42
43
# File 'lib/twitter/rest/request.rb', line 38

def perform
  response = http_client.headers(@headers).public_send(@request_method, @uri.to_s, request_options)
  response_body = response.body.empty? ? "" : symbolize_keys!(response.parse)
  response_headers = response.headers
  fail_or_return_response_body(response.code, response_body, response_headers)
end