<commit_before>class Mashery::WOW::Data::Base

  def initialize args
    args.each do |k,v|
      instance_variable_set("@#{k}", v) unless v.nil?
    end
  end

  # Query Battlenet API for the the data based on the subclass's scope
  #
  # Hash Params:
  #   :region          - (e.g. 'us', 'ea')
  #   :name            - String name of the toon
  #   :realm           - String name of the server the character is on (String)
  #   :locale          - String locale (defaults to 'en_US')
  #   :key             - String api key
  #
  # Example : IF a character named 'AlexeiStukov' is on 'DragonMaw' 'US' server
  def self.find_all(args)
    region = args.delete(:region)
    key    = args.delete(:key)
    locale     = args.delete(:locale) || 'en_US'

    base_api = Mashery::WOW.new(region: region)
    call_url = base_api.url + 'data/' + scopes[:url] + "?locale=#{locale}&apikey=#{key}"

    begin
      data = open(call_url)
      parsed_response = JSON.parse(data.read)

      if Mashery::API.valid_call?(data.status, parsed_response)
        collection = collection_from_api(parsed_response)
      else
        collection = []
      end


    rescue OpenURI::HTTPError => e
      collection = []
    end

    return collection
  end

  private

  def self.collection_from_api(response_collection)
    response_collection[scopes[:collection_root]].collect do |attrs|
      new(attrs)
    end
  end

  def self.url(collection_scope)
    "#{Mashery::WOW.url}/data/#{collection_scope}"
  end

  def self.scopes
    self::SCOPES
  end

  def self.params_mapping
    self::PARAMS_MAPPING
  end

  def self.from_api(raw_hash)
    new_hash = {}
    association_hash ||= {}
    other_attributes ||= {}

    # NOTE common tasks below -- marker for easier method extraction
    params_mapping.each do |old_key, new_key|
      if response.has_key?(old_key)
        new_hash[new_key] = response[old_key]
      end
    end

    new_hash.merge!(association_hash)
    new_hash.merge!(other_attributes)
    new(new_hash)
    # NOTE end of common tasks
  end
end<commit_msg>Fix error where the creation of objects from the collection response does not pass through the params mapper method, Milord.<commit_after>class Mashery::WOW::Data::Base

  def initialize args
    args.each do |k,v|
      instance_variable_set("@#{k}", v) unless v.nil?
    end
  end

  # Query Battlenet API for the the data based on the subclass's scope
  #
  # Hash Params:
  #   :region          - (e.g. 'us', 'ea')
  #   :name            - String name of the toon
  #   :realm           - String name of the server the character is on (String)
  #   :locale          - String locale (defaults to 'en_US')
  #   :key             - String api key
  #
  # Example : IF a character named 'AlexeiStukov' is on 'DragonMaw' 'US' server
  def self.find_all(args)
    region = args.delete(:region)
    key    = args.delete(:key)
    locale     = args.delete(:locale) || 'en_US'

    base_api = Mashery::WOW.new(region: region)
    call_url = base_api.url + 'data/' + scopes[:url] + "?locale=#{locale}&apikey=#{key}"

    begin
      data = open(call_url)
      parsed_response = JSON.parse(data.read)

      if Mashery::API.valid_call?(data.status, parsed_response)
        collection = collection_from_api(parsed_response)
      else
        collection = []
      end


    rescue OpenURI::HTTPError => e
      collection = []
    end

    return collection
  end

  private

  def self.collection_from_api(response_collection)
    response_collection[scopes[:collection_root]].collect do |attrs|
      from_api(attrs)
    end
  end

  def self.url(collection_scope)
    "#{Mashery::WOW.url}/data/#{collection_scope}"
  end

  def self.scopes
    self::SCOPES
  end

  def self.params_mapping
    self::PARAMS_MAPPING
  end

  def self.from_api(raw_hash)
    new_hash = {}
    association_hash ||= {}
    other_attributes ||= {}

    # NOTE common tasks below -- marker for easier method extraction
    params_mapping.each do |old_key, new_key|
      if raw_hash.has_key?(old_key)
        new_hash[new_key] = raw_hash[old_key]
      end
    end

    new_hash.merge!(association_hash)
    new_hash.merge!(other_attributes)
    new(new_hash)
    # NOTE end of common tasks
  end
end
{'fraction_non_alphanumeric': 0.10764872521246459, 'fraction_numerical': 0.0, 'mean_word_length': 2.7004366812227074, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '6512047', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1390, 'n_words': 344}