Class: PerfectReach::ApiRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/perfect_reach/api_request.rb

Instance Method Summary collapse

Constructor Details

#initialize(method_name, params = {}, request_type = nil, auth_token = PerfectReach.config.api_key, host = PerfectReach.config.host, port = PerfectReach.config.port) ⇒ ApiRequest

Returns a new instance of ApiRequest.



10
11
12
13
14
15
16
17
# File 'lib/perfect_reach/api_request.rb', line 10

def initialize(method_name, params = {}, request_type = nil, auth_token = PerfectReach.config.api_key, host = PerfectReach.config.host, port = PerfectReach.config.port)
	@method_name = method_name.to_s
	@params = (params || {}).merge(:format => 'json')
	@request_type = (request_type || guess_request_type).camelize
	@auth_token = auth_token
	@host = host
	@port = port
end

Instance Method Details

#responseObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/perfect_reach/api_request.rb', line 19

def response
	@response ||= begin
		http = Net::HTTP.new(@host, @port)
		http.use_ssl = PerfectReach.config.use_https
		res = http.request(request)

		case res
			when Net::HTTPSuccess
				JSON.parse(res.body || '{}')
			when Net::HTTPNotModified
				{"status" => "not_modified"}
			else
				raise ApiError.new(res.code, JSON.parse(res.body.presence || '{}'), request, request_path, @params)
		end
	end
end