This repository has been archived by the owner on Apr 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
http.rb
102 lines (88 loc) · 3 KB
/
http.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
module ClioClient
class Unauthorized < StandardError; end
class ResourceNotFound < StandardError; end
class BadRequest < StandardError; end
class UnknownResponse < StandardError; end
class Forbidden < StandardError; end
module Http
def base_uri(path, params = {})
uri = URI.parse(self.base_scope_url)
uri.path = path
uri.query = URI.encode_www_form(params) if params.any?
uri
end
def get(path, params ={}, parse=true)
uri = base_uri("#{api_prefix}/#{path}", params)
req = Net::HTTP::Get.new(uri.request_uri)
make_api_request(req, uri, parse)
end
def put(path, body = "", parse=true)
uri = base_uri("#{api_prefix}/#{path}")
req = Net::HTTP::Put.new(uri.request_uri)
req.body = body
req.add_field("Content-Type", "application/json")
make_api_request(req, uri, parse)
end
def multipart_post(path, params, parse = true)
uri = base_uri("#{api_prefix}/#{path}")
req = Net::HTTP::Post::Multipart.new(uri.request_uri, params)
make_api_request(req, uri, parse)
end
def post(path, body ="", parse = true)
uri = base_uri("#{api_prefix}/#{path}")
req = Net::HTTP::Post.new(uri.request_uri)
req.body = body
req.add_field("Content-Type", "application/json")
make_api_request(req, uri, parse)
end
def delete(path, params = {}, parse=true)
uri = base_uri("#{api_prefix}/#{path}", params)
req = Net::HTTP::Delete.new(uri.request_uri)
make_api_request(req, uri, parse)
end
def make_api_request(req, uri, parse = true)
raise ClioClient::Unauthorized if self.access_token.nil? || self.access_token.empty?
req.add_field("Authorization", "Bearer #{self.access_token}")
make_request(req, uri, parse)
end
def make_request(req, uri, parse = true)
req.add_field("Accept", "text/json")
n = Net::HTTP.new(uri.host, uri.port)
n.use_ssl = uri.scheme == 'https'
res = n.start do |http|
http.request(req)
end
parse_response(res, parse)
end
def parse_response(res, parse)
case res
when Net::HTTPNotFound
raise ClioClient::ResourceNotFound.new(parse ? parse_body(res.body)["message"] : "")
when Net::HTTPSuccess
parse ? parse_body(res.body) : res.body
when Net::HTTPUnauthorized
begin
message = parse_body(res.body)["message"]
rescue JSON::ParserError
message = res.body
end
raise ClioClient::Unauthorized.new(message)
when Net::HTTPBadRequest
raise ClioClient::BadRequest.new(parse_body(res.body)["message"])
when Net::HTTPSeeOther
res["Location"]
when Net::HTTPForbidden
raise ClioClient::Forbidden.new(res.body)
else
raise UnknownResponse.new(res.body)
end
end
private
def parse_body(body)
if body !~ /^\s*$/ && !body.nil?
JSON.parse(body)
end
end
def api_prefix; "/api/v2"; end
end
end