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 collapse
-
#base_url ⇒ String
readonly
the endpoint for our API which we will build our requests on.
-
#headers ⇒ Hash
readonly
the headers being sent in the request.
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: ‘example.com’, headers: { ‘Access-Token’ => ‘12345678’ }) This will create a request with the Access-Token set to 12345678.
22 23 24 25 26 27 |
# File 'lib/parliament/request/url_request.rb', line 22 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.
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/parliament/request/url_request.rb', line 54 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 Attribute Details
#base_url ⇒ String (readonly)
the endpoint for our API which we will build our requests on. (expected: example.com - without the trailing slash).
9 10 11 |
# File 'lib/parliament/request/url_request.rb', line 9 def base_url @base_url end |
#headers ⇒ Hash (readonly)
the headers being sent in the request.
9 10 11 |
# File 'lib/parliament/request/url_request.rb', line 9 def headers @headers end |
Instance Method Details
#query_url ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/parliament/request/url_request.rb', line 72 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.
68 69 70 |
# File 'lib/parliament/request/url_request.rb', line 68 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).
82 83 84 85 86 |
# File 'lib/parliament/request/url_request.rb', line 82 def set_url_params(params) @query_params = @query_params.merge(params) self end |