Module: Sesame2::Api

Included in:
Client, Sesame
Defined in:
lib/sesame2/api.rb

Constant Summary collapse

ENDPOINT_URL =
"https://app.candyhouse.co/api/sesame2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



8
9
10
# File 'lib/sesame2/api.rb', line 8

def api_key
  @api_key
end

#client_idObject

Returns the value of attribute client_id.



8
9
10
# File 'lib/sesame2/api.rb', line 8

def client_id
  @client_id
end

Instance Method Details

#call(method, path, query = nil, body = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/sesame2/api.rb', line 23

def call(method, path, query = nil, body = nil)
  response = client.send(method) do |req|
    req.url path
    req.headers["Content-Type"] = "application/json"
    req.headers["x-api-key"] = @api_key unless @api_key.nil?
    req.params = query unless query.nil?
    req.body = body.to_json unless body.nil?
  end
  parse_response(response)
end

#clientObject



12
13
14
# File 'lib/sesame2/api.rb', line 12

def client
  @client ||= Faraday.new(url: ENDPOINT_URL)
end

#get(path, query = nil) ⇒ Object



34
35
36
# File 'lib/sesame2/api.rb', line 34

def get(path, query = nil)
  call(:get, path, query, nil)
end

#parse_response(response) ⇒ Object

Raises:



16
17
18
19
20
21
# File 'lib/sesame2/api.rb', line 16

def parse_response(response)
  parsed_response = response.headers["Content-Length"].to_i.positive? ? JSON.parse(response.body) : ""
  raise Error.new(response.status, parsed_response) if response.status >= 400

  parsed_response
end

#post(path, query, body) ⇒ Object



38
39
40
# File 'lib/sesame2/api.rb', line 38

def post(path, query, body)
  call(:post, path, query, body)
end