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.

See Also:

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#filtersHash (readonly)

Returns The filters to apply to the query.

Returns:

  • (Hash)

    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.

Returns:

  • ('asc', 'desc')

    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_fieldString (readonly)

Returns The field to use for the list order.

Returns:

  • (String)

    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

#pageInteger, String (readonly)

Returns The page to fetch.

Returns:

  • (Integer, String)

    The page to fetch.



13
14
15
# File 'lib/kaze_client/request/requests/utils/list_request.rb', line 13

def page
  @page
end

#per_pageInteger (readonly)

Returns The number of items to fetch by page; between 1 and 100.

Returns:

  • (Integer)

    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).

Parameters:

  • direction ('asc', 'desc')

    Set the direction of the order.

Returns:



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).

Parameters:

  • field (String)

    Set the field to use for the list order

Returns:



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).

Parameters:

  • page (Integer, String)

    Set the page to fetch

Returns:



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).

Parameters:

  • per_page (Integer, String)

    Set the number of items to fetch per page

Returns:



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.

Examples:

To filter on id then on name

filter_by_id(2).filter_by_name('foo')

Parameters:

  • value (String)

    Add a filter to the query.

Returns:



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

Returns:

  • (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