Class: FriendlyShipping::Services::ShipEngine::ParseAddressValidationResponse

Inherits:
Object
  • Object
show all
Extended by:
Dry::Monads::Result::Mixin
Defined in:
lib/friendly_shipping/services/ship_engine/parse_address_validation_response.rb

Class Method Summary collapse

Class Method Details

.call(request:, response:) ⇒ Success<ApiResult<Array<Physical::Location>>>, Failure<ApiResult>

Parameters:

Returns:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/friendly_shipping/services/ship_engine/parse_address_validation_response.rb', line 13

def call(request:, response:)
  parsed_json = JSON.parse(response.body)
  if parsed_json.first['status'] == "error"
    errors = parsed_json.first['messages'].map { |message| message['message'] }.join(", ")
    Failure(
      ApiResult.new(
        errors,
        original_request: request,
        original_response: response
      )
    )
  else
    locations = parsed_json.map do |result|
      address = result['matched_address']
      Physical::Location.new(
        name: address['name'],
        email: address['email'],
        phone: address['phone'],
        company_name: address['company_name'],
        address1: address['address_line1'],
        address2: address['address_line2'],
        address3: address['address_line3'],
        city: address['city_locality'],
        zip: address['postal_code'],
        region: address['state_province'],
        country: address['country_code'],
        address_type: address_type(address)
      )
    end
    Success(
      ApiResult.new(
        locations,
        original_request: request,
        original_response: response
      )
    )
  end
rescue JSON::ParserError => e
  Failure(
    FriendlyShipping::ApiResult.new(
      e,
      original_request: request,
      original_response: response
    )
  )
end