Sha256: 702066391de45a015b098de6aa384d2f7ff31540d1b707ee886af8ac0a43ed63

Contents?: true

Size: 1.56 KB

Versions: 8

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

module Desertcart
  class Client
    include LedgerSync::Ledgers::Client::Mixin

    ROOT_URI = 'https://api.desertcart.com/api'

    REQUEST_HEADERS = { 'Accept' => 'application/vnd.api+json; version:3.0',
                        'Content-Type' => 'application/json' }.freeze

    attr_reader :user_id,
                :user_token,
                :store_id,
                :store_token

    def initialize(args = {})
      @user_id = args[:user_id]
      @user_token = args[:user_token]
      @store_id = args[:store_id]
      @store_token = args[:store_token]
    end

    def read(**keywords)
      request(keywords.merge(method: :get))
    end

    def create(**keywords)
      request(keywords.merge(method: :post))
    end

    def update(**keywords)
      request(keywords.merge(method: :put))
    end

    def auth_headers
      {
        'X-User-Id' => @user_id,
        'X-User-Token' => @user_token,
        'X-Store-Id' => @store_id,
        'X-Store-Token' => @store_token
      }
    end

    def request(args = {})
      request = LedgerSync::Ledgers::Request.new(
        body: args.fetch(:body, nil),
        headers: args.fetch(:headers, {})
          .merge(REQUEST_HEADERS)
          .merge(auth_headers),
        method: args.fetch(:method),
        url: url_from_path(path: args.fetch(:path)),
        params: args.fetch(:params, {})
      )

      request.perform
    end

    def url_from_path(path:)
      request_url = ROOT_URI
      request_url += '/' unless path.to_s.start_with?('/')
      request_url + path.to_s
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
desertcart-1.2.0 lib/desertcart/client.rb
desertcart-1.1.5 lib/desertcart/client.rb
desertcart-1.1.4 lib/desertcart/client.rb
desertcart-1.1.3 lib/desertcart/client.rb
desertcart-1.1.2 lib/desertcart/client.rb
desertcart-1.1.1 lib/desertcart/client.rb
desertcart-1.1.0 lib/desertcart/client.rb
desertcart-1.0.0 lib/desertcart/client.rb