Class: A2::Filtered
- Inherits:
-
CmdParse::Command
- Object
- CmdParse::Command
- A2::Filtered
show all
- Defined in:
- lib/a2/mixins/filtered.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name, opts = {}) ⇒ Filtered
Returns a new instance of Filtered.
4
5
6
7
8
|
# File 'lib/a2/mixins/filtered.rb', line 4
def initialize(name, opts = {})
set_custom_opts!(opts)
super(name, takes_commands: opts[:takes_commands])
set_filter_optparse_options!(options, @query_filter)
end
|
Instance Attribute Details
#filter_key ⇒ Object
Returns the value of attribute filter_key.
3
4
5
|
# File 'lib/a2/mixins/filtered.rb', line 3
def filter_key
@filter_key
end
|
#query_filter ⇒ Object
Returns the value of attribute query_filter.
3
4
5
|
# File 'lib/a2/mixins/filtered.rb', line 3
def query_filter
@query_filter
end
|
Instance Method Details
#generate_json_filters ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/a2/mixins/filtered.rb', line 48
def generate_json_filters
if @opt[:json_file]
file_contents = File.read(@opt[:json_file])
JSON.parse(file_contents).to_json
elsif @opt[:filters]
@opt[:filters] = parse_filters(@opt[:filters])
@opt.to_json
else
command_parser.main_command.commands['help'].execute(*command_parser.current_command.command_chain.map(&:name))
abort('Must provide one of either "--json-file" or "--filters"')
end
end
|
#generate_query_filters ⇒ Object
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/a2/mixins/filtered.rb', line 61
def generate_query_filters
query = []
@opt[:filters].split(',').each do |filter|
query << "filter=#{filter}"
end if @opt[:filters]
query << "start=#{@opt[:start]}" if @opt[:start]
query << "end=#{@opt[:end]}" if @opt[:end]
query_string = query.join('&')
query_string.prepend('?')
end
|
#parse_filters(filters) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/a2/mixins/filtered.rb', line 36
def parse_filters(filters)
parsed_filters = []
filters.split(',').each do |f|
k,*v = f.split(':')
parsed_filters << {
@filter_key => k,
'values' => [v.join(':')]
}
end
parsed_filters
end
|
#set_custom_opts!(opts) ⇒ Object
10
11
12
13
14
|
# File 'lib/a2/mixins/filtered.rb', line 10
def set_custom_opts!(opts)
@opt ||= {}
@query_filter = opts.delete(:query_filter) || false
@filter_key = opts.delete(:filter_key) || 'key'
end
|
#set_filter_optparse_options!(options, query_filter) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/a2/mixins/filtered.rb', line 16
def set_filter_optparse_options!(options, query_filter)
options.on('-f', '--filters FILTERS', 'A comma-separated list of filters.') do |filters|
@opt[:filters] = filters
end
if query_filter
options.on('-S', '--start-time START', 'Earliest most recent check-in node information to return. Formatted iso8601 (YYYY-MM-DD\'T\'HH:mm:ssZ)') do |start_time|
@opt[:start] = start_time
end
options.on('-E', '--end-time END', 'Latest most recent check-in node information to return. Formatted iso8601 (YYYY-MM-DD\'T\'HH:mm:ssZ)') do |end_time|
@opt[:end] = end_time
end
else
options.on('-j', '--json-file FILE', 'Path to a json file containing a filter payload.') do |file|
@opt[:json_file] = file
end
end
end
|
#with_filter_query {|query_string| ... } ⇒ Object
72
73
74
75
|
# File 'lib/a2/mixins/filtered.rb', line 72
def with_filter_query(&block)
query_string = generate_query_filters
yield(query_string)
end
|