-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into 10082-remove-pref
- Loading branch information
Showing
23 changed files
with
1,398 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'base_address' | ||
require 'common/hash_helpers' | ||
|
||
module VAProfile | ||
module Models | ||
module V3 | ||
# Model for addresses sent and received from the VA profile address validation API | ||
# AddressValidationV is used for ProfileServiceV3 and ContactInformationV2 | ||
class ValidationAddress < V3::BaseAddress | ||
# Convert a ValidationAddress into a hash that can be sent to the address validation | ||
# API | ||
# @return [Hash] hash that is formatted for POSTing to address validation API | ||
def address_validation_req | ||
Common::HashHelpers.deep_remove_blanks( | ||
address: attributes.slice( | ||
:address_line1, | ||
:address_line2, | ||
:address_line3 | ||
).deep_transform_keys { |k| k.to_s.camelize(:lower) }.merge( | ||
intPostalCode: @international_postal_code, | ||
cityName: @city, | ||
zipCode5: @zip_code, | ||
zipCode4: @zip_code_suffix, | ||
country: { countryCodeISO2: @country_code_iso2, countryCodeISO3: @country_code_iso3, | ||
countryName: @country_name, countryCodeFIPS: @country_code_fips }, | ||
state: { stateCode: @state_code, stateName: @state_name }, | ||
province: { provinceName: @province_name, provinceCode: @province_code }, | ||
addressPOU: @address_pou | ||
) | ||
) | ||
end | ||
|
||
# @return [VAProfile::Models::V3::ValidationAddress] validation address model created from | ||
# address validation API response | ||
def self.build_from_address_suggestion(address_suggestion_hash) | ||
address_type = address_suggestion_hash['address_type'].upcase | ||
attributes = { | ||
address_line1: address_suggestion_hash['address_line1'], | ||
address_line2: address_suggestion_hash['address_line2'], | ||
address_line3: address_suggestion_hash['address_line3'], | ||
address_type:, | ||
city: address_suggestion_hash['city_name'], | ||
country_name: address_suggestion_hash.dig('country', 'country_name'), | ||
country_code_iso3: address_suggestion_hash.dig('country', 'country_code_iso3') | ||
}.merge(regional_attributes(address_type, address_suggestion_hash)) | ||
|
||
new(attributes) | ||
end | ||
|
||
def self.regional_attributes(address_type, address_hash) | ||
if address_type == INTERNATIONAL | ||
{ | ||
province: address_hash['province']['province_name'], | ||
international_postal_code: address_hash['int_postal_code'] | ||
} | ||
else | ||
{ | ||
state_code: address_hash.dig('state', 'state_code'), | ||
county_code: address_hash.dig('county', 'county_code'), | ||
county_name: address_hash.dig('county', 'county_name'), | ||
zip_code: address_hash['zip_code5'], | ||
zip_code_suffix: address_hash['zip_code4'] | ||
} | ||
end | ||
end | ||
|
||
def self.build_address_metadata(address_suggestion_hash) | ||
{ | ||
confidence_score: address_suggestion_hash['confidence'], | ||
address_type: address_suggestion_hash['address_type'], | ||
delivery_point_validation: address_suggestion_hash['delivery_point_validation'] | ||
} | ||
end | ||
end | ||
end | ||
end | ||
end |
36 changes: 36 additions & 0 deletions
36
lib/va_profile/v3/address_validation/address_suggestions_response.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'va_profile/models/v3/validation_address' | ||
require_relative 'service' | ||
|
||
module VAProfile | ||
module V3 | ||
module AddressValidation | ||
# Wrapper for response from VA profile address validation API. | ||
# Contains address suggestions and validation key used to ignore suggested addresses | ||
# and use original address. | ||
class AddressSuggestionsResponse | ||
def initialize(candidate_res) | ||
validation_key = candidate_res['override_validation_key'] | ||
@response = { | ||
addresses: candidate_res['candidate_addresses'].map do |address_suggestion_hash| | ||
{ | ||
address: VAProfile::Models::V3::ValidationAddress.build_from_address_suggestion( | ||
address_suggestion_hash | ||
).to_h.compact, | ||
address_meta_data: VAProfile::Models::V3::ValidationAddress.build_address_metadata( | ||
address_suggestion_hash | ||
).to_h | ||
} | ||
end, | ||
validation_key: | ||
} | ||
end | ||
|
||
def to_json(*_args) | ||
@response.to_json | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'va_profile/configuration' | ||
|
||
module VAProfile | ||
module V3 | ||
module AddressValidation | ||
class Configuration < VAProfile::Configuration | ||
def base_path | ||
"#{VAProfile::Configuration::SETTINGS.address_validation.url}/services/address-validation/v3/" | ||
end | ||
|
||
def base_request_headers | ||
super.merge('apiKey' => VAProfile::Configuration::SETTINGS.address_validation.api_key) | ||
end | ||
|
||
def service_name | ||
'VAProfile/V3/AddressValidation' | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'common/client/concerns/monitoring' | ||
require 'common/exceptions' | ||
require_relative 'configuration' | ||
require_relative 'address_suggestions_response' | ||
require 'va_profile/service' | ||
require 'va_profile/stats' | ||
|
||
module VAProfile | ||
module V3 | ||
module AddressValidation | ||
# Wrapper for the VA profile address validation/suggestions API | ||
class Service < VAProfile::Service | ||
include Common::Client::Concerns::Monitoring | ||
|
||
STATSD_KEY_PREFIX = "#{VAProfile::Service::STATSD_KEY_PREFIX}.address_validation".freeze | ||
configuration VAProfile::V3::AddressValidation::Configuration | ||
|
||
def initialize; end | ||
|
||
# Get address suggestions and override key from the VA profile API | ||
# @return [VAProfile::AddressValidation::AddressSuggestionsResponse] response wrapper around address | ||
# suggestions data | ||
def address_suggestions(address) | ||
with_monitoring do | ||
candidate_res = candidate(address) | ||
|
||
AddressSuggestionsResponse.new(candidate_res) | ||
end | ||
end | ||
|
||
# @return [Hash] raw data from VA profile address validation API including | ||
# address suggestions, validation key, and address errors | ||
def candidate(address) | ||
begin | ||
res = perform( | ||
:post, | ||
'candidate', | ||
address.address_validation_req.to_json | ||
) | ||
rescue => e | ||
handle_error(e) | ||
end | ||
|
||
res.body | ||
end | ||
|
||
private | ||
|
||
def handle_error(error) | ||
raise error unless error.is_a?(Common::Client::Errors::ClientError) | ||
|
||
save_error_details(error) | ||
raise_invalid_body(error, self.class) unless error.body.is_a?(Hash) | ||
|
||
raise Common::Exceptions::BackendServiceException.new( | ||
'VET360_AV_ERROR', | ||
detail: error.body | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.