class Bluepark::Client

Constants

STATUS_CODES

Attributes

bluepark_api_uri[RW]
bluepark_token[RW]
user_name[RW]

Public Class Methods

new(config = {}) click to toggle source
# File lib/bluepark/client.rb, line 20
def initialize(config = {})
  @user_name = config[:user_name]
  @bluepark_token = config[:bluepark_token]
  @bluepark_api_uri = config[:bluepark_api_uri]
end

Public Instance Methods

api_call_with_token(action, path, options = {}) click to toggle source
# File lib/bluepark/client.rb, line 47
def api_call_with_token(action, path, options = {})
  headers = { Authorization: "Basic #{token}" }
  headers.merge!(options[:headers]) if options[:headers].is_a?(Hash)
  headers[:Accept] = 'text/json'
  headers[:"content-type"] = 'application/json'
  args = ["#{@bluepark_api_uri}#{path}"]
  args << encode_json(options[:body]) unless options[:body].nil?
  args << headers

  begin
    response = RestClient.send(action, *args)
    decode_status(response)
  rescue RestClient::ExceptionWithResponse => e
    decode_status(e.response)
  end
end
company() click to toggle source
# File lib/bluepark/client.rb, line 101
def company
  rest_get_with_token('company')
end
decode_json(json) click to toggle source
# File lib/bluepark/client.rb, line 30
def decode_json(json)
  return Oj.load(json) if json != ''
  []
end
decode_status(response) click to toggle source
# File lib/bluepark/client.rb, line 35
def decode_status(response)
  if response.body.nil? || response.body.strip.empty? || response.code >= 300
    error = {
      status: STATUS_CODES[response.code],
      status_code: response.code
    }.merge(decode_json(response.body).to_h)
    raise BlueparkError, encode_json(error)
  else
    decode_json(response.body)
  end
end
encode_json(data) click to toggle source
# File lib/bluepark/client.rb, line 26
def encode_json(data)
  Oj.dump(data, mode: :compat)
end
orders() click to toggle source
# File lib/bluepark/client.rb, line 81
def orders
  Orders.new(self)
end
products() click to toggle source
# File lib/bluepark/client.rb, line 85
def products
  Products.new(self)
end
rest_delete_with_token(path, body = {}) click to toggle source
# File lib/bluepark/client.rb, line 77
def rest_delete_with_token(path, body = {})
  api_call_with_token(:delete, path, body)
end
rest_get_with_token(path, query_params = {}) click to toggle source
# File lib/bluepark/client.rb, line 64
def rest_get_with_token(path, query_params = {})
  headers = query_params.empty? ? nil : { params: query_params }
  api_call_with_token(:get, path, headers: headers)
end
rest_post_with_token(path, body = {}) click to toggle source
# File lib/bluepark/client.rb, line 73
def rest_post_with_token(path, body = {})
  api_call_with_token(:post, path, body: body)
end
rest_put_with_token(path, body = {}) click to toggle source
# File lib/bluepark/client.rb, line 69
def rest_put_with_token(path, body = {})
  api_call_with_token(:put, path, body: body)
end
sales(order_id) click to toggle source
# File lib/bluepark/client.rb, line 97
def sales(order_id)
  Sales.new(self, order_id)
end
skus() click to toggle source
# File lib/bluepark/client.rb, line 89
def skus
  Skus.new(self)
end
taxonomies() click to toggle source
# File lib/bluepark/client.rb, line 93
def taxonomies
  Taxonomies.new(self)
end

Private Instance Methods

token() click to toggle source
# File lib/bluepark/client.rb, line 107
def token
  Base64.encode64("#{@user_name}:#{@bluepark_token}").tr("\n", '')
end