Sha256: 921baba8957b40475639fe61b46cfb5488d31ad17cb4123eb0cdfc191e577add

Contents?: true

Size: 1.78 KB

Versions: 19

Compression:

Stored size: 1.78 KB

Contents

require_relative '../request'
require_relative '../exceptions'
require_relative 'result'
require_relative '../us_street/match_type'


module SmartyStreets
  module USExtract
    # It is recommended to instantiate this class using ClientBuilder.build_us_extract_api_client()
    class Client
      def initialize(sender, serializer)
        @sender = sender
        @serializer = serializer
      end

      # Sends a Lookup object to the US Extract Code API and stores the result in the Lookup's result field.
      # It also returns the result directly.
      def send(lookup)
        if lookup.nil? or lookup.text.nil? or not lookup.text.is_a? String or lookup.text.empty?
          raise SmartyError, 'Client.send() requires a Lookup with the "text" field set'
        end

        request = build_request(lookup)
        response = @sender.send(request)
        raise response.error if response.error
        result = USExtract::Result.new(@serializer.deserialize(response.payload))

        lookup.result = result
      end

      def build_request(lookup)
        request = Request.new
        request.content_type = 'text/plain'
        request.payload = lookup.text

        add_parameter(request, 'html', lookup.html.to_s)
        add_parameter(request, 'aggressive', lookup.aggressive.to_s)
        add_parameter(request, 'addr_line_breaks', lookup.addresses_have_line_breaks.to_s)
        add_parameter(request, 'addr_per_line', lookup.addresses_per_line.to_s)
        if lookup.match !=  SmartyStreets::USStreet::MatchType::STRICT && lookup.match != nil
          add_parameter(request, 'match', lookup.match)
        end

        request
      end

      def add_parameter(request, key, value)
        if value and not value.empty?
          request.parameters[key] = value
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
smartystreets_ruby_sdk-5.20.4 lib/smartystreets_ruby_sdk/us_extract/client.rb
smartystreets_ruby_sdk-5.20.3 lib/smartystreets_ruby_sdk/us_extract/client.rb
smartystreets_ruby_sdk-5.20.2 lib/smartystreets_ruby_sdk/us_extract/client.rb
smartystreets_ruby_sdk-5.19.0 lib/smartystreets_ruby_sdk/us_extract/client.rb
smartystreets_ruby_sdk-5.18.1 lib/smartystreets_ruby_sdk/us_extract/client.rb
smartystreets_ruby_sdk-5.18.0 lib/smartystreets_ruby_sdk/us_extract/client.rb
smartystreets_ruby_sdk-5.17.2 lib/smartystreets_ruby_sdk/us_extract/client.rb
smartystreets_ruby_sdk-5.17.1 lib/smartystreets_ruby_sdk/us_extract/client.rb
smartystreets_ruby_sdk-5.17.0 lib/smartystreets_ruby_sdk/us_extract/client.rb
smartystreets_ruby_sdk-5.16.2 lib/smartystreets_ruby_sdk/us_extract/client.rb
smartystreets_ruby_sdk-5.16.1 lib/smartystreets_ruby_sdk/us_extract/client.rb
smartystreets_ruby_sdk-5.16.0 lib/smartystreets_ruby_sdk/us_extract/client.rb
smartystreets_ruby_sdk-5.15.4 lib/smartystreets_ruby_sdk/us_extract/client.rb
smartystreets_ruby_sdk-5.15.3 lib/smartystreets_ruby_sdk/us_extract/client.rb
smartystreets_ruby_sdk-5.15.2 lib/smartystreets_ruby_sdk/us_extract/client.rb
smartystreets_ruby_sdk-5.15.1 lib/smartystreets_ruby_sdk/us_extract/client.rb
smartystreets_ruby_sdk-5.15.0 lib/smartystreets_ruby_sdk/us_extract/client.rb
smartystreets_ruby_sdk-5.14.22 lib/smartystreets_ruby_sdk/us_extract/client.rb
smartystreets_ruby_sdk-5.14.19 lib/smartystreets_ruby_sdk/us_extract/client.rb