class Shipwire::Response

Attributes

body[R]
error_summary[R]
validation_errors[R]
warnings[R]

Public Class Methods

new(underlying_response: nil, error_summary: nil) click to toggle source
# File lib/shipwire/response.rb, line 7
def initialize(underlying_response: nil, error_summary: nil)
  @error_summary = error_summary

  @validation_errors = []
  @warnings = []

  if underlying_response
    load_response(underlying_response)
  end
end

Public Instance Methods

error_report() click to toggle source
# File lib/shipwire/response.rb, line 34
def error_report
  report_lines = []

  if has_error_summary?
    report_lines << 'Error summary:'
    report_lines << error_summary
  end

  if has_validation_errors?
    report_lines << 'Validation errors:'
    report_lines << validation_errors.pretty_inspect.rstrip
  end

  report_lines.join("\n")
end
has_error_summary?() click to toggle source
# File lib/shipwire/response.rb, line 22
def has_error_summary?
  !error_summary.nil?
end
has_validation_errors?() click to toggle source
# File lib/shipwire/response.rb, line 26
def has_validation_errors?
  !validation_errors.empty?
end
has_warnings?() click to toggle source
# File lib/shipwire/response.rb, line 30
def has_warnings?
  !warnings.empty?
end
ok?() click to toggle source
# File lib/shipwire/response.rb, line 18
def ok?
  !has_error_summary? && !has_validation_errors?
end

Private Instance Methods

load_response(response) click to toggle source
# File lib/shipwire/response.rb, line 52
def load_response(response)
  @body = JSON.parse(response.body)
  @warnings = parse_warnings_from(body)
  @error_summary = parse_error_summary_from(body)
  @validation_errors = parse_validation_errors_from(body)
end
parse_error_summary_from(body) click to toggle source

Errors because of a 40x or 50x error

# File lib/shipwire/response.rb, line 60
def parse_error_summary_from(body)
  if (400..599).include?(body['status']) && body.key?('message')
    body['message']
  else
    nil
  end
end
parse_validation_errors_from(body) click to toggle source

Errors specified in Shipwire response body

# File lib/shipwire/response.rb, line 69
def parse_validation_errors_from(body)
  body.fetch('errors', {})
end
parse_warnings_from(body) click to toggle source
# File lib/shipwire/response.rb, line 73
def parse_warnings_from(body)
  body.fetch('warnings', []).map do |warning|
    warning.fetch('message')
  end
end