Class: A2::Filtered

Inherits:
CmdParse::Command
  • Object
show all
Defined in:
lib/a2/mixins/filtered.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ Filtered

Returns a new instance of Filtered.



3
4
5
6
7
# File 'lib/a2/mixins/filtered.rb', line 3

def initialize(name, opts = {})
  set_custom_opts!(opts)
  super(name, opts)
  set_filter_optparse_options!(options, @query_filter)
end

Instance Method Details

#generate_json_filtersObject



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_filtersObject



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

#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 << {
      @filter_key => k,
      'values' => [v.join(':')]
    }
  end
  parsed_filters
end

#set_custom_opts!(opts) ⇒ Object



9
10
11
12
13
# File 'lib/a2/mixins/filtered.rb', line 9

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/a2/mixins/filtered.rb', line 15

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
    # Only available for query parameter filters
    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
    # Only available for POST body filters
    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

Yields:

  • (query_string)


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