class Parliament::Request::UrlRequest
URL request object, allowing the user to build a URL to make a request to an API.
@since 0.7.5
Public Class Methods
Creates a new instance of Parliament::Request::UrlRequest
.
@see Parliament::Request::BaseRequest#initialize.
@param [String] base_url the base url of our api. (expected: example.com - without the trailing slash). @param [Hash] headers the headers being sent in the request. @param [Parliament::Builder] builder the builder to use in order to build a response. @param [Module] decorators the decorator module to use in order to provide possible alias methods for any objects created by the builder. @example Passing headers
request = Parliament::Request::UrlRequest.new
(base_url: 'example.com', headers: { 'Access-Token' => '12345678' }) This will create a request with the Access-Token set to 12345678.
Parliament::Request::BaseRequest::new
# File lib/parliament/request/url_request.rb, line 20 def initialize(base_url: nil, headers: nil, builder: nil, decorators: nil) @endpoint_parts = [] base_url ||= ENV['PARLIAMENT_BASE_URL'] super end
Public Instance Methods
Overrides Ruby's method_missing
to allow creation of URLs through method calls.
@example Adding a simple URL part
request = Parliament::Request::UrlRequest.new(base_url: 'http://example.com') # url: http://example.com/people request.people
@example Adding a simple URL part with parameters
request = Parliament::Request::UrlRequest.new(base_url: 'http://example.com') # url: http://example.com/people/123456 request.people('123456')
@example Chaining URL parts and using hyphens
request = Parliament::Request::UrlRequest.new(base_url: 'http://example.com') # url: http://example.com/people/123456/foo/bar/hello-world/7890 request.people('123456').foo.bar('hello-world', '7890')
@param [Symbol] method the 'method' (url part) we are processing. @param [Array<Object>] params parameters passed to the specified method (url part). @param [Block] block additional block (kept for compatibility with method_missing
interface).
@return [Parliament::Request::UrlRequest] self (this is to allow method chaining).
# File lib/parliament/request/url_request.rb, line 52 def method_missing(method, *params, &block) @endpoint_parts << method.to_s @endpoint_parts << params @endpoint_parts = @endpoint_parts.flatten! block&.call self || super end
# File lib/parliament/request/url_request.rb, line 70 def query_url uri_string = [@base_url, @endpoint_parts].join('/').gsub(' ', '%20') uri = URI.parse(uri_string) uri.query = URI.encode_www_form(@query_params) unless @query_params.empty? uri.to_s end
This class always responds to method calls, even those missing. Therefore, respond_to_missing? always returns true.
@return [Boolean] always returns true.
# File lib/parliament/request/url_request.rb, line 66 def respond_to_missing?(_, _ = false) true # responds to everything, always end
@return [Parliament::Request::UrlRequest] self (this is to allow method chaining).
# File lib/parliament/request/url_request.rb, line 80 def set_url_params(params) @query_params = @query_params.merge(params) self end