Class: TelphinApi::Client
- Inherits:
-
Object
- Object
- TelphinApi::Client
- Includes:
- Resolver
- Defined in:
- lib/telphin_api/client.rb
Overview
A class representing a connection to Telphin. It holds the access token.
Instance Attribute Summary collapse
-
#expires_in ⇒ Time
readonly
Срок действия маркера доступа (в секундах).
-
#refresh_token ⇒ String
readonly
Новый маркер, который можно использовать для обновления маркера с истекшим сроком действия.
-
#token ⇒ String
readonly
Значение маркера доступа.
-
#token_type ⇒ String
readonly
Тип маркера.
Instance Method Summary collapse
-
#authorized? ⇒ Boolean
Is a
TelphinApi::Client
instance authorized. -
#execute(*args) ⇒ Object
Called without arguments it returns the
execute
namespace; called with arguments it calls the top-levelexecute
API method. -
#expired? ⇒ Boolean
Did the token already expire.
-
#initialize(token = nil) ⇒ Client
constructor
A new API client.
-
#method_missing(*args, &block) ⇒ Object
If the called method is a namespace, it creates and returns a new
TelphinApi::Namespace
instance.
Methods included from Resolver
Constructor Details
#initialize(token = nil) ⇒ Client
A new API client.
If given an OAuth2::AccessToken
instance, it extracts and keeps
the token string, the user id and the expiration time;
otherwise it just stores the given token.
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/telphin_api/client.rb', line 27 def initialize(token = nil) if token.respond_to?(:token) && token.respond_to?(:params) # token is an OAuth2::AccessToken @token = token.token @token_type = token.params['token_type'] @refresh_token = token.params['refresh_token'] @expires_in = Time.now + token.expires_in unless token.expires_in.nil? else # token is a String or nil @token = token end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args, &block) ⇒ Object
If the called method is a namespace, it creates and returns a new TelphinApi::Namespace
instance.
Otherwise it creates a TelphinApi::Method
instance and calls it passing the arguments and a block.
62 63 64 65 66 67 68 |
# File 'lib/telphin_api/client.rb', line 62 def method_missing(*args, &block) if Namespace.exists?(args.first) create_namespace(args.first) else call_method(args, &block) end end |
Instance Attribute Details
#expires_in ⇒ Time (readonly)
Срок действия маркера доступа (в секундах).
20 21 22 |
# File 'lib/telphin_api/client.rb', line 20 def expires_in @expires_in end |
#refresh_token ⇒ String (readonly)
Новый маркер, который можно использовать для обновления маркера с истекшим сроком действия.
16 17 18 |
# File 'lib/telphin_api/client.rb', line 16 def refresh_token @refresh_token end |
#token ⇒ String (readonly)
Значение маркера доступа. Это значение используется при выполнении запросов к API.
8 9 10 |
# File 'lib/telphin_api/client.rb', line 8 def token @token end |
#token_type ⇒ String (readonly)
Тип маркера. Допустимо только значение Bearer.
12 13 14 |
# File 'lib/telphin_api/client.rb', line 12 def token_type @token_type end |
Instance Method Details
#authorized? ⇒ Boolean
Is a TelphinApi::Client
instance authorized.
41 42 43 |
# File 'lib/telphin_api/client.rb', line 41 def !@token.nil? end |
#execute(*args) ⇒ Object
Called without arguments it returns the execute
namespace;
called with arguments it calls the top-level execute
API method.
52 53 54 55 56 57 58 |
# File 'lib/telphin_api/client.rb', line 52 def execute(*args) if args.empty? create_namespace(:execute) else call_method([:execute, *args]) end end |
#expired? ⇒ Boolean
Did the token already expire.
46 47 48 |
# File 'lib/telphin_api/client.rb', line 46 def expired? @expires_in && @expires_in < Time.now end |