Sha256: aee3b6264517c4b4cc0a5633c87e16ee56b88cbe78c7dedf7274b9e5f465f04e

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 KB

Contents

require 'osc_ruby/connect'
require_relative '../ext/string'

require 'json'

module OSCRuby

	module QueryModule
	
		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

		def self.create(rn_client,resource,json_content)

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

			obj_to_find.body

		end

		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

		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
		
	end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
osc_ruby-0.1.4 lib/osc_ruby/query_module.rb
osc_ruby-0.1.3 lib/osc_ruby/query_module.rb
osc_ruby-0.1.2 lib/osc_ruby/query_module.rb