Sha256: 772203f9d75976f2ab37e9c485ea9ac3ffccb15037250ea8bb95346a420c3dd5

Contents?: true

Size: 984 Bytes

Versions: 1

Compression:

Stored size: 984 Bytes

Contents

# frozen_string_literal: true

require "whats/actions/login"

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

    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

    attr_reader :base_path, :token, :token_type

    def token_name
      case token_type
      when :basic
        "Basic"
      when :bearer
        "Bearer"
      end
    end

    def login
      Whats::Actions::Login.new
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
whatsapp-1.0.0 lib/whats/client.rb