Class: Bitly::HTTP::Request
- Inherits:
-
Object
- Object
- Bitly::HTTP::Request
- Defined in:
- lib/bitly/http/request.rb
Instance Attribute Summary collapse
-
#headers ⇒ Hash
readonly
A hash of HTTP headers that will be included with the request.
-
#method ⇒ String
readonly
The HTTP method that the request should be.
-
#params ⇒ Hash
readonly
A hash of parameters that will be turned into query parameters or a request body.
Instance Method Summary collapse
-
#body ⇒ String
Returns the body of the request if the request is an HTTP method that uses a body to send data.
-
#initialize(uri:, method: "GET", params: {}, headers: {}) ⇒ Request
constructor
Creates a new Bitly::HTTP::Request object, which is to be used by the [Bitly::HTTP::Client].
-
#uri ⇒ URI
Returns the uri for the request.
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].
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
#headers ⇒ Hash (readonly)
Returns 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 |
#method ⇒ String (readonly)
Returns The HTTP method that the request should be.
9 10 11 |
# File 'lib/bitly/http/request.rb', line 9 def method @method end |
#params ⇒ Hash (readonly)
Returns 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
#body ⇒ String
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.
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 |
#uri ⇒ URI
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.
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 |