Class: RubyBox::Session
- Inherits:
-
Object
- Object
- RubyBox::Session
- Defined in:
- lib/ruby-box/session.rb
Constant Summary collapse
- OAUTH2_URLS =
{ :site => 'https://www.box.com', :authorize_url => "/api/oauth2/authorize", :token_url => "/api/oauth2/token" }
Instance Method Summary collapse
- #authorize_url(redirect_uri) ⇒ Object
- #build_auth_header ⇒ Object
- #delete(url, raw = false) ⇒ Object
- #do_stream(url, opts) ⇒ Object
- #get(url, raw = false) ⇒ Object
- #get_access_token(code) ⇒ Object
- #handle_errors(response, raw) ⇒ Object
-
#initialize(opts = {}, backoff = 0.1) ⇒ Session
constructor
A new instance of Session.
- #refresh_token(refresh_token) ⇒ Object
- #request(uri, request, raw = false, retries = 0) ⇒ Object
Constructor Details
#initialize(opts = {}, backoff = 0.1) ⇒ Session
Returns a new instance of Session.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/ruby-box/session.rb', line 12 def initialize(opts={}, backoff=0.1) @backoff = backoff # try not to excessively hammer API. 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 |
Instance Method Details
#authorize_url(redirect_uri) ⇒ Object
27 28 29 30 |
# File 'lib/ruby-box/session.rb', line 27 def (redirect_uri) @redirect_uri = redirect_uri @oauth2_client.auth_code.(:redirect_uri => redirect_uri) end |
#build_auth_header ⇒ Object
41 42 43 |
# File 'lib/ruby-box/session.rb', line 41 def build_auth_header "BoxAuth api_key=#{@api_key}&auth_token=#{@auth_token}" end |
#delete(url, raw = false) ⇒ Object
51 52 53 54 55 |
# File 'lib/ruby-box/session.rb', line 51 def delete(url, raw=false) uri = URI.parse(url) request = Net::HTTP::Delete.new( uri.request_uri ) resp = request( uri, request, raw ) end |
#do_stream(url, opts) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ruby-box/session.rb', line 90 def do_stream(url, opts) params = { :content_length_proc => opts[:content_length_proc], :progress_proc => opts[:progress_proc] } 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 |
#get(url, raw = false) ⇒ Object
45 46 47 48 49 |
# File 'lib/ruby-box/session.rb', line 45 def get(url, raw=false) uri = URI.parse(url) request = Net::HTTP::Get.new( uri.request_uri ) resp = request( uri, request, raw ) end |
#get_access_token(code) ⇒ Object
32 33 34 |
# File 'lib/ruby-box/session.rb', line 32 def get_access_token(code) @access_token = @oauth2_client.auth_code.get_token(code) end |
#handle_errors(response, raw) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/ruby-box/session.rb', line 107 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 } end # 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 raise(RubyBox::ServerError.new(parsed_body, status, body), parsed_body["message"]) end raw ? body : parsed_body end |
#refresh_token(refresh_token) ⇒ Object
36 37 38 39 |
# File 'lib/ruby-box/session.rb', line 36 def refresh_token(refresh_token) refresh_access_token_obj = OAuth2::AccessToken.new(@oauth2_client, @access_token.token, {'refresh_token' => refresh_token}) @access_token = refresh_access_token_obj.refresh! end |
#request(uri, request, raw = false, retries = 0) ⇒ Object
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 |
# File 'lib/ruby-box/session.rb', line 57 def request(uri, request, raw=false, retries=0) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.ssl_version = :SSLv3 #http.set_debug_output($stdout) if @access_token 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 # Got unauthorized (401) status, try to refresh the token if response.code.to_i == 401 and @refresh_token and retries == 0 refresh_token(@refresh_token) request(uri, request, raw, retries + 1) end sleep(@backoff) # try not to excessively hammer API. handle_errors( response, raw ) end |