-
Notifications
You must be signed in to change notification settings - Fork 11
/
connect.rb
81 lines (73 loc) · 2.21 KB
/
connect.rb
1
2
3
4
5
6
7
8
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
48
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
require 'json'
require 'ostruct'
module ProsperWorks
module ApiOperations
module Connect
def get_uri(api_name, id = nil)
url = client.base_url + "#{api_name}"
url = url + "/#{id}" unless id.nil?
URI.parse(url)
end
def send_request(http_type, uri, entity = nil)
response = nil
headers = client.headers
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.start do
case http_type
when "get"
request = Net::HTTP::Get.new(uri.request_uri, headers)
when "post"
request = Net::HTTP::Post.new(uri.request_uri, headers)
request.body = entity.to_json
when "put"
request = Net::HTTP::Put.new(uri.request_uri, headers)
request.body = entity.to_json
when "delete"
request = Net::HTTP::Delete.new(uri.request_uri, headers)
end
response = http.request(request)
end
response
end
def handle_response(entity, response)
case response.code.to_i
when 200
json_object = JSON.parse(response.body)
return json_object if json_object.is_a?(Array)
json_object.each_pair do |key, value|
if entity.respond_to?(key.to_sym)
entity.send("#{key}=", value)
end
end
entity
when Errors::BadRequest.status_code
Errors::BadRequest.new
when Errors::Unauthorized.status_code
Errors::Unauthorized.new
when Errors::Forbidden.status_code
Errors::Forbidden.new
when Errors::NotFound.status_code
Errors::NotFound.new
when Errors::Unprocessable.status_code
Errors::Unprocessable.new
when Errors::RateLimit.status_code
Errors::RateLimit.new
else
Errors::ServerError.new
end
end
def handle_multiple_response(response)
result = handle_response(nil, response)
if result.is_a?(ProsperWorks::Errors::Base)
# pass the error along
result
else
result.map do |res|
new(res)
end
end
end
end
end
end