Sha256: eb76fa9a99252c369ee583c9edf5d7d8afc181560aff08b8ef4c91c45cbcfb89

Contents?: true

Size: 1.14 KB

Versions: 4

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

require 'rakuten_web_service/string_support'

module RakutenWebService
  class Response
    include Enumerable

    def initialize(resource_class, json)
      @resource_class = resource_class
      @json = json.dup
    end

    def [](key)
      @json[key]
    end

    def each
      resources.each do |resource|
        yield resource
      end
    end

    using RakutenWebService::StringSupport

    %w[count hits page first last carrier pageCount].each do |name|
      method_name = name.to_snake
      define_method method_name do
        self[name]
      end
    end

    def genre_information
      return unless @resource_class.respond_to?(:genre_class)
      return if self['GenreInformation'].empty?
      RWS::GenreInformation.new(self['GenreInformation'][0], @resource_class.genre_class)
    end

    def resources
      @resources ||= @resource_class.parse_response(@json)
    end

    def has_next_page?
      page && (not last_page?)
    end

    def has_previous_page?
      page && (not first_page?)
    end

    def first_page?
      page == 1
    end

    def last_page?
      page >= page_count
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rakuten_web_service-1.12.0 lib/rakuten_web_service/response.rb
rakuten_web_service-1.11.0 lib/rakuten_web_service/response.rb
rakuten_web_service-1.10.0 lib/rakuten_web_service/response.rb
rakuten_web_service-1.9.2 lib/rakuten_web_service/response.rb