Sha256: 58cbfcb6b3afc1872324430c284f0d700091fe7ecbedd7168abb8d049b16d385

Contents?: true

Size: 1.64 KB

Versions: 5

Compression:

Stored size: 1.64 KB

Contents

require 'rakuten_web_service/resource'

module RakutenWebService
  module Books
    class Genre < Resource
      set_resource_name 'books_genre'

      endpoint 'https://app.rakuten.co.jp/services/api/BooksGenre/Search/20121128'

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

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

      attribute :booksGenreId, :booksGenreName, :genreLevel

      def self.root
        new('000')
      end

      def self.new(params)
        case params
        when String
          Genre[params] ||= self.search(:booksGenreId => params).first
        when Hash
          super
        else
          raise ArgumentError, 'Invalid parameter for initializing Books::Genre'
        end
      end

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

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

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

      def search(params={})
        params = params.merge(:booksGenreId => self.id)
        resource = Books::Resource.find_resource_by_genre_id(self.id)
        resource.search(params)
      end

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rakuten_web_service-0.5.0 lib/rakuten_web_service/books/genre.rb
rakuten_web_service-0.4.2 lib/rakuten_web_service/books/genre.rb
rakuten_web_service-0.4.1 lib/rakuten_web_service/books/genre.rb
rakuten_web_service-0.3.1 lib/rakuten_web_service/books/genre.rb
rakuten_web_service-0.3.0 lib/rakuten_web_service/books/genre.rb