Class: UnipassApi::Client
- Inherits:
-
Object
- Object
- UnipassApi::Client
- Defined in:
- lib/unipass_api/client.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#expires_at ⇒ Object
Returns the value of attribute expires_at.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
Instance Method Summary collapse
- #client ⇒ Object
- #expired? ⇒ Boolean
- #get(url, options = {}) ⇒ Object
-
#initialize(options = {}, &block) ⇒ Client
constructor
A new instance of Client.
- #post(url, options = {}) ⇒ Object
- #refresh! ⇒ Object
- #request(verb, url, options = {}) ⇒ Object
Constructor Details
#initialize(options = {}, &block) ⇒ Client
Returns a new instance of Client.
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/unipass_api/client.rb', line 7 def initialize( = {}, &block) @client_id = [:client_id] || UnipassApi.[:client_id] @client_secret = [:client_secret] || UnipassApi.[:client_secret] @site = [:site] || UnipassApi.[:site] @api_site = [:api_site] || UnipassApi.[:api_site] @authorize_url = [:authorize_url] || UnipassApi.[:authorize_url] @token_url = [:token_url] || UnipassApi.[:token_url] self.access_token = [:access_token] self.refresh_token = [:refresh_token] self.expires_at = [:expires_at] end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
5 6 7 |
# File 'lib/unipass_api/client.rb', line 5 def access_token @access_token end |
#expires_at ⇒ Object
Returns the value of attribute expires_at.
5 6 7 |
# File 'lib/unipass_api/client.rb', line 5 def expires_at @expires_at end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
5 6 7 |
# File 'lib/unipass_api/client.rb', line 5 def refresh_token @refresh_token end |
Instance Method Details
#client ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/unipass_api/client.rb', line 50 def client @client ||= ::OAuth2::Client.new(@client_id, @client_secret, { :site => @api_site, :authorize_url => @authorize_url, :token_url => @token_url }) end |
#expired? ⇒ Boolean
20 21 22 |
# File 'lib/unipass_api/client.rb', line 20 def expired? token.expired? end |
#get(url, options = {}) ⇒ Object
42 43 44 |
# File 'lib/unipass_api/client.rb', line 42 def get(url, = {}) request(:get, url, ) end |
#post(url, options = {}) ⇒ Object
46 47 48 |
# File 'lib/unipass_api/client.rb', line 46 def post(url, = {}) request(:post, url, ) end |
#refresh! ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/unipass_api/client.rb', line 24 def refresh! new_token = token.refresh! self.access_token = new_token.token self.refresh_token = new_token.refresh_token self.expires_at = new_token.expires_at new_token end |
#request(verb, url, options = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/unipass_api/client.rb', line 32 def request(verb, url, = {}) token.request(verb, url, ).parsed rescue OAuth2::Error => error case error.response.status when 404 then raise ResourceNotFound.new(error.response) when 422 then raise ResourceInvalid.new(error.response) else raise end end |