Sha256: f706f26089ef0fd363ffb3a5f94685febe1393b0becd9d36b6c2001aeee673aa

Contents?: true

Size: 1.92 KB

Versions: 3

Compression:

Stored size: 1.92 KB

Contents

require 'erb'

module OEHClient

	module Helper

		module Request

			ONE_PROTOCOL 			= "https://"
			THUNDERHEAD_DOMAIN 		= ".thunderhead.com"
			ONE_URI_PART 			= "/one/oauth1"

			API_URI_PART 			= "/rt/api"
			API_VERSION 			= "/2.0"

			# request_url builds the target request URL with the passed parameters, URL encoding the parameters
			#  as necessary to create a valid request
			def self.format_url(url, params)

				# for each of the parameters, build a single query parameter string
				parameter_part = ""
				params.each do |key, value|
					# if there is more than one argument, add the apppropriate separator (&) between 
					#  query parameters
					parameter_part << "&" if (parameter_part.length > 0)
					# URL Encode the value of the property
					parameter_part << "#{key.to_s}=#{ERB::Util.url_encode(value)}"

				end
				# return the fully-qualified URL with parameters (if passsed)
				(parameter_part.length > 0 ? "#{url}?#{parameter_part}" : "#{url}")

			end

			# default_JSON_header is the default header that is passed to any OEH Request if not provided explicitly by the 
			#  calling methods
			def self.default_JSON_header()
				{'Accept' => 'application/json' , 'Content-Type' =>'application/json', 'X-Requested-With' => 'XMLHttpRequest' }
			end


		end  # module Request

		module Response

			def self.handle(api_response)
				# raise a generic HTTPRequestException if the the status code is not 100 (Continue) or 200 (OK)
				raise OEHClient::Exception::HTTPRequestException.new(api_response.message, api_response.code) if (api_response.code != "200" && api_response.code != "100")
				
				begin
					# Return the Body of the response for valid requests
					ActiveSupport::JSON.decode(api_response.body)
				rescue ActiveSupport::JSON.parse_error
					# otherwise raise a standard processing exception
					raise OEHClient::Exception::HTTPRequestException(api.body, 500)
				end


			end

		end # module Response

	end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
oeh-client-0.1.3 lib/oehclient/helper.rb
oeh-client-0.1.2 lib/oehclient/helper.rb
oeh-client-0.1.1 lib/oehclient/helper.rb