Sha256: d2aa4dafeef07a341ab8b2ec84be0458eab97fcf15bd6ca5a8b883a3e4cf9875

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module FreshchatWhatsapp
  class Client
    def initialize(base_path=nil, token=nil, batoken_type = :bearer)
      @base_path = base_path ||  FreshchatWhatsapp.configuration.base_path
      @token = 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

2 entries across 2 versions & 1 rubygems

Version Path
freshchat_whatsapp-0.1.4 lib/freshchat_whatsapp/client.rb
freshchat_whatsapp-0.1.3 lib/freshchat_whatsapp/client.rb