class Bitly::HTTP::Response

The Response class handles generic responses from the API. It is made up of a status code, body and headers. The body is expected to be JSON and it will parse the body. The status should lie within the range 100 - 599 and the headers should be a hash.

Attributes

body[R]

@return [Hash] The response's parsed body

headers[R]

@return [Hash] The response's headers

request[R]

@return [Bitly::HTTP::Request] The request that caused this response

status[R]

@return [String] The response's status code

Public Class Methods

new(status:, body:, headers:, request: nil) click to toggle source

Creates a new Bitly::HTTP::Response object, which can be used by other objects in the library.

@example

response = Bitly::HTTP::Response.new(status: "200", body: "{}", headers: {})

@param [String] status The status code of the response, which should be

between 100 and 599

@param [String] body The body of the response, a String that is valid

JSON and will be parsed

@param [Hash] headers The response headers

# File lib/bitly/http/response.rb, line 37
def initialize(status:, body:, headers:, request: nil)
  errors = []
  @status = status
  errors << "Status must be a valid HTTP status code. Received #{status}" unless is_status?(status)
  if body.nil? || body.empty?
    @body = nil
  else
    begin
      @body = JSON.parse(body)
    rescue JSON::ParserError
      @body = {
        "message" => body
      }
    end
  end
  @headers = headers
  errors << "Headers must be a hash. Received #{headers}" unless headers.is_a?(Hash)
  @request = request
  errors << "Request must be a Bitly::HTTP::Request. Received #{request}" if request && !request.is_a?(Request)
  raise ArgumentError, errors.join("\n"), caller if errors.any?
end

Private Instance Methods

is_status?(status) click to toggle source
# File lib/bitly/http/response.rb, line 61
def is_status?(status)
  !!status.match(/\A[1-5][0-9][0-9]\z/)
end