Module: Deliveries::Couriers::Spring::Request

Defined in:
lib/deliveries/couriers/spring/request.rb

Class Method Summary collapse

Class Method Details

.endpointObject



33
34
35
36
37
38
39
# File 'lib/deliveries/couriers/spring/request.rb', line 33

def endpoint
  if Spring.live?
    Spring::ENDPOINT_LIVE
  else
    Spring::ENDPOINT_TEST
  end
end

.execute(params:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/deliveries/couriers/spring/request.rb', line 7

def execute(params:)
  response = HTTParty.post(
    endpoint,
    body: params.to_json,
    headers: headers,
    debug_output: Deliveries.debug ? Deliveries.logger : nil
  )
  raise Deliveries::ClientError unless response.success?

  parsed_response = JSON.parse(response.body, symbolize_names: true)
  error_level = parsed_response[:ErrorLevel]

  if error_level.zero?
    parsed_response
  else
    raise Deliveries::APIError.new(
      parsed_response[:Error],
      error_level
    )
  end
end

.headersObject



29
30
31
# File 'lib/deliveries/couriers/spring/request.rb', line 29

def headers
  { 'Content-Type' => "text/json; charset='UTF-8'" }
end