Sha256: 3ac94572f7efa2de08cdb627f247911a1c147d36fb3b9a573d15b2ac39a4a8fc

Contents?: true

Size: 1.93 KB

Versions: 10

Compression:

Stored size: 1.93 KB

Contents

require_relative 'result'
require_relative '../batch'
require_relative '../request'

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

      # Sends a Lookup object to the US ZIP Code API and stores the result in the Lookup's result field.
      def send_lookup(lookup)
        batch = Batch.new
        batch.add(lookup)
        send_batch(batch)
      end

      # Sends a Batch object containing no more than 100 Lookup objects to the US ZIP Code API and stores the
      # results in the result field of the Lookup object.
      def send_batch(batch)
        smarty_request = Request.new

        return if batch.empty?

        converted_lookups = remap_keys(batch.all_lookups)
        smarty_request.payload = @serializer.serialize(converted_lookups)

        response = @sender.send(smarty_request)

        raise response.error if response.error

        results = @serializer.deserialize(response.payload)
        results = [] if results == nil
        assign_results_to_lookups(batch, results)
      end

      def assign_results_to_lookups(batch, results)
        results.each do |raw_result|
          result = Result.new(raw_result)
          batch[result.input_index].result = result
        end
      end

      def remap_keys(obj)
        converted_obj = []
        obj.each do |lookup|
          converted_lookup = {}

          add_field(converted_lookup, 'city', lookup.city)
          add_field(converted_lookup, 'state', lookup.state)
          add_field(converted_lookup, 'zipcode', lookup.zipcode)

          converted_obj.push(converted_lookup)
        end

        converted_obj
      end

      def add_field(converted_lookup, key, value)
        converted_lookup[key] = value unless value.nil? or value.empty?
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
smartystreets_ruby_sdk-5.2.2 lib/smartystreets_ruby_sdk/us_zipcode/client.rb
smartystreets_ruby_sdk-5.2.1 lib/smartystreets_ruby_sdk/us_zipcode/client.rb
smartystreets_ruby_sdk-5.2.0 lib/smartystreets_ruby_sdk/us_zipcode/client.rb
smartystreets_ruby_sdk-5.1.1 lib/smartystreets_ruby_sdk/us_zipcode/client.rb
smartystreets_ruby_sdk-5.1.0 lib/smartystreets_ruby_sdk/us_zipcode/client.rb
smartystreets_ruby_sdk-5.0.0 lib/smartystreets_ruby_sdk/us_zipcode/client.rb
smartystreets_ruby_sdk-4.2.0 lib/smartystreets_ruby_sdk/us_zipcode/client.rb
smartystreets_ruby_sdk-4.1.3 lib/smartystreets_ruby_sdk/us_zipcode/client.rb
smartystreets_ruby_sdk-4.0.1 lib/smartystreets_ruby_sdk/us_zipcode/client.rb
smartystreets_ruby_sdk-4.0.0 lib/smartystreets_ruby_sdk/us_zipcode/client.rb