Sha256: a46a96cdfb4a43563e673af203e369673b48c7d589caa874773abaaaf1d3da1c

Contents?: true

Size: 1.29 KB

Versions: 16

Compression:

Stored size: 1.29 KB

Contents

require_relative './model'

module Spaceship
  module ConnectAPI
    class Response
      include Enumerable
      attr_reader :body
      attr_reader :status
      attr_reader :client

      def initialize(body: nil, status: nil, client: nil)
        @body = body
        @status = status
        @client = client
      end

      def next_url
        return nil if body.nil?
        links = body["links"] || {}
        return links["next"]
      end

      def next_page
        url = next_url
        return nil if url.nil?
        return client.get(url)
      end

      def next_pages(count: 1)
        if !count.nil? && count < 0
          count = 0
        end

        responses = [self]
        counter = 0

        resp = self
        loop do
          resp = resp.next_page
          break if resp.nil?
          responses << resp
          counter += 1

          break if !count.nil? && counter >= count
        end

        return responses
      end

      def all_pages
        return next_pages(count: nil)
      end

      def to_models
        return [] if body.nil?
        model_or_models = Spaceship::ConnectAPI::Models.parse(body)
        return [model_or_models].flatten
      end

      def each(&block)
        to_models.each do |model|
          yield(model)
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
fastlane-2.126.0.beta.20190623200100 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.126.0.beta.20190622200100 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.126.0.beta.20190621200104 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.126.0.beta.20190620200055 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.126.0.beta.20190619200046 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.126.0.beta.20190618200051 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.126.0.beta.20190617200114 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.126.0.beta.20190616200045 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.126.0.beta.20190615200013 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.126.0.beta.20190614200105 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.126.0.beta.20190613200017 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.126.0.beta.20190611200027 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.126.0.beta.20190610200032 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.126.0.beta.20190609200013 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.126.0.beta.20190608200023 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.126.0.beta.20190607200028 spaceship/lib/spaceship/connect_api/response.rb