Module: KazeClient::Utils::ListRequest
- Included in:
- Collection::ItemsRequest, CollectionsRequest, JobWorkflowsRequest, JobsRequest, PartnersRequest, KazeClient::UsersRequest
- Defined in:
- lib/kaze_client/request/requests/utils/list_request.rb
Overview
Included by the request where a list will be returned.
Instance Attribute Summary collapse
-
#filters ⇒ Hash
readonly
The filters to apply to the query.
-
#order_direction ⇒ 'asc', 'desc'
readonly
The direction of the order.
-
#order_field ⇒ String
readonly
The field to use for the list order.
-
#page ⇒ Integer, String
readonly
The page to fetch.
-
#per_page ⇒ Integer
readonly
The number of items to fetch by page; between 1 and 100.
Instance Method Summary collapse
-
#add_order_direction(direction) ⇒ KazeClient::Utils::ListRequest
Self (to chain methods).
-
#add_order_field(field) ⇒ KazeClient::Utils::ListRequest
Self (to chain methods).
-
#add_page(page) ⇒ KazeClient::Utils::ListRequest
Self (to chain methods).
-
#add_per_page(per_page) ⇒ KazeClient::Utils::ListRequest
Self (to chain methods).
-
#filter_by_id(value) ⇒ KazeClient::Utils::ListRequest
This is an example.
- #initialize(method, url) ⇒ Object
- #method_missing(method_name, *args, &block) ⇒ Object
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/kaze_client/request/requests/utils/list_request.rb', line 91 def method_missing(method_name, *args, &block) match_data = method_name.match(/^filter_by_(\w+)/) # Match_data[0] is the whole match while match_data[1] is the first capture group; here # it is the field to filter on. # Only the first given argument is considered to be the value to filter on. Possible # subsequent arguments are ignored. match_data ? filter_by(match_data[1], args[0]) : super end |
Instance Attribute Details
#filters ⇒ Hash (readonly)
Returns The filters to apply to the query.
25 26 27 |
# File 'lib/kaze_client/request/requests/utils/list_request.rb', line 25 def filters @filters end |
#order_direction ⇒ 'asc', 'desc' (readonly)
Returns The direction of the order.
22 23 24 |
# File 'lib/kaze_client/request/requests/utils/list_request.rb', line 22 def order_direction @order_direction end |
#order_field ⇒ String (readonly)
Returns The field to use for the list order.
19 20 21 |
# File 'lib/kaze_client/request/requests/utils/list_request.rb', line 19 def order_field @order_field end |
#page ⇒ Integer, String (readonly)
Returns The page to fetch.
13 14 15 |
# File 'lib/kaze_client/request/requests/utils/list_request.rb', line 13 def page @page end |
#per_page ⇒ Integer (readonly)
Returns The number of items to fetch by page; between 1 and 100.
16 17 18 |
# File 'lib/kaze_client/request/requests/utils/list_request.rb', line 16 def per_page @per_page end |
Instance Method Details
#add_order_direction(direction) ⇒ KazeClient::Utils::ListRequest
Returns self (to chain methods).
75 76 77 78 79 80 81 82 |
# File 'lib/kaze_client/request/requests/utils/list_request.rb', line 75 def add_order_direction(direction) # Ensure the +direction+ parameter is valid; default direction is ascendant @order_direction = %w[asc desc].include?(direction.to_s) ? direction.to_s : 'asc' @query[:order_direction] = @order_direction self end |
#add_order_field(field) ⇒ KazeClient::Utils::ListRequest
Returns self (to chain methods).
66 67 68 69 70 71 |
# File 'lib/kaze_client/request/requests/utils/list_request.rb', line 66 def add_order_field(field) @order_field = field @query[:order_field] = @order_field self end |
#add_page(page) ⇒ KazeClient::Utils::ListRequest
Returns self (to chain methods).
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/kaze_client/request/requests/utils/list_request.rb', line 36 def add_page(page) # Ensure the +per_page+ parameter is valid; default is 1 @page = if !page.is_a?(Numeric) || page.to_i < 1 1 else page end @query[:page] = @page self end |
#add_per_page(per_page) ⇒ KazeClient::Utils::ListRequest
Returns self (to chain methods).
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/kaze_client/request/requests/utils/list_request.rb', line 51 def add_per_page(per_page) # Ensure the +per_page+ parameter is between 1 and 100 @per_page = if !per_page.is_a?(Numeric) || per_page.to_i < 1 1 else [per_page, 100].min end @query[:per_page] = @per_page self end |
#filter_by_id(value) ⇒ KazeClient::Utils::ListRequest
This is an example. Any method beginning with filter_by_
is valid and defined using method_missing
. This adds a filter on the string following filter_by_
to the query.
91 92 93 94 95 96 97 98 99 |
# File 'lib/kaze_client/request/requests/utils/list_request.rb', line 91 def method_missing(method_name, *args, &block) match_data = method_name.match(/^filter_by_(\w+)/) # Match_data[0] is the whole match while match_data[1] is the first capture group; here # it is the field to filter on. # Only the first given argument is considered to be the value to filter on. Possible # subsequent arguments are ignored. match_data ? filter_by(match_data[1], args[0]) : super end |
#initialize(method, url) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/kaze_client/request/requests/utils/list_request.rb', line 27 def initialize(method, url) super(method, url) @query ||= {} @filters ||= {} end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
101 102 103 |
# File 'lib/kaze_client/request/requests/utils/list_request.rb', line 101 def respond_to_missing?(method_name, include_private=false) method_name.match?(/^filter_by_/) || super end |