Sha256: 893ff1cf9ed99d8e48b47f4b8f7f110cad7e6881952b0467b6288b55b567a0c0

Contents?: true

Size: 1.34 KB

Versions: 9

Compression:

Stored size: 1.34 KB

Contents

require_relative './base'
require_relative './models/model'

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

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

      def client
        return Spaceship::ConnectAPI::Base.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

9 entries across 9 versions & 1 rubygems

Version Path
fastlane-2.126.0.beta.20190606200048 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.126.0.beta.20190605200119 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.126.0.beta.20190604200017 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.125.2 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.126.0.beta.20190603200020 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.125.1 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.126.0.beta.20190602200102 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.126.0.beta.20190601200101 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.125.0 spaceship/lib/spaceship/connect_api/response.rb