class Foxtrot::Client

Attributes

host[RW]
ssl[RW]

Public Class Methods

new(api_key, version='v1') click to toggle source
# File lib/foxtrot/client.rb, line 18
def initialize(api_key, version='v1')
  @api_key = api_key
  @version = version
  @headers = {:content_type => :json, :accept => :json}
end

Public Instance Methods

optimize!(data, args={}) click to toggle source
# File lib/foxtrot/client.rb, line 29
def optimize!(data, args={})
  check_required 'optimize', data
  resp = post_request 'optimize', data, args
  Foxtrot::Response::Response.response_for('optimize', resp, self)
end
poll!(endpoint, txid, args={}) click to toggle source
# File lib/foxtrot/client.rb, line 24
def poll!(endpoint, txid, args={})
  resp = get_request(endpoint, {txid: txid})
  Foxtrot::Response::Response.response_for(endpoint, resp, self)
end

Private Instance Methods

build_url(endpoint, args) click to toggle source
# File lib/foxtrot/client.rb, line 47
def build_url(endpoint, args)
  uri = Addressable::URI.new
  uri.host = self.class.host
  uri.scheme = self.class.ssl ? 'https' : 'http'
  uri.query_values = args.merge({key: @api_key})
  uri.path = "/api/#{@version}/#{endpoint}"
  uri.to_s
end
check_required(endpoint, data) click to toggle source
# File lib/foxtrot/client.rb, line 37
def check_required(endpoint, data)
  required = Foxtrot::RequiredFields.const_get(endpoint.upcase)
  data = data.inject({}){ |h, (k,v)| h.merge({k.to_sym => v}) }
  required.each do |field|
    unless data.include?(field)
      raise Foxtrot::Errors::ParameterError.new(field)
    end
  end
end
get_request(endpoint, args) click to toggle source
# File lib/foxtrot/client.rb, line 56
def get_request(endpoint, args)
  resp = RestClient.get build_url(endpoint, args), @headers
  JSON.load resp
end
post_request(endpoint, data, args) click to toggle source
# File lib/foxtrot/client.rb, line 61
def post_request(endpoint, data, args)
  resp = RestClient.post build_url(endpoint, args), data.to_json, @headers
  JSON.load resp
end