lib/ruby-box/session.rb in ruby-box-1.11.1 vs lib/ruby-box/session.rb in ruby-box-1.12.1

- old
+ new

@@ -15,10 +15,11 @@ if opts[:client_id] @oauth2_client = OAuth2::Client.new(opts[:client_id], opts[:client_secret], OAUTH2_URLS.dup) @access_token = OAuth2::AccessToken.new(@oauth2_client, opts[:access_token]) if opts[:access_token] @refresh_token = opts[:refresh_token] + @as_user = opts[:as_user] else # Support legacy API for historical reasons. @api_key = opts[:api_key] @auth_token = opts[:auth_token] end end @@ -64,10 +65,13 @@ request.add_field('Authorization', "Bearer #{@access_token.token}") else request.add_field('Authorization', build_auth_header) end + + request.add_field('As-User', "#{@as_user}") if @as_user + response = http.request(request) if response.is_a? Net::HTTPNotFound raise RubyBox::ObjectNotFound end @@ -78,11 +82,11 @@ request(uri, request, raw, retries + 1) end sleep(@backoff) # try not to excessively hammer API. - handle_errors( response.code.to_i, response.body, raw ) + handle_errors( response, raw ) end def do_stream(url, opts) params = { :content_length_proc => opts[:content_length_proc], @@ -92,15 +96,19 @@ if @access_token params['Authorization'] = "Bearer #{@access_token.token}" else params['Authorization'] = build_auth_header end + + params['As-User'] = @as_user if @as_user open(url, params) end - def handle_errors( status, body, raw ) + def handle_errors( response, raw ) + status = response.code.to_i + body = response.body begin parsed_body = JSON.parse(body) rescue msg = body.nil? || body.empty? ? "no data returned" : body parsed_body = { "message" => msg } @@ -109,9 +117,12 @@ # status is used to determine whether # we need to refresh the access token. parsed_body["status"] = status case status / 100 + when 3 + # 302 Found. We should return the url + parsed_body["location"] = response["Location"] if status == 302 when 4 raise(RubyBox::ItemNameInUse.new(parsed_body, status, body), parsed_body["message"]) if parsed_body["code"] == "item_name_in_use" raise(RubyBox::AuthError.new(parsed_body, status, body), parsed_body["message"]) if parsed_body["code"] == "unauthorized" || status == 401 raise(RubyBox::RequestError.new(parsed_body, status, body), parsed_body["message"]) when 5