class Whats::Client

Attributes

base_path[R]
token[R]
token_type[R]

Public Class Methods

new(token = nil, token_type = :bearer) click to toggle source
# File lib/whats/client.rb, line 7
def initialize(token = nil, token_type = :bearer)
  @base_path = Whats.configuration.base_path
  @token = token || login.token
  @token_type = token_type
end

Public Instance Methods

request(path, payload = nil) click to toggle source
# 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

Private Instance Methods

login() click to toggle source
# File lib/whats/client.rb, line 43
def login
  Whats::Actions::Login.new
end
token_name() click to toggle source
# File lib/whats/client.rb, line 34
def token_name
  case token_type
  when :basic
    "Basic"
  when :bearer
    "Bearer"
  end
end