Sha256: 1a045b478e01b9244c27c703a2fd4494de4a91ac75c2c251799ab3f77fb7ce4a

Contents?: true

Size: 1.35 KB

Versions: 14

Compression:

Stored size: 1.35 KB

Contents

# typed: strict
# frozen_string_literal: true

module DearInventory
  class Request
    extend T::Sig

    sig do
      params(parameters: DearInventory::Models::Request).
        returns(DearInventory::Response)
    end
    def self.call(parameters)
      new(parameters).call
    end

    sig do
      params(parameters: DearInventory::Models::Request).void
    end
    def initialize(parameters)
      @parameters = parameters
    end

    sig { returns(DearInventory::Response) }
    def call
      options = request_params(@parameters.action, @parameters.params)
      response = HTTP.headers(headers).
        public_send(@parameters.action, @parameters.uri, options)

      DearInventory::Response.new(
        response: response,
        request: @parameters
      )
    end

    private

    sig { returns(T::Hash[Symbol, String]) }
    def headers
      {
        "Content-Type": "application/json",
        "api-auth-accountid": DearInventory.config.require(:account_id),
        "api-auth-applicationkey": DearInventory.config.require(:key),
      }
    end

    sig do
      params(action: Symbol, params: DearInventory::Parameters).
        returns(T::Hash[Symbol, T::Hash[Symbol, T.untyped]])
    end
    def request_params(action, params)
      if action == :get
        { params: params.to_h }
      else
        { json: params.to_h }
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
dear_inventory-0.7.5 lib/dear_inventory/lib/request.rb
dear_inventory-0.7.4 lib/dear_inventory/lib/request.rb
dear_inventory-0.7.3 lib/dear_inventory/lib/request.rb
dear_inventory-0.7.2 lib/dear_inventory/lib/request.rb
dear_inventory-0.7.1 lib/dear_inventory/lib/request.rb
dear_inventory-0.7.0 lib/dear_inventory/lib/request.rb
dear_inventory-0.6.3 lib/dear_inventory/lib/request.rb
dear_inventory-0.6.2 lib/dear_inventory/lib/request.rb
dear_inventory-0.6.1 lib/dear_inventory/lib/request.rb
dear_inventory-0.6.0 lib/dear_inventory/lib/request.rb
dear_inventory-0.5.0 lib/dear_inventory/lib/request.rb
dear_inventory-0.4.1 lib/dear_inventory/lib/request.rb
dear_inventory-0.4.0 lib/dear_inventory/lib/request.rb
dear_inventory-0.3.0 lib/dear_inventory/lib/request.rb