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, offline: 65536, docs: 131072, groups: 262144, notifications: 524288, stats: 1048576, email: 4194304, market: 134217728 }
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.
51 52 53 54 55 56 57 58 59 60 61 62 |
# 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 |
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.
94 95 96 97 98 99 100 |
# 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 |
Instance Attribute Details
#email ⇒ String (readonly)
Current user email.
40 41 42 |
# File 'lib/vkontakte_api/client.rb', line 40 def email @email end |
#expires_at ⇒ Time (readonly)
Token expiration time
44 45 46 |
# File 'lib/vkontakte_api/client.rb', line 44 def expires_at @expires_at end |
#token ⇒ String (readonly)
An access token needed by authorized requests.
32 33 34 |
# File 'lib/vkontakte_api/client.rb', line 32 def token @token end |
#user_id ⇒ Integer (readonly)
Current user id.
36 37 38 |
# File 'lib/vkontakte_api/client.rb', line 36 def user_id @user_id end |
Instance Method Details
#authorized? ⇒ Boolean
Is a VkontakteApi::Client
instance authorized.
65 66 67 |
# File 'lib/vkontakte_api/client.rb', line 65 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.
84 85 86 87 88 89 90 |
# 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? ⇒ Boolean
Did the token already expire.
70 71 72 |
# File 'lib/vkontakte_api/client.rb', line 70 def expired? @expires_at && @expires_at < Time.now end |
#scope ⇒ Array
Access rights of this token.
76 77 78 79 80 |
# File 'lib/vkontakte_api/client.rb', line 76 def scope SCOPE.reject do |access_scope, mask| (settings & mask).zero? end.keys end |