Sha256: fefdb2b38374ed97f1e0f5779d90591ca52a2f1fd6d44417aae205018cea8348

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

require "faraday"
require "faraday-cookie_jar"
require "singleton"

module Acumatica
  class Client
    include Singleton

    API_VERSION = "6.00.001"

    attr_accessor :url, :name, :password, :token, :debug

    def self.configure
      yield(instance)
      instance
    end

    def base_url
      @base_url ||= URI.join(url, "entity/Default/#{API_VERSION}/")
    end

    def connection
      @connection ||= Faraday.new do |conn|
        conn.request :json

        if token
          conn.request :oauth2, token
        else
          conn.use :cookie_jar
        end
        conn.use Acumatica::ErrorHandler

        conn.response :json
        conn.response :logger, nil, bodies: true if debug
        conn.adapter Faraday.default_adapter
      end
    end

    def session
      login
      result = yield
      logout
      result
    end

    def login
      response = connection.post do |req|
        req.url URI.join(@url, "/entity/auth/login")
        req.body = { name: @name, password: @password }
      end
      response.success?
    end

    def logout
      connection.post(URI.join(@url, "/entity/auth/logout")).success?
    end

    def customers
      Acumatica::Customer
    end

    def sales_orders
      Acumatica::SalesOrder
    end

    def stock_items
      Acumatica::StockItem
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acumatica-0.6.0 lib/acumatica/client.rb