106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/canvas-api.rb', line 106
def retrieve_response(request)
request['User-Agent'] = "CanvasAPI Ruby"
begin
response = @http.request(request)
rescue Timeout::Error => e
raise ApiError.new("request timed out")
end
raise ApiError.new("unexpected redirect to #{response.location}") if response.code.to_s.match(/3\d\d/)
json = JSON.parse(response.body) rescue {'error' => 'invalid JSON'}
if !json.is_a?(Array)
raise ApiError.new(json['error']) if json['error']
if !response.code.to_s.match(/2\d\d/)
json['message'] ||= "unexpected error"
raise ApiError.new("#{json['status']} #{json['message']}")
end
else
json = ResultSet.new(self, json)
if response['link']
json.link = response['link']
json.next_endpoint = response['link'].split(/,/).detect{|rel| rel.match(/rel="next"/) }.split(/;/).first.strip[1..-2].sub(/https?:\/\/[^\/]+/, '') rescue nil
end
end
json
end
|