Class: VkontakteApi::Client
- Inherits:
-
Object
- Object
- VkontakteApi::Client
- 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
-
#email ⇒ String
readonly
Current user email.
-
#expires_at ⇒ Time
readonly
Token expiration time.
-
#token ⇒ String
readonly
An access token needed by authorized requests.
-
#user_id ⇒ Integer
readonly
Current user id.
Instance Method Summary collapse
-
#authorized? ⇒ Boolean
Is a
VkontakteApi::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
VkontakteApi::Namespace
instance. -
#scope ⇒ Array
Access rights of this token.
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.
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
#email ⇒ String (readonly)
Current user email.
37 38 39 |
# File 'lib/vkontakte_api/client.rb', line 37 def email @email end |
#expires_at ⇒ Time (readonly)
Token expiration time
41 42 43 |
# File 'lib/vkontakte_api/client.rb', line 41 def expires_at @expires_at end |
#token ⇒ String (readonly)
An access token needed by authorized requests.
29 30 31 |
# File 'lib/vkontakte_api/client.rb', line 29 def token @token end |
#user_id ⇒ Integer (readonly)
Current user id.
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.
62 63 64 |
# File 'lib/vkontakte_api/client.rb', line 62 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.
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.
67 68 69 |
# File 'lib/vkontakte_api/client.rb', line 67 def expired? @expires_at && @expires_at < Time.now end |
#scope ⇒ Array
Access rights of this token.
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 |