Module: OSCRuby::QueryModule

Included in:
ServiceProduct
Defined in:
lib/osc_ruby/query_module.rb

Class Method Summary collapse

Class Method Details

.check_obj_for_errors(obj_to_check) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/osc_ruby/query_module.rb', line 56

def self.check_obj_for_errors(obj_to_check)

  json_obj = JSON.parse(obj_to_check.body)

  if json_obj['items'][0]['rows'].count == 0

    raise ArgumentError, 'There were no objects matching your query; please try again.'

  end

end

.create(rn_client, resource, json_content) ⇒ Object



20
21
22
23
24
# File 'lib/osc_ruby/query_module.rb', line 20

def self.create(rn_client,resource,json_content)

  OSCRuby::Connect.post_or_patch(rn_client,resource,json_content)

end

.find(rn_client, resource) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/osc_ruby/query_module.rb', line 10

def self.find(rn_client,resource)

  obj_to_find = OSCRuby::Connect.get(rn_client,resource)

  check_obj_for_errors(obj_to_find)

  normalize(obj_to_find)

end

.normalize(input) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/osc_ruby/query_module.rb', line 26

def self.normalize(input)

  if input.code.to_i == 404

    input.body

  else

    json_input = JSON.parse(input.body)

    final_hash = []

    json_input['items'][0]['rows'].each do |row|

      obj_hash = {}
      
      json_input['items'][0]['columnNames'].each_with_index do |column,i|
        obj_hash[column] = if !row[i].nil? && row[i].is_i? == true then row[i].to_i else row[i] end
      end

      final_hash.push(obj_hash)

    end

    final_hash.to_json

  end

end