class Lightspeed::Client

Attributes

access_token[R]
account_id[R]
configuration[R]

Public Class Methods

new(access_token:, account_id:, configuration: Lightspeed::Configuration.new) click to toggle source
# File lib/lightspeed/client.rb, line 26
def initialize(access_token:, account_id:, configuration: Lightspeed::Configuration.new)
  @access_token = access_token
  @account_id = account_id
  @configuration = configuration
end
tokens(client_id:, client_secret:, refresh_token:) click to toggle source
# File lib/lightspeed/client.rb, line 22
def self.tokens(client_id:, client_secret:, refresh_token:)
  API::Tokens.new(client_id, client_secret, refresh_token)
end

Public Instance Methods

accounts() click to toggle source
# File lib/lightspeed/client.rb, line 56
def accounts; API::Accounts.new(self); end
categories() click to toggle source
# File lib/lightspeed/client.rb, line 60
def categories; API::Categories.new(self); end
customers() click to toggle source
# File lib/lightspeed/client.rb, line 58
def customers; API::Customers.new(self); end
delete(url) click to toggle source
# File lib/lightspeed/client.rb, line 44
def delete(url)
  response = conn.delete(url)
  rate_limit_wait(response)
  response
end
get(url, params: {}, relations: []) click to toggle source
# File lib/lightspeed/client.rb, line 32
def get(url, params: {}, relations: [])
  response = conn.get(url, params.merge(load_relations: "[#{relation_names(relations)}]"))
  rate_limit_wait(response)
  response
end
items() click to toggle source
# File lib/lightspeed/client.rb, line 59
def items; API::Items.new(self); end
post(url, body: {}) click to toggle source
# File lib/lightspeed/client.rb, line 38
def post(url, body: {})
  response = conn.post(url, body)
  rate_limit_wait(response)
  response
end
put(url, body: {}) click to toggle source
# File lib/lightspeed/client.rb, line 50
def put(url, body: {})
  response = conn.put(url, body)
  rate_limit_wait(response)
  response
end
sales() click to toggle source
# File lib/lightspeed/client.rb, line 57
def sales; API::Sales.new(self); end

Private Instance Methods

conn() click to toggle source
# File lib/lightspeed/client.rb, line 66
def conn
  @conn ||= Faraday.new(Lightspeed.configuration.server) do |f|
    f.authorization :Bearer, access_token if access_token
    f.request :json
    f.response :json, :content_type => /\bjson$/
    f.adapter Faraday.default_adapter
  end
end
rate_limit_wait(response, points_expense: 10, points_threshold_percentage: 0.5) click to toggle source
# File lib/lightspeed/client.rb, line 81
def rate_limit_wait(response, points_expense: 10, points_threshold_percentage: 0.5)
  return unless Lightspeed.configuration.rate_limit || configuration.rate_limit

  bucket_level, bucket_limit = response.headers["x-ls-api-bucket-level"].split("/").map(&:to_f)
  drip_level = response.headers["x-ls-api-drip-rate"].to_f
  threshold = bucket_limit * points_threshold_percentage

  return if bucket_level + points_expense <= threshold

  points_over = bucket_level + points_expense - threshold
  wait_for_points = points_over / drip_level
  sleep(wait_for_points)
end
relation_names(relations) click to toggle source
# File lib/lightspeed/client.rb, line 75
def relation_names(relations)
  relations.map do |relation|
    "\"#{relation.to_s.camelize}\""
  end.join(",")
end