class Manifestly::Client
Constants
- DEFAULT_API_VERSION
- DEFAULT_URL
Attributes
api_key[R]
api_version[R]
url[R]
Public Class Methods
new(url: ENV['MANIFESTLY_URL'], api_version: ENV['MANIFESTLY_API_VERSION'], api_key: ENV['MANIFESTLY_API_KEY'])
click to toggle source
# File lib/manifestly/http/client.rb, line 10 def initialize(url: ENV['MANIFESTLY_URL'], api_version: ENV['MANIFESTLY_API_VERSION'], api_key: ENV['MANIFESTLY_API_KEY']) @url = url || DEFAULT_URL @api_version = api_version || DEFAULT_API_VERSION @api_key = api_key || raise('api key is required') end
Public Instance Methods
delete(path, params: {}, headers: {})
click to toggle source
# File lib/manifestly/http/client.rb, line 48 def delete(path, params: {}, headers: {}) handle_request { raw_delete("#{url}/#{api_version}/#{path}", params: params, headers: headers) } end
get(path, params: {}, headers: {})
click to toggle source
# File lib/manifestly/http/client.rb, line 16 def get(path, params: {}, headers: {}) handle_request { raw_get("#{url}/#{api_version}/#{path}", params: params, headers: headers) } end
post(path, params: {}, headers: {})
click to toggle source
# File lib/manifestly/http/client.rb, line 26 def post(path, params: {}, headers: {}) handle_request { raw_post("#{url}/#{api_version}/#{path}", params: params, headers: headers) } end
put(path, params: {}, headers: {})
click to toggle source
# File lib/manifestly/http/client.rb, line 37 def put(path, params: {}, headers: {}) handle_request { raw_put("#{url}/#{api_version}/#{path}", params: params, headers: headers) } end
Private Instance Methods
handle_request() { || ... }
click to toggle source
# File lib/manifestly/http/client.rb, line 70 def handle_request short_response = trim_response(yield) case short_response[:status] when 404 raise Faraday::Error::ResourceNotFound, short_response when 400...600 raise Faraday::Error::ClientError, short_response end short_response end
include_api_key!(params)
click to toggle source
# File lib/manifestly/http/client.rb, line 58 def include_api_key!(params) params.merge!(api_key: api_key) end
include_json_accept!(headers)
click to toggle source
# File lib/manifestly/http/client.rb, line 66 def include_json_accept!(headers) headers.merge!('Accept': 'application/json') end
include_json_content_type!(headers)
click to toggle source
# File lib/manifestly/http/client.rb, line 62 def include_json_content_type!(headers) headers.merge!('Content-Type': 'application/json') end
raw_delete(path, params: {}, headers: {})
click to toggle source
# File lib/manifestly/http/client.rb, line 52 def raw_delete(path, params: {}, headers: {}) include_api_key!(params) include_json_accept!(headers) Faraday.delete(path, params, headers) end
raw_get(path, params: {}, headers: {})
click to toggle source
# File lib/manifestly/http/client.rb, line 20 def raw_get(path, params: {}, headers: {}) include_api_key!(params) include_json_accept!(headers) Faraday.get(path, params, headers) end
raw_post(path, params: {}, headers: {})
click to toggle source
# File lib/manifestly/http/client.rb, line 30 def raw_post(path, params: {}, headers: {}) include_api_key!(params) include_json_content_type!(headers) include_json_accept!(headers) Faraday.post(path, params.to_json, headers) end
raw_put(path, params: {}, headers: {})
click to toggle source
# File lib/manifestly/http/client.rb, line 41 def raw_put(path, params: {}, headers: {}) include_api_key!(params) include_json_content_type!(headers) include_json_accept!(headers) Faraday.put(path, params.to_json, headers) end
trim_response(full_response)
click to toggle source
# File lib/manifestly/http/client.rb, line 82 def trim_response(full_response) {status: full_response.status, headers: full_response.headers, body: full_response.body} end