Module: Snap::Client

Included in:
Api::Outbound, Api::ShipmentStatus, Api::Shipments, Api::StockTotals
Defined in:
lib/snap/client.rb

Overview

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.

Instance Method Summary collapse

Instance Method Details

#clientObject



6
7
8
9
10
11
# 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) ⇒ Object



25
26
27
28
29
# File 'lib/snap/client.rb', line 25

def delete(*args, &block)
  wrap_response do
    super(*args, &block)
  end
end

#get(*args, &block) ⇒ Object



13
14
15
16
17
# File 'lib/snap/client.rb', line 13

def get(*args, &block)
  wrap_response do
    super(*args, &block)
  end
end

#post(*args, &block) ⇒ Object



19
20
21
22
23
# File 'lib/snap/client.rb', line 19

def post(*args, &block)
  wrap_response do
    super(*args, &block)
  end
end

#put(*args, &block) ⇒ Object



31
32
33
34
35
# File 'lib/snap/client.rb', line 31

def put(*args, &block)
  wrap_response do
    super(*args, &block)
  end
end

#snoop_for_errors(httparty_response) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# 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_responseObject



37
38
39
40
41
# 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