# -*- encoding : utf-8 -*-
module Forklift
  module Utils
    # collection = {"count" => 123, "gd" => [...]}
    # target = gd or catalog
    def create_from_collection(collection, target, client)
      result = {}
      result[:count] = collection["count"]
      case target.to_sym
      when :catalog
        result[:catalogs] = collection["category"].nil? ? [] : collection["category"].map do |hash|
          Forklift::Client::Catalog.new(client, hash)
        end
      when :gd
        result[:gds] = collection["gd"].nil? ? [] : collection["gd"].map do |hash|
          Forklift::Client::Gd.new(client, hash)
        end
      end

      return result
    end

    def sym_or_str_key(hash, key)
      result = hash[key.to_sym] || hash[key.to_s]
      return result
    end
  end
end

# -*- encoding : utf-8 -*-
module Forklift
  module Utils
    # collection = {"count" => 123, "gd" => [...]}
    # target = gd or catalog
    def create_from_collection(collection, target, client)
      result = {}
      result[:count] = collection["count"]
      case target.to_sym
      when :catalog

        if result[:count].to_i == 1
          result[:catalogs] = [Forklift::Client::Catalog.new(client, collection["category"])]
        elsif result[:count].to_i > 1
          result[:catalogs] = collection["category"].map do |hash|
            Forklift::Client::Catalog.new(client, hash)
          end
        else
          result[:catalogs] = []
        end

      when :gd

        if result[:count].to_i == 1
          result[:gds] = [Forklift::Client::Gd.new(client, collection["gd"])]
        elsif result[:count].to_i > 1
          result[:gds] = collection["gd"].nil? ? [] : collection["gd"].map do |hash|
            Forklift::Client::Gd.new(client, hash)
          end
        else
          result[:gds] = []
        end

      end

      return result
    end

    def sym_or_str_key(hash, key)
      result = hash[key.to_sym] || hash[key.to_s]
      return result
    end
  end
end

Fix a damn bug cause by parsing XML.
Fix a damn bug cause by parsing XML.

The XML return by Yahoo API is in the form:

<categories count="8">
    <category>
        <no>259</no>
        <level_no>2</level_no>
        <name><![CDATA[ohoh-mini]]></name>
        <type><![CDATA[1]]></type>
    </category>
    <category>
        <no>260</no>
        <level_no>2</level_no>
        <name><![CDATA[ohoh-max]]></name>
        <type><![CDATA[2]]></type>
    </category>
</categories>

After parsing the XML(via MultiXML), we get the Hash in the form:

{"categories"=>
    {"count"=>"8", "category"=>
        [{"no"=>"259", "level_no"=>"2", "name"=>"ohoh-mini", "type"=>"1"}, {"no"=>"260", "level_no"=>"2", "name"=>"ohoh-max", "type"=>"2"}]
    }
}

The bug happens when the returned XML is:

<categories count="8">
    <category>
        <no>259</no>
        <level_no>2</level_no>
        <name><![CDATA[ohoh-mini]]></name>
        <type><![CDATA[1]]></type>
    </category>
</categories>

In this case, the Hash will be:

{"categories"=>{"category"=>{"no"=>"259", "level_no"=>"2", "name"=>"ohoh-mini", "type"=>"1"}, "count"=>"8"}}

i.e. the value of key "category" will be a Hash instead of an Array, this will cause an error in Forklift::Utils
