module Snap::Client

Encapsulates the configuration of each client before JIT before requests are made. This allows us to use our configuration which won't have been available until runtime, not load time.

Public Instance Methods

client() click to toggle source
# File lib/snap/client.rb, line 6
def client
  base_uri Snap.config.endpoint
  basic_auth(Snap.config.username, Snap.config.password)
  headers('Content-Type' => 'application/json')
  self
end
delete(*args, &block) click to toggle source
Calls superclass method
# File lib/snap/client.rb, line 25
def delete(*args, &block)
  wrap_response do
    super(*args, &block)
  end
end
get(*args, &block) click to toggle source
Calls superclass method
# File lib/snap/client.rb, line 13
def get(*args, &block)
  wrap_response do
    super(*args, &block)
  end
end
post(*args, &block) click to toggle source
Calls superclass method
# File lib/snap/client.rb, line 19
def post(*args, &block)
  wrap_response do
    super(*args, &block)
  end
end
put(*args, &block) click to toggle source
Calls superclass method
# File lib/snap/client.rb, line 31
def put(*args, &block)
  wrap_response do
    super(*args, &block)
  end
end
snoop_for_errors(httparty_response) click to toggle source
# File lib/snap/client.rb, line 43
def snoop_for_errors(httparty_response)
  case httparty_response.parsed_response.class.to_s
    # Snap can return response bodies in many different formats. How we look
    # for and what errors can occur are dependent on that type. For example
    # 500's mostly return raw html as a string. Pages with lists are an
    # Array. Resource endpoints are typically Hash.
  when 'Hash'
    raise Api::OrderStageError, httparty_response if httparty_response.parsed_response.value? 'ORDER_STAGE'
    raise Api::DefinitionError, httparty_response if httparty_response.parsed_response.value? 'DEFINITION'
    raise Api::BadRequestError, httparty_response if httparty_response.code == 400
  end
end
wrap_response() { || ... } click to toggle source
# File lib/snap/client.rb, line 37
def wrap_response
  httparty_response = yield
  snoop_for_errors(httparty_response)
  Snap::Response.new(httparty_response, model)
end