Class: BaseOperators::BaseDSLOperators
- Inherits:
-
Object
- Object
- BaseOperators::BaseDSLOperators
- Defined in:
- lib/dsl/operators/base_operators.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#page ⇒ Object
readonly
Returns the value of attribute page.
Instance Method Summary collapse
- #api_key(key) ⇒ Object
- #fetch_info ⇒ Object
- #get_result_number(num) ⇒ Object
- #humanized_tricorder_name ⇒ Object
- #init_error_messages ⇒ Object
- #log(message, options = {}) ⇒ Object
- #merge_results ⇒ Object
- #no_logging ⇒ Object
- #params ⇒ Object
- #print_all_info ⇒ Object
- #print_all_results ⇒ Object
- #print_info(*fields) ⇒ Object
- #print_only_once ⇒ Object
- #print_result ⇒ Object
- #printer(object) ⇒ Object
- #search ⇒ Object
- #search_locations(*tricorders) ⇒ Object
- #set_collection(collection) ⇒ Object
- #set_path(path) ⇒ Object
- #set_storedir(path) ⇒ Object
- #set_subject(name) ⇒ Object
- #set_tricorder(name) ⇒ Object
- #set_uid(uid) ⇒ Object
Instance Attribute Details
#page ⇒ Object (readonly)
Returns the value of attribute page.
7 8 9 |
# File 'lib/dsl/operators/base_operators.rb', line 7 def page @page end |
Instance Method Details
#api_key(key) ⇒ Object
15 16 17 18 |
# File 'lib/dsl/operators/base_operators.rb', line 15 def api_key(key) @api_key = key self end |
#fetch_info ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/dsl/operators/base_operators.rb', line 125 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) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/dsl/operators/base_operators.rb', line 97 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 ⇒ Object
182 183 184 185 186 187 188 |
# File 'lib/dsl/operators/base_operators.rb', line 182 def humanized_tricorder_name exit(0) if @error @tricorder.to_s.split('_').map(&:capitalize).join(' ') end |
#init_error_messages ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/dsl/operators/base_operators.rb', line 25 def 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 = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/dsl/operators/base_operators.rb', line 47 def log(, = {}) return unless [:verbose] || @verbose if [:pager] self.send([:pager], ) else ap() end end |
#merge_results ⇒ Object
90 91 92 93 94 95 |
# File 'lib/dsl/operators/base_operators.rb', line 90 def merge_results exit(0) if @error @results = @results.inject(:merge) self end |
#no_logging ⇒ Object
42 43 44 45 |
# File 'lib/dsl/operators/base_operators.rb', line 42 def no_logging @verbose = false self end |
#params ⇒ Object
66 67 68 69 70 71 |
# File 'lib/dsl/operators/base_operators.rb', line 66 def params { apiKey: @api_key, pageSize: 1000 } end |
#print_all_info ⇒ Object
237 238 239 240 241 242 243 244 245 246 |
# File 'lib/dsl/operators/base_operators.rb', line 237 def print_all_info @results.each do |result| set_uid(result['uid']) fetch_info ap printer(@info) end self end |
#print_all_results ⇒ Object
254 255 256 257 258 |
# File 'lib/dsl/operators/base_operators.rb', line 254 def print_all_results ap printer(@results) self end |
#print_info(*fields) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/dsl/operators/base_operators.rb', line 202 def print_info(*fields) collection, field, number = fields.flatten num = 0 || number @results.each do |res| set_uid(res['uid']) fetch_info begin info = printer(@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 log(@out, {pager: :puts, verbose: true}) 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 ⇒ Object
119 120 121 122 123 |
# File 'lib/dsl/operators/base_operators.rb', line 119 def print_only_once @print_only_once = true self end |
#print_result ⇒ Object
248 249 250 251 252 |
# File 'lib/dsl/operators/base_operators.rb', line 248 def print_result ap printer(@result) self end |
#printer(object) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/dsl/operators/base_operators.rb', line 190 def printer(object) exit(0) if @error if object.empty? "No info found!" else object end end |
#search ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/dsl/operators/base_operators.rb', line 144 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) ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/dsl/operators/base_operators.rb', line 80 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) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/dsl/operators/base_operators.rb', line 73 def set_collection(collection) exit(0) if @error @collection = collection self end |
#set_path(path) ⇒ Object
57 58 59 60 |
# File 'lib/dsl/operators/base_operators.rb', line 57 def set_path(path) @path = path self end |
#set_storedir(path) ⇒ Object
62 63 64 |
# File 'lib/dsl/operators/base_operators.rb', line 62 def set_storedir(path) @storedir = path end |
#set_subject(name) ⇒ Object
9 10 11 12 13 |
# File 'lib/dsl/operators/base_operators.rb', line 9 def set_subject(name) @subject = name self end |
#set_tricorder(name) ⇒ Object
20 21 22 23 |
# File 'lib/dsl/operators/base_operators.rb', line 20 def set_tricorder(name) @tricorder = name.to_sym self end |
#set_uid(uid) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/dsl/operators/base_operators.rb', line 111 def set_uid(uid) exit(0) if @error @uid = uid self end |