Module: BoxView::Http
- Included in:
- Session
- Defined in:
- lib/box_view/http.rb
Defined Under Namespace
Classes: BadRequestError, Error, RequestThrottledError, RetryNeededError
Instance Method Summary
collapse
-
#base_uri(path, params = {}) ⇒ Object
-
#check_for_error(res) ⇒ Object
-
#convert_params(params) ⇒ Object
-
#delete(path, parse = true) ⇒ Object
-
#get(path, params = {}, parse = true) ⇒ Object
-
#make_api_request(req, uri, parse = true) ⇒ Object
-
#make_request(req, uri, parse = true) ⇒ Object
-
#parse_response(res) ⇒ Object
-
#post(path, body = "", parse = true) ⇒ Object
-
#put(path, body = "", parse = true) ⇒ Object
Instance Method Details
#base_uri(path, params = {}) ⇒ Object
19
20
21
22
23
24
|
# File 'lib/box_view/http.rb', line 19
def base_uri(path, params = {})
uri = URI.parse("https://view-api.box.com")
uri.path = path
uri.query = URI.encode_www_form(convert_params(params)) if params.any?
uri
end
|
#check_for_error(res) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/box_view/http.rb', line 72
def check_for_error(res)
case res.code.to_s
when '202' if res['Retry-After']
raise BoxView::Http::RetryNeededError.new('Retry Needed', res['Retry-After'])
end
when '400' msg = 'Bad Request'
if err_dets = error_details(res)
msg += " (#{err_dets})"
end
raise BoxView::Http::BadRequestError.new(msg)
when '429' raise BoxView::Http::RequestThrottledError.new('Request Throttled', res['Retry-After'])
end
end
|
#convert_params(params) ⇒ Object
96
97
98
99
100
101
102
103
|
# File 'lib/box_view/http.rb', line 96
def convert_params(params)
params.each_pair do |key, val|
if [Date, Time, DateTime].include?(val.class)
params[key] = val.iso8601
end
end
params
end
|
#delete(path, parse = true) ⇒ Object
49
50
51
52
53
|
# File 'lib/box_view/http.rb', line 49
def delete(path, parse=true)
uri = base_uri("#{api_prefix}/#{path}")
req = Net::HTTP::Delete.new(uri.request_uri)
make_api_request(req, uri, parse)
end
|
#get(path, params = {}, parse = true) ⇒ Object
26
27
28
29
30
|
# File 'lib/box_view/http.rb', line 26
def get(path, params={}, parse=true)
uri = base_uri("#{api_prefix}/#{path}", params)
req = Net::HTTP::Get.new(uri.to_s)
make_api_request(req, uri, parse)
end
|
#make_api_request(req, uri, parse = true) ⇒ Object
55
56
57
58
59
|
# File 'lib/box_view/http.rb', line 55
def make_api_request(req, uri, parse=true)
return if self.token.nil? || self.token.empty?
req.add_field("Authorization", "Token #{self.token}")
make_request(req, uri, parse)
end
|
#make_request(req, uri, parse = true) ⇒ Object
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/box_view/http.rb', line 61
def make_request(req, uri, parse=true)
req.add_field("Accept", "text/json")
n = Net::HTTP.new(uri.host, uri.port)
n.use_ssl = true
res = n.start do |http|
http.request(req)
end
check_for_error(res)
parse ? parse_response(res) : res.body
end
|
#parse_response(res) ⇒ Object
90
91
92
93
94
|
# File 'lib/box_view/http.rb', line 90
def parse_response(res)
JSON.parse(res.body)
rescue JSON::ParserError
nil
end
|
#post(path, body = "", parse = true) ⇒ Object
33
34
35
36
37
38
39
|
# File 'lib/box_view/http.rb', line 33
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
|
#put(path, body = "", parse = true) ⇒ Object
41
42
43
44
45
46
47
|
# File 'lib/box_view/http.rb', line 41
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
|