Class: Cloth::Client
- Inherits:
-
Object
- Object
- Cloth::Client
- Defined in:
- lib/cloth/client.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
Class Method Summary collapse
Instance Method Summary collapse
- #get(url, options = {}) ⇒ Object
-
#initialize(api_key = nil) ⇒ Client
constructor
A new instance of Client.
- #post(url, options = {}) ⇒ Object
- #request(method, url, options = {}) ⇒ Object
Constructor Details
#initialize(api_key = nil) ⇒ Client
Returns a new instance of Client.
7 8 9 10 |
# File 'lib/cloth/client.rb', line 7 def initialize(api_key = nil) @api_key = api_key || Cloth.api_key raise Exception, "An API key must be set before the Cloth client can be used. Set an API key with 'Cloth.api_key = ...'" unless @api_key end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
6 7 8 |
# File 'lib/cloth/client.rb', line 6 def api_key @api_key end |
Class Method Details
Instance Method Details
#get(url, options = {}) ⇒ Object
12 13 14 |
# File 'lib/cloth/client.rb', line 12 def get(url, = {}) JSON.parse(request(:get, url, ).run.body) end |
#post(url, options = {}) ⇒ Object
16 17 18 19 |
# File 'lib/cloth/client.rb', line 16 def post(url, = {}) headers = { 'Content-Type': "application/x-www-form-urlencoded" } JSON.parse(request(:post, url, .merge({ headers: headers })).run.body) end |
#request(method, url, options = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/cloth/client.rb', line 21 def request(method, url, = {}) fixed_url = url[0] == "/" ? url[1..-1] : url url = [Cloth.api_url, fixed_url].join('/') Typhoeus::Request.new( url, method: method, body: [:body], params: [:params], headers: { 'Accept': 'application/json', 'Cloth-Api-Key': @api_key, 'Content-Type': 'application/json' }.merge([:headers] || {}) ) end |