Sha256: eb9d48fa3edb61ff4d3237e832f4311b4e8dfc51b97915bec50c46ed0f67aa0c

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module LoopsSdk
  class Base
    class << self
      private

      def handle_response(response)
        case response.status
        when 200
          JSON.parse(response.body)
        when 400, 404, 405, 409, 500
          raise APIError.new(response.status, response.body)
        else
          raise APIError.new(response.status, "Unexpected error occurred")
        end
      end

      def make_request(method, path, params = {}, body = nil)
        response = LoopsSdk.configuration.connection.send(method, path, params) do |req|
          req.body = body.to_json if body
        end
        handle_response(response)
      end
    end
  end

  # The `APIError` class in Ruby represents an error that occurs during an API request with specific
  # status and response body information.
  class APIError < StandardError
    attr_reader :status, :body

    def initialize(status, body)
      @status = status
      @body = body
      super("API request failed with status #{status}: #{body}")
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
loops_sdk-0.1.2 lib/loops_sdk/base.rb
loops_sdk-0.1.0 lib/loops_sdk/base.rb