Class: BaseOperators::BaseDSLOperators

Inherits:
Object
  • Object
show all
Defined in:
lib/dsl/operators/base_operators.rb

Direct Known Subclasses

TricorderDSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pageObject (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_infoObject



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_nameObject



182
183
184
185
186
187
188
# File 'lib/dsl/operators/base_operators.rb', line 182

def humanized_tricorder_name
  exit(0) if @error

  init_error_messages

  @tricorder.to_s.split('_').map(&:capitalize).join(' ')
end

#init_error_messagesObject



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 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 = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/dsl/operators/base_operators.rb', line 47

def log(message, options = {})
  return unless options[:verbose] || @verbose

  if options[:pager]
    self.send(options[:pager], message)
  else
    ap(message)
  end
end

#merge_resultsObject



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_loggingObject



42
43
44
45
# File 'lib/dsl/operators/base_operators.rb', line 42

def no_logging
  @verbose = false
  self
end

#paramsObject



66
67
68
69
70
71
# File 'lib/dsl/operators/base_operators.rb', line 66

def params
  {
    apiKey: @api_key,
    pageSize: 1000
  }
end


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


254
255
256
257
258
# File 'lib/dsl/operators/base_operators.rb', line 254

def print_all_results
  ap printer(@results)

  self
end


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


119
120
121
122
123
# File 'lib/dsl/operators/base_operators.rb', line 119

def print_only_once
  @print_only_once = true

  self
end


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

  init_error_messages

  if object.empty?
    "No info found!"
  else
    object
  end
end

#searchObject



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