class BaseOperators::BaseDSLOperators
Attributes
page[R]
Public Instance Methods
api_key(key)
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 16 def api_key(key) @api_key = key self end
fetch_info()
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 132 def fetch_info exit(0) if @error response = Net::HTTP.get(URI.parse(@path + "?uid=#{@uid}")) @info = JSON.parse(response) @info[@tricorder.to_s].each_key do |key,val| @info[@tricorder.to_s].compact! @info[@tricorder.to_s].delete_if { |_k, v| v == false } if @info[@tricorder.to_s][key].is_a?(Array) @info[@tricorder.to_s][key].each_with_index do |_, index| @info[@tricorder.to_s][key][index].compact! @info[@tricorder.to_s][key][index].delete_if { |_k, v| v == false } end end end end
get_result_number(num)
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 104 def get_result_number(num) exit(0) if @error begin @result = @results[num - 1] set_uid(@result['uid']) rescue ap "No results found!" @error = true end self end
humanized_tricorder_name()
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 202 def humanized_tricorder_name exit(0) if @error init_error_messages @tricorder.to_s.split('_').map(&:capitalize).join(' ') end
init_error_messages()
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 26 def init_error_messages if @tricorder.nil? || !Tricorders::TRICORDERS.include?(@tricorder) ap "Invalid Tricorder Name! #{@tricorder}" ap "Set the correct tricorder via 'set_tricorder(name)'" ap "Tricorders can either be #{Tricorders::TRICORDERS}" @error = true end if @subject.nil? ap 'No Subject Defined!' ap "Set a subject via 'set_subject(name)'" @error = true end exit(0) if @error end
log(message, options = {})
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 54 def log(message, options = {}) return unless options[:verbose] || @verbose if options[:pager] self.send(options[:pager], message) else ap(message) end end
merge_results()
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 97 def merge_results exit(0) if @error @results = @results.inject(:merge) self end
no_logging()
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 43 def no_logging @verbose = false self end
params()
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 73 def params { apiKey: @api_key, pageSize: 1000 } end
preprocessor(*preprocessors)
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 48 def preprocessor(*preprocessors) @preprocessors = preprocessors.flatten self end
print_all_info()
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 265 def print_all_info @results.each do |result| set_uid(result['uid']) fetch_info printer(@info) end self end
print_all_results()
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 282 def print_all_results printer(@results) self end
print_info(*fields)
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 230 def print_info(*fields) collection, field, number = fields.flatten num = 0 || number @results.each do |res| set_uid(res['uid']) fetch_info begin info = @info[@tricorder.to_s] print_info = if field info[collection][num][field] else info[collection] end if @print_only_once @out ||= print_info else @out = print_info end printer(@out) unless @printed @printed = true if @print_only_once rescue log "Collection not found or empty" log "Available collections are #{@info.delete_if { |_k, v| v == false || v.is_a?(Array) && v&.empty? }.keys.join(", ")}" end end self end
print_only_once()
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 126 def print_only_once @print_only_once = true self end
print_result()
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 276 def print_result printer(@result) self end
printer(object)
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 210 def printer(object) exit(0) if @error init_error_messages @object = object if object.empty? ap "No info found!" else if @preprocessors @preprocessors.each do |preprocessor| self.send(preprocessor.to_sym) unless self.instance_variable_get("@#{preprocessor}".to_sym) end else print_object end end end
search()
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 164 def search exit(0) if @error log "Searching '#{humanized_tricorder_name}' databank for '#{@subject}'..." search_params = params.merge( name: @subject, title: @subject, description: @subject ) response = Net::HTTP.post_form(URI.parse(@path + '/search'), search_params) @search << JSON.parse(response.body) if @search[0].key?('code') ap @search[0]['code'] @error = true end @search.flatten! count = @search.count 0.upto(count).each do |c| next if @search.nil? || @search[c].nil? || @search[c][@collection].nil? @results << @search[c][@collection] @results.flatten! @results.each_with_index do |result, index| result.compact! result.delete_if { |_k, v| v == false } @results[index] = result end end self end
search_locations(*tricorders)
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 87 def search_locations(*tricorders) exit(0) if @error tricorders.flatten.each do |location| self.send("search_#{location.to_s}".to_sym) end self end
set_collection(collection)
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 80 def set_collection(collection) exit(0) if @error @collection = collection self end
set_format(type)
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 151 def set_format(type) formats = [:raw, :plain, :json, :html] if formats.include?(type.to_sym) @format = type.to_sym else ap "Invalid format #{@format}. Valid formats are #{formats.join(', ')}" @error = true end self end
set_path(path)
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 64 def set_path(path) @path = path self end
set_storedir(path)
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 69 def set_storedir(path) @storedir = path end
set_subject(name)
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 10 def set_subject(name) @subject = name self end
set_tricorder(name)
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 21 def set_tricorder(name) @tricorder = name.to_sym self end
set_uid(uid)
click to toggle source
# File lib/dsl/operators/base_operators.rb, line 118 def set_uid(uid) exit(0) if @error @uid = uid self end