Sha256: c414f399b40a506d24899c8ea753b919856bae462160b9ce8e74e40adfe10362

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

require 'faraday'

require_relative 'models/response'
require_relative 'models/product'
require_relative 'models/products'
require_relative 'models/order'
require_relative 'models/stock'
require_relative 'models/remote_code'

module Wegift
  class Client
    attr_accessor :api_host, :api_path, :api_key, :api_secret, :connection

    # supported: basic-http-auth - see: https://playground.wegift.io

    def initialize(options = {})
      @api_host = options[:api_host] || 'https://playground.wegift.io'
      @api_path = options[:api_path] || '/api/b2b-sync/v1'
      @api_key = options[:api_key].to_s
      @api_secret = options[:api_secret]

      @connection = Faraday.new(url: @api_host) do |c|
        c.basic_auth(@api_key, @api_secret)
        c.adapter Faraday.default_adapter
        unless options[:proxy].nil?
          c.options[:proxy] = {
            uri: URI(options[:proxy])
          }
        end
      end
    end

    def request(method, path, payload = {})
      @connection.send(method) do |req|
        req.url [@api_path, path].join
        req.headers['Content-Type'] = 'application/json'
        req.body = payload.to_json if method.to_sym.eql?(:post)
        req.params = payload if method.to_sym.eql?(:get)
      end
    end

    # TODO: shared context/connection for all calls
    # keep client => https://github.com/lostisland/faraday#basic-use

    # global methods

    def products
      products = Wegift::Products.new
      products.get(self)
    end

    def product(id = nil)
      products = Wegift::Product.new(product_code: id)
      products.get(self)
    end

    def order(options)
      order = Wegift::Order.new(options)
      order.post(self)
    end

    def stock(id)
      stock = Wegift::Stock.new(id: id)
      stock.get(self)
    end

    def remote_code(url)
      code = Wegift::RemoteCode.new(url: url)
      code.get(self)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wegift-ruby-client-1.10.2 lib/wegift/client.rb
wegift-ruby-client-1.10.0 lib/wegift/client.rb