Module: Sesame2::Api
Constant Summary collapse
- ENDPOINT_URL =
"https://app.candyhouse.co/api/sesame2"
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#client_id ⇒ Object
Returns the value of attribute client_id.
Instance Method Summary collapse
- #call(method, path, query = nil, body = nil) ⇒ Object
- #client ⇒ Object
- #get(path, query = nil) ⇒ Object
- #parse_response(response) ⇒ Object
- #post(path, query, body) ⇒ Object
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
8 9 10 |
# File 'lib/sesame2/api.rb', line 8 def api_key @api_key end |
#client_id ⇒ Object
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 |
#client ⇒ Object
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
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 |