Sha256: 2d9677cc9ddaaa86f74825c15b69d2c011bd562404b00a5ee78007537b658f03

Contents?: true

Size: 1.35 KB

Versions: 8

Compression:

Stored size: 1.35 KB

Contents

module Mashery
  class RpcClient
    class Response
      def initialize(query_builder)
        @query_builder = query_builder
        @json          = query_builder.to_json
      end

      def result
        @json["result"]
      end

      def error
        @json["error"]
      end

      def items
        result["items"]
      end

      def to_objects
        items.map do |item|
          @query_builder.reify(item)
        end
      end

      def find_each(&block)
        to_objects.each(&block)

        if next_page?
          response = next_page!
          response.find_each(&block)
        end
      end

      def error?
        error.present?
      end

      def length
        items.length
      end

      def more_pages?
        current_page < total_pages
      end

      alias :next_page? :more_pages?

      def next_page_number
        current_page + 1
      end

      # Can't call Mashery too many times, or they get mad
      MASHERY_WAIT_TIME = 0.1

      def next_page!
        sleep(MASHERY_WAIT_TIME)
        Response.new(@query_builder.page(next_page_number))
      end

      def total_items
        result["total_items"]
      end

      def total_pages
        result["total_pages"]
      end

      def current_page
        result["current_page"]
      end

      def items_per_page
        result["items_per_page"]
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mashery_rails-0.6.9.pre4 lib/mashery/rpc_client/response.rb
mashery_rails-0.6.9.pre3 lib/mashery/rpc_client/response.rb
mashery_rails-0.6.9.pre1 lib/mashery/rpc_client/response.rb
mashery_rails-0.6.8 lib/mashery/rpc_client/response.rb
mashery_rails-0.6.7 lib/mashery/rpc_client/response.rb
mashery_rails-0.6.6 lib/mashery/rpc_client/response.rb
mashery_rails-0.6.3 lib/mashery/rpc_client/response.rb
mashery_rails-0.6.2 lib/mashery/rpc_client/response.rb