Class: Coconut::API
- Inherits:
-
Object
- Object
- Coconut::API
- Defined in:
- lib/coconut/api.rb
Overview
Coconut::API is responsible for making API requests. It takes a Coconut::Client to send api_key, endpoint and region.
Direct Known Subclasses
Class Method Summary collapse
Class Method Details
.headers(cli) ⇒ Object
7 8 9 10 11 12 13 14 |
# File 'lib/coconut/api.rb', line 7 def self.headers(cli) if cli.api_key.nil? raise Coconut::Error, "You must specify an API key with Coconut.api_key=" end HTTP.basic_auth(user: cli.api_key, pass: ""). headers(:user_agent => "Coconut/v2 RubyBindings/#{Coconut::VERSION}") end |
.request(verb, path, options = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/coconut/api.rb', line 16 def self.request(verb, path, ={}) cli = [:client] || Coconut.default_client case verb when :get resp = headers(cli).get("#{cli.endpoint}#{path}") when :post resp = headers(cli).post("#{cli.endpoint}#{path}", json: [:json]) end if resp.code > 399 # if response is 400 or 401, we return the error message and error code if resp.code.between?(400, 401) raise Coconut::Error, "#{resp.parse["message"]} (code=#{resp.parse["error_code"]})" else raise Coconut::Error, "Server returned HTTP status #{resp.code}." end end return resp.parse end |