Sha256: d7a199b0ec89204a1edfb32ad083bea4b1ad861f94fba9b651cdec7518397f00

Contents?: true

Size: 958 Bytes

Versions: 2

Compression:

Stored size: 958 Bytes

Contents

require "faraday"

module RiseAi
  class Resource
    def post_request(url, body:, headers: {})
      handle_response(connection.post(url, body, headers))
    end

    private

    def handle_response(response)
      error_message = response.body

      case response.status
      when 401
        raise Error, "Invalid authorization credentials: #{error_message}"
      when 404
        raise Error, "Not found: #{error_message}"
      when 422
        raise Error, "Unprocessable entity: #{error_message}"
      when 406
        raise Error,
          "Not acceptable. Missing mandatory fields: #{error_message}"
      when 409
        raise Error, "Conflict: #{error_message}"
      when 503
        raise Error, "Timed out: #{error_message}"
      end

      response
    end

    def connection
      @connection ||=
        Faraday.new do |conn|
          conn.request :json
          conn.adapter Faraday.default_adapter
        end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rise_ai-0.1.1 lib/rise_ai/resource.rb
rise_ai-0.1.0 lib/rise_ai/resource.rb