Sha256: afdc321eb93ba4296164f56a3f7a21ad7998f000a3ee3ea5c9d81b520c724a4a

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

require 'rakuten_web_service/resource'
require 'rakuten_web_service/ichiba/ranking'

module RakutenWebService
  module Ichiba
    class Genre < Resource

      class << self
        def new(params)
          case params
          when Integer, String
            Genre[params.to_s] || search(:genre_id => params.to_s).first
          when Hash
            super
          end
        end

        def root
          self.new(0)
        end

        def [](id)
          repository[id.to_s]
        end

        def []=(id, genre)
          repository[id.to_s] = genre
        end

        private
        def repository
          @repository ||= {}
        end
      end

      endpoint 'https://app.rakuten.co.jp/services/api/IchibaGenre/Search/20120723'

      set_parser do |response|
        current = response['current']
        if children = response['children']
          children = children.map { |child| Genre.new(child['child']) }
          current.merge!('children' => children)
        end
        if parents = response['parents']
          parents = parents.map { |parent| Genre.new(parent['parent']) }
          current.merge!('parents' => parents)
        end

        genre = Genre.new(current)
        [genre]
      end

      attribute :genreId, :genreName, :genreLevel

      def initialize(params)
        super
        Genre[self.id.to_s] = self
      end

      def ranking(options={})
        RakutenWebService::Ichiba::RankingItem.search(:genre_id => self.id)
      end

      def children
        @params['children'] ||= Genre.search(:genre_id => self.id).first.children
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rakuten_web_service-0.4.1 lib/rakuten_web_service/ichiba/genre.rb
rakuten_web_service-0.3.1 lib/rakuten_web_service/ichiba/genre.rb
rakuten_web_service-0.3.0 lib/rakuten_web_service/ichiba/genre.rb