Class: Whats::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/whats/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(token = nil, token_type = :bearer) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
# File 'lib/whats/client.rb', line 7

def initialize(token = nil, token_type = :bearer)
  @base_path = Whats.configuration.base_path
  @token = token || .token
  @token_type = token_type
end

Instance Method Details

#request(path, payload = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/whats/client.rb', line 13

def request(path, payload = nil)
  full_path = "#{base_path}#{path}"

  response = Typhoeus.post(
    full_path,
    headers: {
      "Authorization" => "#{token_name} #{token}",
      "Content-Type" => "application/json"
    },
    body: payload && payload.to_json
  )

  raise Whats::Errors::RequestError.new("API request error.", response) if response.failure?

  JSON.parse(response.response_body)
end