Class: Zazu::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/zazu/client.rb

Overview

The main SDK entry point.

zazu = Zazu::Client.new(api_key: "sk_live_...")
zazu.entity.get
zazu.accounts.list(limit: 50)

All public state is set at construction time. The client is thread-safe in the sense that the underlying Faraday connection uses a connection pool via the HTTPX adapter — multiple threads can share one client.

Constant Summary collapse

DEFAULT_BASE_URL =
"https://zazu.ma"
DEFAULT_TIMEOUT =
30
USER_AGENT =
"zazu-ruby/#{VERSION}".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: ENV.fetch("ZAZU_API_KEY", nil), base_url: ENV.fetch("ZAZU_BASE_URL", DEFAULT_BASE_URL), api_version: ENV.fetch("ZAZU_API_VERSION", nil), timeout: Integer(ENV.fetch("ZAZU_TIMEOUT", DEFAULT_TIMEOUT)), logger: nil) ⇒ Client

Returns a new instance of Client.

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/zazu/client.rb', line 27

def initialize(
  api_key: ENV.fetch("ZAZU_API_KEY", nil),
  base_url: ENV.fetch("ZAZU_BASE_URL", DEFAULT_BASE_URL),
  api_version: ENV.fetch("ZAZU_API_VERSION", nil),
  timeout: Integer(ENV.fetch("ZAZU_TIMEOUT", DEFAULT_TIMEOUT)),
  logger: nil
)
  raise ConfigurationError, "Missing api_key. Pass api_key: or set ZAZU_API_KEY." if api_key.to_s.empty?

  @api_key = api_key
  @base_url = base_url.to_s.chomp("/")
  @api_version = api_version
  @timeout = timeout
  @logger = logger
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



25
26
27
# File 'lib/zazu/client.rb', line 25

def api_key
  @api_key
end

#api_versionObject (readonly)

Returns the value of attribute api_version.



25
26
27
# File 'lib/zazu/client.rb', line 25

def api_version
  @api_version
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



25
26
27
# File 'lib/zazu/client.rb', line 25

def base_url
  @base_url
end

#loggerObject (readonly)

Returns the value of attribute logger.



25
26
27
# File 'lib/zazu/client.rb', line 25

def logger
  @logger
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



25
26
27
# File 'lib/zazu/client.rb', line 25

def timeout
  @timeout
end

Instance Method Details

#accountsObject

Resource accessors — each returns a memoized resource module.



44
45
46
# File 'lib/zazu/client.rb', line 44

def accounts
  @accounts ||= Resources::Accounts.new(self)
end

#customersObject



48
49
50
# File 'lib/zazu/client.rb', line 48

def customers
  @customers ||= Resources::Customers.new(self)
end

#entityObject



52
53
54
# File 'lib/zazu/client.rb', line 52

def entity
  @entity ||= Resources::Entity.new(self)
end

#invoicesObject



56
57
58
# File 'lib/zazu/client.rb', line 56

def invoices
  @invoices ||= Resources::Invoices.new(self)
end


60
61
62
# File 'lib/zazu/client.rb', line 60

def payment_links
  @payment_links ||= Resources::PaymentLinks.new(self)
end

#request(method, path, params: nil, body: nil, headers: {}) ⇒ Object

Performs an HTTP request and returns a Response on success. Translates non-2xx responses into the matching Error subclass.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/zazu/client.rb', line 71

def request(method, path, params: nil, body: nil, headers: {})
  raw = connection.send(method) do |req|
    req.url(path)
    req.params.update(params) if params
    req.body = body unless body.nil?
    headers.each { |k, v| req.headers[k] = v }
  end

  response = Response.new(raw)
  return response if response.success?

  raise build_error(response)
rescue Faraday::TimeoutError => e
  raise ConnectionError, "Request timed out after #{timeout}s: #{e.message}"
rescue Faraday::ConnectionFailed => e
  raise ConnectionError, "Connection failed: #{e.message}"
end

#webhook_endpointsObject



64
65
66
# File 'lib/zazu/client.rb', line 64

def webhook_endpoints
  @webhook_endpoints ||= Resources::WebhookEndpoints.new(self)
end