Class: Coffrify::Client
- Inherits:
-
Object
- Object
- Coffrify::Client
- Defined in:
- lib/coffrify/client.rb
Constant Summary collapse
- DEFAULT_API_URL =
"https://api.coffrify.com".freeze
- DEFAULT_TIMEOUT =
30
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#api_url ⇒ Object
readonly
Returns the value of attribute api_url.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#workspace_id ⇒ Object
readonly
Returns the value of attribute workspace_id.
Instance Method Summary collapse
- #api_keys ⇒ Object
- #audit ⇒ Object
-
#initialize(api_key:, api_url: DEFAULT_API_URL, workspace_id: nil, timeout: DEFAULT_TIMEOUT) ⇒ Client
constructor
A new instance of Client.
-
#request(method, path, body: nil, query: nil, headers: {}) ⇒ Object
── HTTP plumbing ────────────────────────────────────────────────────.
-
#transfers ⇒ Object
── Resources ────────────────────────────────────────────────────────.
- #webhooks ⇒ Object
Constructor Details
#initialize(api_key:, api_url: DEFAULT_API_URL, workspace_id: nil, timeout: DEFAULT_TIMEOUT) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 14 |
# File 'lib/coffrify/client.rb', line 8 def initialize(api_key:, api_url: DEFAULT_API_URL, workspace_id: nil, timeout: DEFAULT_TIMEOUT) raise ArgumentError, "api_key must start with 'cfy_'" unless api_key.is_a?(String) && api_key.start_with?("cfy_") @api_key = api_key @api_url = api_url @workspace_id = workspace_id @timeout = timeout end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
6 7 8 |
# File 'lib/coffrify/client.rb', line 6 def api_key @api_key end |
#api_url ⇒ Object (readonly)
Returns the value of attribute api_url.
6 7 8 |
# File 'lib/coffrify/client.rb', line 6 def api_url @api_url end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
6 7 8 |
# File 'lib/coffrify/client.rb', line 6 def timeout @timeout end |
#workspace_id ⇒ Object (readonly)
Returns the value of attribute workspace_id.
6 7 8 |
# File 'lib/coffrify/client.rb', line 6 def workspace_id @workspace_id end |
Instance Method Details
#api_keys ⇒ Object
19 |
# File 'lib/coffrify/client.rb', line 19 def api_keys; @api_keys ||= ApiKeys.new(self); end |
#audit ⇒ Object
20 |
# File 'lib/coffrify/client.rb', line 20 def audit; @audit ||= Audit.new(self); end |
#request(method, path, body: nil, query: nil, headers: {}) ⇒ Object
── HTTP plumbing ────────────────────────────────────────────────────
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/coffrify/client.rb', line 23 def request(method, path, body: nil, query: nil, headers: {}) uri = URI.parse("#{api_url}/v1#{path}") uri.query = URI.encode_www_form(query) if query && !query.empty? http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme == "https" http.read_timeout = timeout http.open_timeout = timeout req = build_request(method, uri, body, headers) attempts = 0 begin attempts += 1 resp = http.request(req) handle_response(resp) rescue Net::OpenTimeout, Net::ReadTimeout, IOError, Errno::ECONNRESET => e if attempts < 3 sleep(0.5 * (2 ** (attempts - 1))) retry end raise Error, "Network error after #{attempts} attempts: #{e.}" end end |