Module: A2::Filtered
- Included in:
- Paginated, Subcommand::Infra::ListNodeStatusCounts
- Defined in:
- lib/a2/mixins/filtered.rb
Instance Method Summary collapse
- #generate_json_filters ⇒ Object
- #generate_query_filters ⇒ Object
- #initialize(name, opts = {}) ⇒ Object
- #parse_filters(filters) ⇒ Object
- #query_filter? ⇒ Boolean
- #set_custom_opts!(opts) ⇒ Object
- #set_filter_optparse_options!(options) ⇒ Object
- #with_filter_query {|query_string| ... } ⇒ Object
Instance Method Details
#generate_json_filters ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/a2/mixins/filtered.rb', line 47 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
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/a2/mixins/filtered.rb', line 60 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 |
#initialize(name, opts = {}) ⇒ Object
3 4 5 6 7 |
# File 'lib/a2/mixins/filtered.rb', line 3 def initialize(name, opts = {}) set_custom_opts!(opts) super(name, opts) () end |
#parse_filters(filters) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/a2/mixins/filtered.rb', line 35 def parse_filters(filters) parsed_filters = [] filters.split(',').each do |f| k,v = f.split(':') parsed_filters << { 'key' => k, 'values' => [v] } end parsed_filters end |
#query_filter? ⇒ Boolean
31 32 33 |
# File 'lib/a2/mixins/filtered.rb', line 31 def query_filter? @opt[:query_filter] end |
#set_custom_opts!(opts) ⇒ Object
9 10 11 12 |
# File 'lib/a2/mixins/filtered.rb', line 9 def set_custom_opts!(opts) @opt ||= {} @opt[:query_filter] = opts.delete(:query_filter) || false end |
#set_filter_optparse_options!(options) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/a2/mixins/filtered.rb', line 14 def () .on('-f', '--filters FILTERS', 'A comma-separated list of filters.') do |filters| @opt[:filters] = filters end # Only available for POST body filters .on('-j', '--json-file FILE', 'Path to a json file containing a filter payload.') do |file| @opt[:json_file] = file end unless query_filter? # Only available for query parameter filters .on('-S', '--start START', 'Earliest most recent check-in node information to return. Formatted iso8601 (YYYY-MM-DD\'T\'HH:mm:ssZ)') do |start| @opt[:start] = start end if query_filter? .on('-E', '--end END', 'Latest most recent check-in node information to return. Formatted iso8601 (YYYY-MM-DD\'T\'HH:mm:ssZ)') do |end_arg| @opt[:end] = end_arg end if query_filter? end |
#with_filter_query {|query_string| ... } ⇒ Object
71 72 73 74 |
# File 'lib/a2/mixins/filtered.rb', line 71 def with_filter_query(&block) query_string = generate_query_filters yield(query_string) end |