class Cloth::Client

Attributes

api_key[R]

Public Class Methods

get(url, options = {}) click to toggle source
# File lib/cloth/client.rb, line 38
def get(url, options = {})
  Cloth.client.get(url, options)
end
new(api_key = nil) click to toggle source
# 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
post(url, options = {}) click to toggle source
# File lib/cloth/client.rb, line 42
def post(url, options = {})
  Cloth.client.post(url, options)
end

Public Instance Methods

get(url, options = {}) click to toggle source
# File lib/cloth/client.rb, line 12
def get(url, options = {})
  JSON.parse(request(:get, url, options).run.body)
end
post(url, options = {}) click to toggle source
# File lib/cloth/client.rb, line 16
def post(url, options = {})
  headers = { 'Content-Type': "application/x-www-form-urlencoded" }
  JSON.parse(request(:post, url, options.merge({ headers: headers })).run.body)
end
request(method, url, options = {}) click to toggle source
# File lib/cloth/client.rb, line 21
def request(method, url, options = {})
  fixed_url = url[0] == "/" ? url[1..-1] : url
  url = [Cloth.api_url, fixed_url].join('/')
  Typhoeus::Request.new(
    url,
    method: method,
    body: options[:body],
    params: options[:params],
    headers: {
      'Accept': 'application/json',
      'Cloth-Api-Key': @api_key,
      'Content-Type': 'application/json'
    }.merge(options[:headers] || {})
  )
end