Sha256: 4679581fb7cd5e46f048371b549b8e11b9222b20f3d0aacdb0fa8ba3776d542c

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

module BranchIO
  class Client
    class ErrorApiCallFailed < StandardError
      attr_reader :response
      def initialize(response)
        @response = response
        super("API call failed")
      end
    end

    class Response
      attr_reader :response

      def initialize(response)
        @response = response
      end

      def request
        response.request
      end

      def success?
        response.success?
      end

      def failure?
        !success?
      end

      def validate!
        raise ErrorApiCallFailed.new(self) unless success?
      end

      def json
        response.parsed_response
      end
    end

    class UrlResponse < Response
      def success?
        true
      end

      def url
        json["url"]
      end
    end

    class ErrorResponse < Response
      def success?
        false
      end

      def code
        json["code"]
      end

      def error
        json["error"]
      end
    end

    class BulkUrlsResponse < Response
      def success?
        responses.all?(&:success?)
      end

      def urls
        successful_responses.map(&:url)
      end

      def errors
        erroneous_responses.map(&:error)
      end

      def successful_responses
        responses.select(&:success?)
      end

      def erroneous_responses
        responses.reject(&:success?)
      end

      def responses
        @responses ||= json.map do |url_info|
          # below the EmbeddedResponseWrapper(s) act as a dummp HTTParty response
          if url_info.has_key?("error")
            ErrorResponse.new(EmbeddedResponseWrapper.new(url_info))
          else
            UrlResponse.new(EmbeddedResponseWrapper.new(url_info))
          end
        end
      end

      private

      class EmbeddedResponseWrapper < Struct.new(:parsed_response); end
    end

    class LinkPropertiesResponse < Response
      def link_properties
        @link_properties ||= BranchIO::LinkProperties.new(json)
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
branch_io-0.1.0 lib/branch_io/client/response.rb