<commit_before>require 'rubygems'
require 'httparty'
require 'mash'
require 'activesupport'
require 'pp'

module A2WS
  class Base
    include HTTParty
    base_uri 'http://ecs.amazonaws.com'
    default_params :Service => 'AWSECommerceService'
  end

  class ItemSearch < Base
    default_params :Operation => 'ItemSearch'

    def initialize(api_key, search_index)
      self.class.default_params :SearchIndex => search_index,  :AWSAccessKeyId => api_key
    end

    def find(options = {})
      result = self.class.get('/onca/xml', options)

      items = result["ItemSearchResponse"]["Items"]
      if items['Request']['IsValid'] == 'True'
        items['Item'].collect do |i|
          i['ItemAttributes'].inject({}) {|h, j| h[j.first.titlecase.downcase.gsub(' ', '_')] = j.last ; h}.to_mash
        end
      else
        raise items['Request']['Errors']['Error']['Message']
      end
    end
  end
  
end<commit_msg>Make sure we preserve the entire item.<commit_after>require 'rubygems'
require 'httparty'
require 'mash'
require 'activesupport'
require 'pp'

module A2WS
  class Base
    include HTTParty
    base_uri 'http://ecs.amazonaws.com'
    default_params :Service => 'AWSECommerceService'
  end

  class ItemSearch < Base
    attr_accessor :items
    
    default_params :Operation => 'ItemSearch'

    def initialize(api_key, search_index = :All)
      self.class.default_params :SearchIndex => search_index,  :AWSAccessKeyId => api_key
    end

    def find(keywords, options = {})
      options.merge!({:Keywords => keywords})
      puts options.inspect
      result = self.class.get('/onca/xml', :query => options)

      @items = result["ItemSearchResponse"]["Items"]
      if @items['Request']['IsValid'] == 'True'
        @items['Item'].collect do |i|
          Mash.new downcase_keys(i)
        end
      else
        raise @items['Request']['Errors']['Error']['Message']
      end
    end
    
    protected
    
    def downcase_keys(hash)
      new_hash = {}
      hash.keys.each do |key|
        value = hash.delete(key)
        new_hash[downcase_key(key)] = value
        new_hash[downcase_key(key)] = downcase_keys(value) if value.is_a?(Hash)
        new_hash[downcase_key(key)] = value.each{|p| downcase_keys(p) if p.is_a?(Hash)} if value.is_a?(Array)
      end
      new_hash
    end
    
    def downcase_key(key)
      key.titlecase.downcase.gsub(' ', '_')
    end
    
  end
  
end
{'fraction_non_alphanumeric': 0.13280926768721554, 'fraction_numerical': 0.0008274720728175424, 'mean_word_length': 2.99009900990099, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4570407', 'n_tokens_mistral': 785, 'n_tokens_neox': 788, 'n_words': 167}