OAuth2::AccessToken#refresh! returns new object, not itself #116

Open
akostrikov opened this Issue Aug 17, 2012 · 1 comment

Comments

Projects
None yet
2 participants

It is a common practice that when we use bang! methods methods are performed to object itself.

On line https://github.com/intridea/oauth2/blob/master/lib/oauth2/access_token.rb#L99 #refresh! method returns new token, so we can not use it in such way:

def try_refresh
  # some work
  token.refresh!
  # more work
  token
end

Instead we should use temporary variable, like this:

def try_refresh
  # some work
  new_token = token.refresh!
  # more work
  new_token
end

moll commented Aug 28, 2012

Here's my "patch" for using in your own subclass of AccessToken:

def refresh!(opts = {})
  new_token = super
  (new_token.instance_variables - %w[@refresh_token]).each {|name|
    self.instance_variable_set(name, new_token.instance_variable_get(name))
  }
  self
end

Update: Skip copying refresh_token as that's not carried over with the new token.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment