Module: OSCRuby::NormalizeModule

Included in:
QueryModule
Defined in:
lib/osc_ruby/modules/normalize_module.rb

Class Method Summary collapse

Class Method Details

.nested_normalize(input, resource) ⇒ Object



49
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
# File 'lib/osc_ruby/modules/normalize_module.rb', line 49

def nested_normalize(input,resource)

	if input.code.to_i == 404

		input.body

	else

		json_input = JSON.parse(input.body)

		final_hash = []

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

			item_array = []

			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

				item_array.push(obj_hash)

			end

			final_hash.push(item_array)

		end

		puts JSON.pretty_generate(final_hash)
		
		final_hash.to_json

	end

end

.normalize(input, resource) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/osc_ruby/modules/normalize_module.rb', line 9

def normalize(input,resource)

	if input.code.to_i == 404

		input.body

	else

		json_input = JSON.parse(input.body)

		final_hash = []

		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

.query_injection(query, json_response) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/osc_ruby/modules/normalize_module.rb', line 89

def query_injection(query,json_response)

	queries = query.split(';')

	count = 0

	json_response.each_with_index do |hash,i|
		if hash == "\n" && queries.count > 1
			json_response[i] = "\nResults for #{queries[count]}:"
			count += 1
		elsif hash == "\n"
			json_response.delete_at(i)
		elsif json_response.last == "\n" 
			json_response.delete_at(json_response.count - 1)
		end
	end

	json_response

end

.remove_new_lines(json_response) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/osc_ruby/modules/normalize_module.rb', line 110

def remove_new_lines(json_response)
	json_response.each_with_index do |hash,i|
		if hash == "\n"
			json_response.delete_at(i)
		end
	end

	json_response
end