Sha256: a1645e77af0def243724064ed0196e01159282ac3eae987240fa564841a3171e

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

# Understands how to represent the result of a request to the Pipedrive API
module Pipekit
  class Result

    def initialize(response_data)
      @response_data = response_data
      raise UnsuccessfulRequestError.new(response_data) unless success?
    end

    def response(resource)
      raise ResourceNotFoundError.new(response_data) unless resource_found?
      return Response.new(resource, response_body) unless response_body.is_a? Array
      response_body.map { |data| Response.new(resource, data) }
    end

    def +(other)
      self.class.new(other.merged_response(response_body))
    end

    def fetch_next_request?
      Config.fetch("request_all_pages", true) && pagination_data["more_items_in_collection"]
    end

    def next_start
      pagination_data["next_start"]
    end

    def self.response(resource, response_data)
      new(response_data).response(resource)
    end

    protected

    def merged_response(other_body)
      response_data.tap do |response|
        response["data"] = other_body + response_body
      end
    end

    private

    attr_reader :response_data

    def pagination_data
      response_data
        .fetch("additional_data", {})
        .fetch("pagination", {})
    end

    def response_body
      response_data["data"]
    end

    def success?
      response_data["success"]
    end

    def resource_found?
      !(response_body.nil? || response_body.empty?)
    end
  end

  class ResourceNotFoundError < StandardError
    def initialize(response)
      @response = response
    end

    def message
      "Resource not found: #{@response}"
    end
  end

  class UnsuccessfulRequestError < StandardError
    def initialize(response)
      @response = response
    end

    def message
      "Request not successful: #{@response}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pipekit-1.0.0 lib/pipekit/result.rb