Module: OSCRuby::QueryModule

Extended by:
ValidationsModule
Included in:
QueryResults, ServiceClass
Defined in:
lib/osc_ruby/query_module.rb

Class Method Summary collapse

Methods included from ValidationsModule

attr_hash_exists_and_is_type_of, check_attributes, check_attributes_request, check_client, check_for_id, check_for_names, check_for_parents, check_interfaces, check_object_for_id, check_query, extract_attributes

Class Method Details

.check_obj_for_errors(obj_to_check) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/osc_ruby/query_module.rb', line 94

def check_obj_for_errors(obj_to_check)

	json_obj = JSON.parse(obj_to_check.body)

	if !json_obj.nil? && 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



32
33
34
35
36
# File 'lib/osc_ruby/query_module.rb', line 32

def create(rn_client,resource,json_content)

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

end

.destroy(rn_client, resource) ⇒ Object



44
45
46
47
48
# File 'lib/osc_ruby/query_module.rb', line 44

def destroy(rn_client,resource)

	OSCRuby::Connect.delete(rn_client,resource)
	
end

.find(rn_client, resource) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/osc_ruby/query_module.rb', line 15

def find(rn_client,resource)

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

	if obj_to_find.code.to_i == 200 || obj_to_find.code.to_i == 201

		check_obj_for_errors(obj_to_find)

		normalize(obj_to_find,resource)
	else

		obj_to_find.body

	end

end

.normalize(input, resource) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/osc_ruby/query_module.rb', line 50

def normalize(input,resource)

	if input.code.to_i == 404

		input.body

	else

		json_input = JSON.parse(input.body)

		final_hash = []

		query_capture = URI.unescape(resource).split('=')[1]

		queries = query_capture.split(';')

		json_input['items'].each do |item|

			item['rows'].each_with_index do |row,row_i|

				obj_hash = {}
				
				item['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)

				if json_input['items'].count > 1 && (item['rows'].count-1 == row_i)

					final_hash.push("\n")

				end

			end

		end

		final_hash.to_json

	end

end

.update(rn_client, resource, json_content) ⇒ Object



38
39
40
41
42
# File 'lib/osc_ruby/query_module.rb', line 38

def update(rn_client,resource,json_content)

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

end