Sha256: f7637a592d096ab39b92006f7bc70e6d32bbfe703cac296e10e8959ad9eb1f82

Contents?: true

Size: 1.72 KB

Versions: 28

Compression:

Stored size: 1.72 KB

Contents

require_relative './model'

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

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

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

      def next_page(&block)
        url = next_url
        return nil if url.nil?
        if block_given?
          return yield(url)
        else
          return client.get(url)
        end
      end

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

        responses = [self]
        counter = 0

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

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

        return responses
      end

      def all_pages(&block)
        return next_pages(count: nil, &block)
      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

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

        resp = self
        loop do
          resp = resp.next_page
          break if resp.nil?
          resp.each(&block)
        end
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 4 rubygems

Version Path
fastlane-2.225.0 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.224.0 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.223.1 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.223.0 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.222.0 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.221.1 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.221.0 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.220.0 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.219.0 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.218.0 spaceship/lib/spaceship/connect_api/response.rb
fastlane-security-patched-2.216.0 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.217.0 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.216.0 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.215.1 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.215.0 spaceship/lib/spaceship/connect_api/response.rb
fastlane-mercafacil-2.214.0 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.214.0 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.213.0 spaceship/lib/spaceship/connect_api/response.rb
fastlane-2.212.2 spaceship/lib/spaceship/connect_api/response.rb
fastlane_pricing_fix-2.212.1 spaceship/lib/spaceship/connect_api/response.rb