Sha256: 5c149656a8d85e07c9caed5522f4be0d609a32784c64d427ea5b04eb1a52d253

Contents?: true

Size: 1 KB

Versions: 6

Compression:

Stored size: 1 KB

Contents

require 'json'

module OSCRuby

	module NormalizeModule

		class << self

			def normalize(input)

				if input.code.to_i == 404

					input.body

				else

					json_input = JSON.parse(input.body)

					final_hash = []

					json_input['items'].each do |item|
						results_array = iterate_through_rows(item)							
				        final_hash.push(results_array)
					end

					if final_hash.size === 1
						final_hash = final_hash.flatten!
					end

					final_hash.to_json

				end

			end

			def iterate_through_rows(item)

				results_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

	    			results_array.push(obj_hash)

		        end

		        results_array

			end

		end

	end
	
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
osc_ruby-1.4.2 lib/osc_ruby/modules/normalize_module.rb
osc_ruby-1.4.1 lib/osc_ruby/modules/normalize_module.rb
osc_ruby-1.4.0 lib/osc_ruby/modules/normalize_module.rb
osc_ruby-1.3.2 lib/osc_ruby/modules/normalize_module.rb
osc_ruby-1.3.1 lib/osc_ruby/modules/normalize_module.rb
osc_ruby-1.2.2 lib/osc_ruby/modules/normalize_module.rb