Class: Coffrify::Client

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

Constant Summary collapse

DEFAULT_API_URL =
"https://api.coffrify.com".freeze
DEFAULT_TIMEOUT =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, api_url: DEFAULT_API_URL, workspace_id: nil, timeout: DEFAULT_TIMEOUT) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


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_keyObject (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_urlObject (readonly)

Returns the value of attribute api_url.



6
7
8
# File 'lib/coffrify/client.rb', line 6

def api_url
  @api_url
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



6
7
8
# File 'lib/coffrify/client.rb', line 6

def timeout
  @timeout
end

#workspace_idObject (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_keysObject



19
# File 'lib/coffrify/client.rb', line 19

def api_keys;  @api_keys  ||= ApiKeys.new(self);   end

#auditObject



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.message}"
  end
end

#transfersObject

── Resources ────────────────────────────────────────────────────────



17
# File 'lib/coffrify/client.rb', line 17

def transfers; @transfers ||= Transfers.new(self); end

#webhooksObject



18
# File 'lib/coffrify/client.rb', line 18

def webhooks;  @webhooks  ||= Webhooks.new(self);  end