Sha256: 982b362c31118d7f236ddefb10735383518c16ad3b1b78afef21235df2593a74

Contents?: true

Size: 1.18 KB

Versions: 8

Compression:

Stored size: 1.18 KB

Contents

module MelissaData
  module WebSmart
    module ResponseProcessor
      require 'yaml'
      CODE_TYPES = [:property, :address]

      def process(response, resp_type)
        codes = codes(response, resp_type)
        if has_error_codes?(codes)
          { errors: codes_for(codes, resp_type, 'error') }
        else
          response.merge!(success: codes_for(codes, resp_type, 'success'))
        end
      end

      def codes_for(codes, resp_type, code_type)
        codes.map do |code|
          YAML.load(File.read("config/#{resp_type}_#{code_type}_codes.yml")).values.first[code.to_s]
        end.compact
      end

      def codes(response, resp_type)
        case resp_type
        when 'property'
          response[:result][:code].split(",")
        when 'address'
          response[:results].split(",")
        end
      end

      def has_error_codes?(codes)
        !codes.select { |code| error_codes.include? code.to_sym }.empty?
      end

      def error_codes
        error_code_strings = CODE_TYPES.map { |t| File.read("config/#{t}_error_codes.yml") }
        error_code_strings.map { |c| YAML.load(c) }.flat_map { |code| code.values.first.keys.map(&:to_sym) }
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
melissa_data-0.2.13 lib/melissa_data/web_smart/response_processor.rb
melissa_data-0.2.12 lib/melissa_data/web_smart/response_processor.rb
melissa_data-0.2.11 lib/melissa_data/web_smart/response_processor.rb
melissa_data-0.2.10 lib/melissa_data/web_smart/response_processor.rb
melissa_data-0.2.9 lib/melissa_data/web_smart/response_processor.rb
melissa_data-0.2.8 lib/melissa_data/web_smart/response_processor.rb
melissa_data-0.2.7 lib/melissa_data/web_smart/response_processor.rb
melissa_data-0.2.6 lib/melissa_data/web_smart/response_processor.rb