Sha256: 8fe50dddb44eada43e0af9d22dc4071144fd9441b72ae5af10c5a37279933fef

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module RakutenWebService
  class Recipe < Resource

    def self.large_categories
      categories('large')
    end

    def self.medium_categories
      categories('medium')
    end

    def self.small_categories
      categories('small')
    end

    def self.categories(category_type)
      @categories ||= {}
      @categories[category_type] ||= Category.search(category_type: category_type).response['result'][category_type].map do |category|
        Category.new(category.merge(categoryType: category_type))
      end
    end

    class << self
      protected :search
    end

    class Category < Resource
      endpoint 'https://app.rakuten.co.jp/services/api/Recipe/CategoryList/20170426'

      attribute :categoryId, :categoryName, :categoryUrl, :parentCategoryId, :categoryType

      def ranking
        Recipe.ranking(absolute_category_id)
      end

      def parent_category
        return nil if parent_category_type.nil?
        Recipe.categories(parent_category_type).find { |c| c.id.to_i === parent_category_id.to_i }
      end

      def absolute_category_id
        if parent_category
          [parent_category.absolute_category_id, id.to_s].join('-')
        else
          id.to_s
        end
      end

      private
        def parent_category_type
          case type
          when 'small' then 'medium'
          when 'medium' then 'large'
          else
            nil
          end
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rakuten_web_service-1.9.0 lib/rakuten_web_service/recipe/category.rb