class TelphinApi::Client

A class representing a connection to Telphin. It holds the access token.

Attributes

expires_in[R]

Срок действия маркера доступа (в секундах). @return [Time]

refresh_token[R]

Новый маркер, который можно использовать для обновления маркера с истекшим сроком действия. @return [String]

token[R]

Значение маркера доступа. Это значение используется при выполнении запросов к API. @return [String]

token_type[R]

Тип маркера. Допустимо только значение Bearer. @return [String]

Public Class Methods

new(token = nil) click to toggle source

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. @param [String, OAuth2::AccessToken] token An access token.

# 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

Public Instance Methods

authorized?() click to toggle source

Is a `TelphinApi::Client` instance authorized.

# File lib/telphin_api/client.rb, line 41
def authorized?
  !@token.nil?
end
execute(*args) click to toggle source

Called without arguments it returns the `execute` namespace; called with arguments it calls the top-level `execute` API method.

# 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?() click to toggle source

Did the token already expire.

# File lib/telphin_api/client.rb, line 46
def expired?
  @expires_in && @expires_in < Time.now
end
method_missing(*args, &block) click to toggle source

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.

# 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

Private Instance Methods

settings() click to toggle source
# File lib/telphin_api/client.rb, line 71
def settings
  @settings ||= self.get_user_settings
end