Class: Sendable::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_id, api_key) ⇒ Client

Returns a new instance of Client.



10
11
12
13
# File 'lib/sendable/client.rb', line 10

def initialize(project_id, api_key)
  @project_id = project_id
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



8
9
10
# File 'lib/sendable/client.rb', line 8

def api_key
  @api_key
end

#project_idObject (readonly)

Returns the value of attribute project_id.



7
8
9
# File 'lib/sendable/client.rb', line 7

def project_id
  @project_id
end

Instance Method Details

#email(template_id, params = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sendable/client.rb', line 33

def email(template_id, params = {})
  uri = URI("https://api.sendable.io/v1/project/#{project_id}/template/#{template_id}/email")

  headers = {
    'Authorization': "ApiKey #{api_key}",
    'Content-Type' => 'application/json',
  }

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  request = Net::HTTP::Post.new(uri.request_uri, headers)
  request.body = params.respond_to?(:to_json) ? params.to_json : JSON.dump(params)
  response = http.request(request)

  JSON.parse(response.body)
end

#render(template_id, params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sendable/client.rb', line 15

def render(template_id, params = {})
  uri = URI("https://api.sendable.io/v1/project/#{project_id}/template/#{template_id}/render")

  headers = {
    'Authorization': "ApiKey #{api_key}",
    'Content-Type' => 'application/json',
  }

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  request = Net::HTTP::Post.new(uri.request_uri, headers)
  request.body = params.respond_to?(:to_json) ? params.to_json : JSON.dump(params)
  response = http.request(request)

  JSON.parse(response.body)
end