Class: X::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/x/client.rb

Constant Summary collapse

DEFAULT_BASE_URL =
"https://api.twitter.com/2/".freeze
DEFAULT_ARRAY_CLASS =
Array
DEFAULT_OBJECT_CLASS =
Hash

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, api_key_secret: nil, access_token: nil, access_token_secret: nil, bearer_token: nil, base_url: DEFAULT_BASE_URL, open_timeout: Connection::DEFAULT_OPEN_TIMEOUT, read_timeout: Connection::DEFAULT_READ_TIMEOUT, write_timeout: Connection::DEFAULT_WRITE_TIMEOUT, debug_output: Connection::DEFAULT_DEBUG_OUTPUT, proxy_url: nil, default_array_class: DEFAULT_ARRAY_CLASS, default_object_class: DEFAULT_OBJECT_CLASS, max_redirects: RedirectHandler::DEFAULT_MAX_REDIRECTS) ⇒ Client

Returns a new instance of Client.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/x/client.rb', line 25

def initialize(api_key: nil, api_key_secret: nil, access_token: nil, access_token_secret: nil,
  bearer_token: nil,
  base_url: DEFAULT_BASE_URL,
  open_timeout: Connection::DEFAULT_OPEN_TIMEOUT,
  read_timeout: Connection::DEFAULT_READ_TIMEOUT,
  write_timeout: Connection::DEFAULT_WRITE_TIMEOUT,
  debug_output: Connection::DEFAULT_DEBUG_OUTPUT,
  proxy_url: nil,
  default_array_class: DEFAULT_ARRAY_CLASS,
  default_object_class: DEFAULT_OBJECT_CLASS,
  max_redirects: RedirectHandler::DEFAULT_MAX_REDIRECTS)

  initialize_oauth(api_key, api_key_secret, access_token, access_token_secret, bearer_token)
  initialize_authenticator
  @base_url = base_url
  initialize_default_classes(default_array_class, default_object_class)
  @connection = Connection.new(open_timeout: open_timeout, read_timeout: read_timeout,
    write_timeout: write_timeout, debug_output: debug_output, proxy_url: proxy_url)
  @request_builder = RequestBuilder.new
  @redirect_handler = RedirectHandler.new(connection: @connection, request_builder: @request_builder,
    max_redirects: max_redirects)
  @response_parser = ResponseParser.new
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



18
19
20
# File 'lib/x/client.rb', line 18

def access_token
  @access_token
end

#access_token_secretObject

Returns the value of attribute access_token_secret.



18
19
20
# File 'lib/x/client.rb', line 18

def access_token_secret
  @access_token_secret
end

#api_keyObject

Returns the value of attribute api_key.



18
19
20
# File 'lib/x/client.rb', line 18

def api_key
  @api_key
end

#api_key_secretObject

Returns the value of attribute api_key_secret.



18
19
20
# File 'lib/x/client.rb', line 18

def api_key_secret
  @api_key_secret
end

#base_urlObject

Returns the value of attribute base_url.



17
18
19
# File 'lib/x/client.rb', line 17

def base_url
  @base_url
end

#bearer_tokenObject

Returns the value of attribute bearer_token.



18
19
20
# File 'lib/x/client.rb', line 18

def bearer_token
  @bearer_token
end

#default_array_classObject

Returns the value of attribute default_array_class.



17
18
19
# File 'lib/x/client.rb', line 17

def default_array_class
  @default_array_class
end

#default_object_classObject

Returns the value of attribute default_object_class.



17
18
19
# File 'lib/x/client.rb', line 17

def default_object_class
  @default_object_class
end

Instance Method Details

#delete(endpoint, headers: {}, array_class: default_array_class, object_class: default_object_class) ⇒ Object



63
64
65
# File 'lib/x/client.rb', line 63

def delete(endpoint, headers: {}, array_class: default_array_class, object_class: default_object_class)
  execute_request(:delete, endpoint, headers: headers, array_class: array_class, object_class: object_class)
end

#get(endpoint, headers: {}, array_class: default_array_class, object_class: default_object_class) ⇒ Object



49
50
51
# File 'lib/x/client.rb', line 49

def get(endpoint, headers: {}, array_class: default_array_class, object_class: default_object_class)
  execute_request(:get, endpoint, headers: headers, array_class: array_class, object_class: object_class)
end

#post(endpoint, body = nil, headers: {}, array_class: default_array_class, object_class: default_object_class) ⇒ Object



53
54
55
56
# File 'lib/x/client.rb', line 53

def post(endpoint, body = nil, headers: {}, array_class: default_array_class, object_class: default_object_class)
  execute_request(:post, endpoint, body: body, headers: headers, array_class: array_class,
    object_class: object_class)
end

#put(endpoint, body = nil, headers: {}, array_class: default_array_class, object_class: default_object_class) ⇒ Object



58
59
60
61
# File 'lib/x/client.rb', line 58

def put(endpoint, body = nil, headers: {}, array_class: default_array_class, object_class: default_object_class)
  execute_request(:put, endpoint, body: body, headers: headers, array_class: array_class,
    object_class: object_class)
end