Sha256: 7a9128cbf9666751eb1c1e95af279f46b31d3ad53e5a2c1c9b3feb63d2770ef1

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

module FreshchatWhatsapp
  class Client
    def initialize(token_type = :bearer)
      @base_path = FreshchatWhatsapp.configuration.base_path
      @token = FreshchatWhatsapp.configuration.api_key
      @token_type = token_type
    end

    def request(path, payload = nil)
      full_path = "#{base_path}#{path}"
      conn = Faraday.new(url: full_path, headers: headers)
      response = conn.post { |request| request.body = body(payload) }
      JSON.parse(response.body)
    end

    def get_request(path, payload = nil)
      full_path = "#{base_path}#{path}"
      conn = Faraday.new(url: full_path, headers: headers)
      response = conn.get { |request| request.body = body(payload) }
      JSON.parse(response.body)
    end

    private

    attr_reader :base_path, :token, :token_type

    def token_name
      case token_type
      when :bearer
        'Bearer'
      end
    end

    def headers
      {
        'Authorization' => "#{token_name} #{token}",
        'Content-Type' => 'application/json'
      }
    end

    def body(payload)
      payload&.to_json
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
freshchat_whatsapp-0.1.2 lib/freshchat_whatsapp/client.rb