Class: VkontakteApi::Client

Inherits:
Object
  • Object
show all
Includes:
Resolver
Defined in:
lib/vkontakte_api/client.rb

Overview

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

Constant Summary collapse

SCOPE =

Access rights and their respective number representation.

{
  notify:        1,
  friends:       2,
  photos:        4,
  audio:         8,
  video:         16,
  offers:        32,
  questions:     64,
  pages:         128,
  status:        1024,
  notes:         2048,
  messages:      4096,
  wall:          8192,
  ads:           32768,
  docs:          131072,
  groups:        262144,
  notifications: 524288,
  stats:         1048576
}

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.



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/vkontakte_api/client.rb', line 48

def initialize(token = nil)
  if token.respond_to?(:token) && token.respond_to?(:params)
    # token is an OAuth2::AccessToken
    @token      = token.token
    @user_id    = token.params['user_id']
    @email      = token.params['email']
    @expires_at = Time.at(token.expires_at) unless token.expires_at.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 VkontakteApi::Namespace instance. Otherwise it creates a VkontakteApi::Method instance and calls it passing the arguments and a block.



91
92
93
94
95
96
97
# File 'lib/vkontakte_api/client.rb', line 91

def method_missing(*args, &block)
  if Namespace.exists?(args.first)
    create_namespace(args.first)
  else
    call_method(args, &block)
  end
end

Instance Attribute Details

#emailString (readonly)

Current user email.

Returns:

  • (String)


37
38
39
# File 'lib/vkontakte_api/client.rb', line 37

def email
  @email
end

#expires_atTime (readonly)

Token expiration time

Returns:

  • (Time)


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

def expires_at
  @expires_at
end

#tokenString (readonly)

An access token needed by authorized requests.

Returns:

  • (String)


29
30
31
# File 'lib/vkontakte_api/client.rb', line 29

def token
  @token
end

#user_idInteger (readonly)

Current user id.

Returns:

  • (Integer)


33
34
35
# File 'lib/vkontakte_api/client.rb', line 33

def user_id
  @user_id
end

Instance Method Details

#authorized?Boolean

Is a VkontakteApi::Client instance authorized.

Returns:

  • (Boolean)


62
63
64
# File 'lib/vkontakte_api/client.rb', line 62

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.



81
82
83
84
85
86
87
# File 'lib/vkontakte_api/client.rb', line 81

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)


67
68
69
# File 'lib/vkontakte_api/client.rb', line 67

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

#scopeArray

Access rights of this token.

Returns:

  • (Array)

    An array of symbols representing the access rights.



73
74
75
76
77
# File 'lib/vkontakte_api/client.rb', line 73

def scope
  SCOPE.reject do |access_scope, mask|
    (settings & mask).zero?
  end.keys
end