Class: TelphinApi::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Resolver

included, #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.

Parameters:

  • token (String, OAuth2::AccessToken) (defaults to: nil)

    An access 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_inTime (readonly)

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

Returns:

  • (Time)


20
21
22
# File 'lib/telphin_api/client.rb', line 20

def expires_in
  @expires_in
end

#refresh_tokenString (readonly)

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

Returns:

  • (String)


16
17
18
# File 'lib/telphin_api/client.rb', line 16

def refresh_token
  @refresh_token
end

#tokenString (readonly)

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

Returns:

  • (String)


8
9
10
# File 'lib/telphin_api/client.rb', line 8

def token
  @token
end

#token_typeString (readonly)

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

Returns:

  • (String)


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.

Returns:

  • (Boolean)


41
42
43
# File 'lib/telphin_api/client.rb', line 41

def authorized?
  !@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.

Returns:

  • (Boolean)


46
47
48
# File 'lib/telphin_api/client.rb', line 46

def expired?
  @expires_in && @expires_in < Time.now
end