Class: SmartRecruiters::Authorization
- Inherits:
-
Object
- Object
- SmartRecruiters::Authorization
- Defined in:
- lib/smart_recruiters/authorization.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
The Configuration object holding settings to be used in the API client.
-
#oauth_client ⇒ Object
Returns the value of attribute oauth_client.
Class Method Summary collapse
Instance Method Summary collapse
- #build_authorize_url ⇒ Object
- #get_token(code) ⇒ Object
-
#initialize(config = Configuration.default) ⇒ Authorization
constructor
Initializes the Authorization.
- #load_token(token) ⇒ Object
- #refresh_token ⇒ Object
Constructor Details
#initialize(config = Configuration.default) ⇒ Authorization
Initializes the Authorization
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/smart_recruiters/authorization.rb', line 12 def initialize(config = Configuration.default) @config = config @oauth_client = OAuth2::Client.new( config.client_id, config.client_secret, authorize_url: 'https://www.smartrecruiters.com/identity/oauth/allow', token_url: 'https://api.smartrecruiters.com/identity/oauth/token', redirect_uri: config.redirect_uri, params: { client_id: config.client_id, client_secret: config.client_secret } ) end |
Instance Attribute Details
#config ⇒ Object
The Configuration object holding settings to be used in the API client.
6 7 8 |
# File 'lib/smart_recruiters/authorization.rb', line 6 def config @config end |
#oauth_client ⇒ Object
Returns the value of attribute oauth_client.
8 9 10 |
# File 'lib/smart_recruiters/authorization.rb', line 8 def oauth_client @oauth_client end |
Class Method Details
.default ⇒ Object
24 25 26 |
# File 'lib/smart_recruiters/authorization.rb', line 24 def self.default @@default ||= Authorization.new end |
Instance Method Details
#build_authorize_url ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/smart_recruiters/authorization.rb', line 28 def = oauth_client.auth_code. unless @config.scopes.length.zero? += "&scope=" + @config.scopes.join("%20") end end |
#get_token(code) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/smart_recruiters/authorization.rb', line 38 def get_token(code) oauth_client.auth_code.get_token( code, { client_id: config.client_id, client_secret: config.client_secret } ) end |
#load_token(token) ⇒ Object
65 66 67 68 69 |
# File 'lib/smart_recruiters/authorization.rb', line 65 def load_token(token) SmartRecruiters::Configuration.default.access_token = token.token SmartRecruiters::Configuration.default.token_expires_at = token.expires_at SmartRecruiters::Configuration.default.refresh_token = token.refresh_token end |
#refresh_token ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/smart_recruiters/authorization.rb', line 45 def refresh_token token = OAuth2::AccessToken.from_hash( oauth_client, { "token_type": "Bearer", expires_at: config.token_expires_at, access_token: config.access_token, refresh_token: config.refresh_token } ) if token.expired? token.refresh!({ client_id: config.client_id, client_secret: config.client_secret }) load_token(token) end token end |