Class: Bitly::HTTP::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/bitly/http/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri:, method: "GET", params: {}, headers: {}) ⇒ Request

Creates a new Bitly::HTTP::Request object, which is to be used by the [Bitly::HTTP::Client].

Examples:

request = Bitly::HTTP::Request.new(uri: URI.parse('https://api-ssl.bitly.com/v3/shorten'), method: "GET")

Parameters:

  • uri (URI)

    A [URI] that you want to make the request to.

  • method (String) (defaults to: "GET")

    The HTTP method that should be used to make the request.

  • params (Hash) (defaults to: {})

    The parameters to be sent as part of the request. GET parameters will be sent as part of the query string and other methods will be added to the request body.

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bitly/http/request.rb', line 32

def initialize(uri: , method: "GET", params: {}, headers: {})
  errors = []
  @uri = uri
  errors << "uri must be an object of type URI. Received a #{uri.class}" unless uri.kind_of?(URI)
  @method = method
  errors << "method must be a valid HTTP method. Received: #{method}." unless HTTP_METHODS.include?(method)
  @params = params
  errors << "params must be a hash. Received: #{params.inspect}." unless params.kind_of?(Hash)
  @headers = headers
  errors << "headers must be a hash. Received: #{headers.inspect}." unless headers.kind_of?(Hash)
  raise ArgumentError, errors.join("\n") if errors.any?
end

Instance Attribute Details

#headersHash (readonly)

Returns A hash of HTTP headers that will be included with the request.

Returns:

  • (Hash)

    A hash of HTTP headers that will be included with the request



17
18
19
# File 'lib/bitly/http/request.rb', line 17

def headers
  @headers
end

#methodString (readonly)

Returns The HTTP method that the request should be.

Returns:

  • (String)

    The HTTP method that the request should be.



9
10
11
# File 'lib/bitly/http/request.rb', line 9

def method
  @method
end

#paramsHash (readonly)

Returns A hash of parameters that will be turned into query parameters or a request body.

Returns:

  • (Hash)

    A hash of parameters that will be turned into query parameters or a request body



13
14
15
# File 'lib/bitly/http/request.rb', line 13

def params
  @params
end

Instance Method Details

#bodyString

Returns the body of the request if the request is an HTTP method that uses a body to send data. The body is a JSON string of the parameters. If the request doesn’t use a body to send data, this returns nil.

Examples:

uri = URI.parse("https://api-ssl.bitly.com/v3/shorten")
request = Bitly::HTTP::Request.new(uri: uri, method: 'POST', params: { foo: "bar" })
request.body
# => "{\"foo\":\"bar\"}"

Returns:

  • (String)

    The request body



83
84
85
86
# File 'lib/bitly/http/request.rb', line 83

def body
  return nil if HTTP_METHODS_WITHOUT_BODY.include?(@method)
  return JSON.generate(params)
end

#uriURI

Returns the uri for the request. If the request is an HTTP method that uses a body to send data, then the uri is the one that the request was initialised with. If the request uses query parameters, then the parameters are serialised and added to the uri’s query.

Examples:

uri = URI.parse("https://api-ssl.bitly.com/v3/shorten")
request = Bitly::HTTP::Request.new(uri: uri, params: { foo: "bar" })
request.uri.to_s
# => "https://api-ssl.bitly.com/v3/shorten?foo=bar"

Returns:

  • (URI)

    The full URI for the request



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bitly/http/request.rb', line 58

def uri
  uri = @uri.dup
  return uri if HTTP_METHODS_WITH_BODY.include?(@method)
  if uri.query
    existing_query = URI.decode_www_form(uri.query)
    new_query = hash_to_arrays(@params)
    uri.query = URI.encode_www_form((existing_query + new_query).uniq)
  else
    uri.query = URI.encode_www_form(@params) if @params.any?
  end
  uri
end