Class: MicrosoftLive::Simple
- Inherits:
-
Object
- Object
- MicrosoftLive::Simple
- Includes:
- HTTParty
- Defined in:
- lib/microsoft-live/simple.rb
Instance Attribute Summary collapse
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
Returns the value of attribute client_secret.
-
#expires_at ⇒ Object
Returns the value of attribute expires_at.
-
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #contacts ⇒ Object
- #ensure_valid_token! ⇒ Object
- #get_collection(url:, limit: 10, offset: 0) ⇒ Object
- #get_object(url:) ⇒ Object
-
#initialize(token:, refresh_token:, expires_at:) ⇒ Simple
constructor
A new instance of Simple.
- #renew_token ⇒ Object
- #user(user_id) ⇒ Object
Constructor Details
#initialize(token:, refresh_token:, expires_at:) ⇒ Simple
Returns a new instance of Simple.
14 15 16 17 18 19 20 21 |
# File 'lib/microsoft-live/simple.rb', line 14 def initialize(token:, refresh_token:, expires_at:) self.client_id = MicrosoftLive.config.client_id self.client_secret = MicrosoftLive.config.client_secret self.redirect_uri = MicrosoftLive.config.redirect_uri self.token = token self.refresh_token = refresh_token self.expires_at = expires_at end |
Instance Attribute Details
#client_id ⇒ Object
Returns the value of attribute client_id.
11 12 13 |
# File 'lib/microsoft-live/simple.rb', line 11 def client_id @client_id end |
#client_secret ⇒ Object
Returns the value of attribute client_secret.
11 12 13 |
# File 'lib/microsoft-live/simple.rb', line 11 def client_secret @client_secret end |
#expires_at ⇒ Object
Returns the value of attribute expires_at.
11 12 13 |
# File 'lib/microsoft-live/simple.rb', line 11 def expires_at @expires_at end |
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
11 12 13 |
# File 'lib/microsoft-live/simple.rb', line 11 def redirect_uri @redirect_uri end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
11 12 13 |
# File 'lib/microsoft-live/simple.rb', line 11 def refresh_token @refresh_token end |
#token ⇒ Object
Returns the value of attribute token.
11 12 13 |
# File 'lib/microsoft-live/simple.rb', line 11 def token @token end |
Instance Method Details
#contacts ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/microsoft-live/simple.rb', line 23 def contacts ::MicrosoftLive::Collector.new( simple: self, path: '/me/contacts', offset: 0, limit: 50, item_class: ::MicrosoftLive::Contact ) end |
#ensure_valid_token! ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/microsoft-live/simple.rb', line 70 def ensure_valid_token! if Time.now.to_i > expires_at new_auth = renew_token self.token = new_auth.token self.refresh_token = new_auth.refresh_token self.expires_at = new_auth.expires_at end end |
#get_collection(url:, limit: 10, offset: 0) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/microsoft-live/simple.rb', line 54 def get_collection(url:, limit: 10, offset: 0) ensure_valid_token! self.class.base_uri 'https://apis.live.net/v5.0' res = self.class.get('/%s?limit=%d&offset=%d' % [ url, limit, offset ], headers: { 'Authorization' => "Bearer #{token}" } ) case res.code when 200 JSON.parse(res.body, symbolize_names: true) else raise ApiError.new res.body end end |
#get_object(url:) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/microsoft-live/simple.rb', line 38 def get_object(url:) ensure_valid_token! self.class.base_uri 'https://apis.live.net/v5.0' res = self.class.get('/%s' % user_id, headers: { 'Authorization' => "Bearer #{token}" } ) case res.code when 200 JSON.parse(res.body, symbolize_names: true) else raise ApiError.new res.body end end |
#renew_token ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/microsoft-live/simple.rb', line 79 def renew_token res = self.class.post('/oauth20_token.srf', body: { client_id: client_id, redirect_uri: redirect_uri, client_secret: client_secret, refresh_token: refresh_token, grant_type: :refresh_token } ) case res.code when 200 data = JSON.parse(res.body, symbolize_names: true) OpenStruct.new( token: data[:access_token], refresh_token: data[:refresh_token], expires_at: Time.now.to_i + data[:expires_in] ) else raise RenewTokenError.new res.body end end |
#user(user_id) ⇒ Object
33 34 35 36 |
# File 'lib/microsoft-live/simple.rb', line 33 def user(user_id) data = get_object(url: user_id) ::MicrosoftLive::User.new(data: data, simple: self) end |