Class: Reloop::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url: "https://reloop.sh") ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
# File 'lib/reloop/client.rb', line 7

def initialize(api_key:, base_url: "https://reloop.sh")
  raise ArgumentError, "Reloop SDK requires an api_key" if api_key.nil? || api_key.empty?

  @api_key = api_key
  @base_url = base_url
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



5
6
7
# File 'lib/reloop/client.rb', line 5

def api_key
  @api_key
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



5
6
7
# File 'lib/reloop/client.rb', line 5

def base_url
  @base_url
end

Instance Method Details

#api_keysObject



42
43
44
# File 'lib/reloop/client.rb', line 42

def api_keys
  @api_keys ||= Services::ApiKey.new(self)
end

#contactsObject



50
51
52
# File 'lib/reloop/client.rb', line 50

def contacts
  @contacts ||= Services::Contacts.new(self)
end

#domainObject



46
47
48
# File 'lib/reloop/client.rb', line 46

def domain
  @domain ||= Services::Domain.new(self)
end

#fetch(method, path, body = nil, params = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/reloop/client.rb', line 14

def fetch(method, path, body = nil, params = nil)
  conn = Faraday.new(url: @base_url) do |f|
    f.headers["x-api-key"] = @api_key
    f.headers["Content-Type"] = "application/json"
    f.headers["Accept"] = "application/json"
    f.adapter Faraday.default_adapter
  end

  response = conn.send(method) do |req|
    req.url path
    req.params = params if params
    req.body = body.to_json if body
  end

  unless response.success?
    error_body = begin
      JSON.parse(response.body)
    rescue StandardError
      {}
    end
    raise Error, "Reloop API Error: #{response.status} #{response.reason_phrase}. #{error_body}"
  end

  return {} if response.status == 204

  JSON.parse(response.body)
end

#mailObject



54
55
56
# File 'lib/reloop/client.rb', line 54

def mail
  @mail ||= Services::Mail.new(self)
end