class VkontakteApi::Client

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

Constants

SCOPE

Access rights and their respective number representation.

Attributes

email[R]

Current user email. @return [String]

expires_at[R]

Token expiration time @return [Time]

token[R]

An access token needed by authorized requests. @return [String]

user_id[R]

Current user id. @return [Integer]

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/vkontakte_api/client.rb, line 51
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

Public Instance Methods

authorized?() click to toggle source

Is a `VkontakteApi::Client` instance authorized.

# File lib/vkontakte_api/client.rb, line 65
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/vkontakte_api/client.rb, line 84
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/vkontakte_api/client.rb, line 70
def expired?
  @expires_at && @expires_at < Time.now
end
method_missing(*args, &block) click to toggle source

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.

# File lib/vkontakte_api/client.rb, line 94
def method_missing(*args, &block)
  if Namespace.exists?(args.first)
    create_namespace(args.first)
  else
    call_method(args, &block)
  end
end
scope() click to toggle source

Access rights of this token. @return [Array] An array of symbols representing the access rights.

# File lib/vkontakte_api/client.rb, line 76
def scope
  SCOPE.reject do |access_scope, mask|
    (settings & mask).zero?
  end.keys
end

Private Instance Methods

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