Class: Parliament::Request::UrlRequest
- Inherits:
-
BaseRequest
- Object
- BaseRequest
- Parliament::Request::UrlRequest
- Defined in:
- lib/parliament/request/url_request.rb
Overview
URL request object, allowing the user to build a URL to make a request to an API.
Constant Summary
Constants inherited from BaseRequest
BaseRequest::CONNECTTIMEOUT, BaseRequest::TIMEOUT
Instance Attribute Summary
Attributes inherited from BaseRequest
Instance Method Summary collapse
-
#initialize(base_url: nil, headers: nil, builder: nil, decorators: nil) ⇒ UrlRequest
constructor
Creates a new instance of Parliament::Request::UrlRequest.
-
#method_missing(method, *params, &block) ⇒ Parliament::Request::UrlRequest
Overrides Ruby's method_missing to allow creation of URLs through method calls.
- #query_url ⇒ Object
-
#respond_to_missing?(_, _ = false) ⇒ Boolean
This class always responds to method calls, even those missing.
-
#set_url_params(params) ⇒ Parliament::Request::UrlRequest
Self (this is to allow method chaining).
Methods inherited from BaseRequest
Constructor Details
#initialize(base_url: nil, headers: nil, builder: nil, decorators: nil) ⇒ UrlRequest
Creates a new instance of Parliament::Request::UrlRequest.
request = Parliament::Request::UrlRequest.new(base_url: 'http://example.com', headers: { 'Access-Token' => '12345678' }) This will create a request with the Access-Token set to 12345678.
20 21 22 23 24 25 |
# 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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *params, &block) ⇒ Parliament::Request::UrlRequest
Overrides Ruby's method_missing to allow creation of URLs through method calls.
52 53 54 55 56 57 58 59 60 61 |
# 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 |
Instance Method Details
#query_url ⇒ Object
70 71 72 73 74 75 76 77 |
# 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 |
#respond_to_missing?(_, _ = false) ⇒ Boolean
This class always responds to method calls, even those missing. Therefore, respond_to_missing? always returns true.
66 67 68 |
# File 'lib/parliament/request/url_request.rb', line 66 def respond_to_missing?(_, _ = false) true # responds to everything, always end |
#set_url_params(params) ⇒ Parliament::Request::UrlRequest
Returns self (this is to allow method chaining).
80 81 82 83 84 |
# File 'lib/parliament/request/url_request.rb', line 80 def set_url_params(params) @query_params = @query_params.merge(params) self end |