Class: Zazu::Client
- Inherits:
-
Object
- Object
- Zazu::Client
- 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
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#api_version ⇒ Object
readonly
Returns the value of attribute api_version.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#accounts ⇒ Object
Resource accessors — each returns a memoized resource module.
- #customers ⇒ Object
- #entity ⇒ Object
-
#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
constructor
A new instance of Client.
- #invoices ⇒ Object
- #payment_links ⇒ Object
-
#request(method, path, params: nil, body: nil, headers: {}) ⇒ Object
Performs an HTTP request and returns a Response on success.
- #webhook_endpoints ⇒ Object
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.
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_key ⇒ Object (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_version ⇒ Object (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_url ⇒ Object (readonly)
Returns the value of attribute base_url.
25 26 27 |
# File 'lib/zazu/client.rb', line 25 def base_url @base_url end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
25 26 27 |
# File 'lib/zazu/client.rb', line 25 def logger @logger end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
25 26 27 |
# File 'lib/zazu/client.rb', line 25 def timeout @timeout end |
Instance Method Details
#accounts ⇒ Object
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 |
#customers ⇒ Object
48 49 50 |
# File 'lib/zazu/client.rb', line 48 def customers @customers ||= Resources::Customers.new(self) end |
#entity ⇒ Object
52 53 54 |
# File 'lib/zazu/client.rb', line 52 def entity @entity ||= Resources::Entity.new(self) end |
#invoices ⇒ Object
56 57 58 |
# File 'lib/zazu/client.rb', line 56 def invoices @invoices ||= Resources::Invoices.new(self) end |
#payment_links ⇒ Object
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
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.}" rescue Faraday::ConnectionFailed => e raise ConnectionError, "Connection failed: #{e.}" end |
#webhook_endpoints ⇒ Object
64 65 66 |
# File 'lib/zazu/client.rb', line 64 def webhook_endpoints @webhook_endpoints ||= Resources::WebhookEndpoints.new(self) end |