Class: Twitter::REST::Request

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

Overview

Handles HTTP requests to the Twitter API

Constant Summary collapse

BASE_URL =

The base URL for Twitter API requests

"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

Initializes a new Request

Examples:

Twitter::REST::Request.new(client, :get, "/1.1/statuses/home_timeline.json")

Parameters:

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


97
98
99
100
101
102
103
104
105
106
# File 'lib/twitter/rest/request.rb', line 97

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

Instance Attribute Details

#clientTwitter::Client

The client making the request

Examples:

request.client # => #<Twitter::REST::Client>

Returns:



26
27
28
# File 'lib/twitter/rest/request.rb', line 26

def client
  @client
end

#headersHash

The request headers

Examples:

request.headers # => {user_agent: "...", authorization: "..."}

Returns:

  • (Hash)


34
35
36
# File 'lib/twitter/rest/request.rb', line 34

def headers
  @headers
end

#optionsHash

The request options

Examples:

request.options # => {count: 10}

Returns:

  • (Hash)


42
43
44
# File 'lib/twitter/rest/request.rb', line 42

def options
  @options
end

#pathString

The request path

Examples:

request.path # => "/1.1/statuses/home_timeline.json"

Returns:

  • (String)


50
51
52
# File 'lib/twitter/rest/request.rb', line 50

def path
  @path
end

#rate_limitTwitter::RateLimit

The rate limit information from the response

Examples:

request.rate_limit # => #<Twitter::RateLimit>

Returns:



58
59
60
# File 'lib/twitter/rest/request.rb', line 58

def rate_limit
  @rate_limit
end

#request_methodSymbol

The HTTP request method

Examples:

request.request_method # => :get

Returns:

  • (Symbol)


66
67
68
# File 'lib/twitter/rest/request.rb', line 66

def request_method
  @request_method
end

#uriURI::Generic

The request URI

Examples:

request.uri # => #<URI::HTTPS>

Returns:

  • (URI::Generic)


74
75
76
# File 'lib/twitter/rest/request.rb', line 74

def uri
  @uri
end

Instance Method Details

#performArray, Hash

Performs the HTTP request and returns the response

Examples:

request.perform # => [{id: 123, text: "Hello"}]

Returns:

  • (Array, Hash)


114
115
116
117
118
# File 'lib/twitter/rest/request.rb', line 114

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

#verbSymbol

Returns the HTTP verb

Examples:

request.verb # => :get

Returns:

  • (Symbol)


82
83
84
# File 'lib/twitter/rest/request.rb', line 82

def verb
  request_method
end