-
Notifications
You must be signed in to change notification settings - Fork 401
Expand file tree
/
Copy pathrequest.rb
More file actions
85 lines (70 loc) · 2.61 KB
/
request.rb
File metadata and controls
85 lines (70 loc) · 2.61 KB
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
module LinkedIn
module Helpers
module Request
DEFAULT_HEADERS = {
'x-li-format' => 'json'
}
API_PATH = '/v1'
protected
def get(path, options={})
response = access_token.get("#{API_PATH}#{path}", DEFAULT_HEADERS.merge(options))
raise_errors(response)
response.body
end
def post(path, body='', options={})
response = access_token.post("#{API_PATH}#{path}", body, DEFAULT_HEADERS.merge(options))
raise_errors(response)
response
end
def put(path, body, options={})
response = access_token.put("#{API_PATH}#{path}", body, DEFAULT_HEADERS.merge(options))
raise_errors(response)
response
end
def delete(path, options={})
response = access_token.delete("#{API_PATH}#{path}", DEFAULT_HEADERS.merge(options))
raise_errors(response)
response
end
private
def raise_errors(response)
# Even if the json answer contains the HTTP status code, LinkedIn also sets this code
# in the HTTP answer (thankfully).
case response.code.to_i
when 401
data = Mash.from_response(response)
raise LinkedIn::Errors::UnauthorizedError.new(data), "(#{data.status}): #{data.message}"
when 400
data = Mash.from_response(response)
raise LinkedIn::Errors::GeneralError.new(data), "(#{data.status}): #{data.message}"
when 403
data = Mash.from_response(response)
raise LinkedIn::Errors::AccessDeniedError.new(data), "(#{data.status}): #{data.message}"
when 404
raise LinkedIn::Errors::NotFoundError, "(#{response.code}): #{response.message}"
when 500
raise LinkedIn::Errors::InformLinkedInError, "LinkedIn had an internal error. Please let them know in the forum. (#{response.code}): #{response.message}"
when 502..503
raise LinkedIn::Errors::UnavailableError, "(#{response.code}): #{response.message}"
end
end
# Stolen from Rack::Util.build_query
def to_query(params)
params.map { |k, v|
if v.class == Array
to_query(v.map { |x| [k, x] })
else
v.nil? ? escape(k) : "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
end
}.join("&")
end
def to_uri(path, options)
uri = URI.parse(path)
if options && options != {}
uri.query = to_query(options)
end
uri.to_s
end
end
end
end