Class: Vk::Client
- Inherits:
-
Object
- Object
- Vk::Client
- Extended by:
- Logging
- Includes:
- Logging, PromptExtension
- Defined in:
- lib/vk/client.rb,
lib/vk/api/client.rb,
lib/vk/client/oauth2.rb
Overview
Class for requesting vk.com api data
Defined Under Namespace
Classes: OAuth2
Constant Summary collapse
- SCHEME =
'https'
- HOST =
'api.vk.com'
- PATH =
'/method/'
- PORT =
443
Instance Attribute Summary collapse
Attributes included from Logging
DSL collapse
- #account ⇒ Vk::API::Account
- #ads ⇒ Vk::API::Ads
- #apps ⇒ Vk::API::Apps
- #audio ⇒ Vk::API::Audio
- #auth ⇒ Vk::API::Auth
- #base ⇒ Vk::API::Base
- #board ⇒ Vk::API::Board
- #database ⇒ Vk::API::Database
- #docs ⇒ Vk::API::Docs
- #fave ⇒ Vk::API::Fave
- #friends ⇒ Vk::API::Friends
- #gifts ⇒ Vk::API::Gifts
- #groups ⇒ Vk::API::Groups
- #leads ⇒ Vk::API::Leads
- #likes ⇒ Vk::API::Likes
- #market ⇒ Vk::API::Market
- #messages ⇒ Vk::API::Messages
- #newsfeed ⇒ Vk::API::Newsfeed
- #notes ⇒ Vk::API::Notes
- #notifications ⇒ Vk::API::Notifications
- #oauth ⇒ Vk::API::Oauth
- #orders ⇒ Vk::API::Orders
- #pages ⇒ Vk::API::Pages
- #photos ⇒ Vk::API::Photos
- #places ⇒ Vk::API::Places
- #polls ⇒ Vk::API::Polls
- #search ⇒ Vk::API::Search
- #secure ⇒ Vk::API::Secure
- #stats ⇒ Vk::API::Stats
- #status ⇒ Vk::API::Status
- #storage ⇒ Vk::API::Storage
- #users ⇒ Vk::API::Users
- #utils ⇒ Vk::API::Utils
- #video ⇒ Vk::API::Video
- #wall ⇒ Vk::API::Wall
- #widgets ⇒ Vk::API::Widgets
Class Method Summary collapse
-
.auth_key(viewer_id) ⇒ Object
Generates auth_key for viewer.
- .authenticated! ⇒ Vk::Client
- .authenticated?(viewer_id, auth_key) ⇒ Boolean
- .oauth_client ⇒ OAuth2::Client
Instance Method Summary collapse
-
#initialize(access_token = ) ⇒ Client
constructor
A new instance of Client.
- #request(method_name, data = {}) ⇒ Object
- #url_for_method(method_name) ⇒ Object
Constructor Details
#initialize(access_token = ) ⇒ Client
Returns a new instance of Client.
60 61 62 |
# File 'lib/vk/client.rb', line 60 def initialize(access_token = ENV['VK_ACCESS_TOKEN']) @access_token = access_token.to_s end |
Instance Attribute Details
#access_token ⇒ String
65 66 67 |
# File 'lib/vk/client.rb', line 65 def access_token @access_token end |
Class Method Details
.auth_key(viewer_id) ⇒ Object
Generates auth_key for viewer
29 30 31 |
# File 'lib/vk/client.rb', line 29 def self.auth_key(viewer_id) Digest::MD5.hexdigest("#{Vk.app_id}_#{viewer_id}_#{Vk.app_secret}") end |
.authenticated! ⇒ Vk::Client
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/vk/client.rb', line 38 def self.authenticated! require 'vk/access' return new(ENV['VK_ACCESS_TOKEN']) if ENV['VK_ACCESS_TOKEN'] require 'oauth2' token = oauth_client.get_access_token # => OAuth2::AccessToken raise 'No token discovered' unless token.try(:token) prompt.say 'Please run following command now to prevent asking for codes again:' prompt.say prompt.say " export VK_ACCESS_TOKEN=#{token.token}" prompt.say Vk.client.access_token ||= token.token ENV['VK_ACCESS_TOKEN'] ||= token.token Vk.client end |
.authenticated?(viewer_id, auth_key) ⇒ Boolean
33 34 35 |
# File 'lib/vk/client.rb', line 33 def self.authenticated?(viewer_id, auth_key) auth_key == self.auth_key(viewer_id) end |
Instance Method Details
#account ⇒ Vk::API::Account
100 101 102 103 |
# File 'lib/vk/api/client.rb', line 100 def account # require "vk/api/account" @account ||= Vk::API::Account.new('account', self) end |
#ads ⇒ Vk::API::Ads
170 171 172 173 |
# File 'lib/vk/api/client.rb', line 170 def ads # require "vk/api/ads" @ads ||= Vk::API::Ads.new('ads', self) end |
#apps ⇒ Vk::API::Apps
150 151 152 153 |
# File 'lib/vk/api/client.rb', line 150 def apps # require "vk/api/apps" @apps ||= Vk::API::Apps.new('apps', self) end |
#audio ⇒ Vk::API::Audio
60 61 62 63 |
# File 'lib/vk/api/client.rb', line 60 def audio # require "vk/api/audio" @audio ||= Vk::API::Audio.new('audio', self) end |
#auth ⇒ Vk::API::Auth
15 16 17 18 |
# File 'lib/vk/api/client.rb', line 15 def auth # require "vk/api/auth" @auth ||= Vk::API::Auth.new('auth', self) end |
#base ⇒ Vk::API::Base
180 181 182 183 |
# File 'lib/vk/api/client.rb', line 180 def base # require "vk/api/base" @base ||= Vk::API::Base.new('base', self) end |
#board ⇒ Vk::API::Board
80 81 82 83 |
# File 'lib/vk/api/client.rb', line 80 def board # require "vk/api/board" @board ||= Vk::API::Board.new('board', self) end |
#database ⇒ Vk::API::Database
160 161 162 163 |
# File 'lib/vk/api/client.rb', line 160 def database # require "vk/api/database" @database ||= Vk::API::Database.new('database', self) end |
#docs ⇒ Vk::API::Docs
125 126 127 128 |
# File 'lib/vk/api/client.rb', line 125 def docs # require "vk/api/docs" @docs ||= Vk::API::Docs.new('docs', self) end |
#fave ⇒ Vk::API::Fave
130 131 132 133 |
# File 'lib/vk/api/client.rb', line 130 def fave # require "vk/api/fave" @fave ||= Vk::API::Fave.new('fave', self) end |
#friends ⇒ Vk::API::Friends
30 31 32 33 |
# File 'lib/vk/api/client.rb', line 30 def friends # require "vk/api/friends" @friends ||= Vk::API::Friends.new('friends', self) end |
#gifts ⇒ Vk::API::Gifts
165 166 167 168 |
# File 'lib/vk/api/client.rb', line 165 def gifts # require "vk/api/gifts" @gifts ||= Vk::API::Gifts.new('gifts', self) end |
#groups ⇒ Vk::API::Groups
75 76 77 78 |
# File 'lib/vk/api/client.rb', line 75 def groups # require "vk/api/groups" @groups ||= Vk::API::Groups.new('groups', self) end |
#leads ⇒ Vk::API::Leads
65 66 67 68 |
# File 'lib/vk/api/client.rb', line 65 def leads # require "vk/api/leads" @leads ||= Vk::API::Leads.new('leads', self) end |
#likes ⇒ Vk::API::Likes
115 116 117 118 |
# File 'lib/vk/api/client.rb', line 115 def likes # require "vk/api/likes" @likes ||= Vk::API::Likes.new('likes', self) end |
#market ⇒ Vk::API::Market
175 176 177 178 |
# File 'lib/vk/api/client.rb', line 175 def market # require "vk/api/market" @market ||= Vk::API::Market.new('market', self) end |
#messages ⇒ Vk::API::Messages
105 106 107 108 |
# File 'lib/vk/api/client.rb', line 105 def # require "vk/api/messages" @messages ||= Vk::API::Messages.new('messages', self) end |
#newsfeed ⇒ Vk::API::Newsfeed
110 111 112 113 |
# File 'lib/vk/api/client.rb', line 110 def newsfeed # require "vk/api/newsfeed" @newsfeed ||= Vk::API::Newsfeed.new('newsfeed', self) end |
#notes ⇒ Vk::API::Notes
90 91 92 93 |
# File 'lib/vk/api/client.rb', line 90 def notes # require "vk/api/notes" @notes ||= Vk::API::Notes.new('notes', self) end |
#notifications ⇒ Vk::API::Notifications
135 136 137 138 |
# File 'lib/vk/api/client.rb', line 135 def notifications # require "vk/api/notifications" @notifications ||= Vk::API::Notifications.new('notifications', self) end |
#oauth ⇒ Vk::API::Oauth
185 186 187 188 |
# File 'lib/vk/api/client.rb', line 185 def oauth # require "vk/api/oauth" @oauth ||= Vk::API::Oauth.new('oauth', self) end |
#orders ⇒ Vk::API::Orders
50 51 52 53 |
# File 'lib/vk/api/client.rb', line 50 def orders # require "vk/api/orders" @orders ||= Vk::API::Orders.new('orders', self) end |
#pages ⇒ Vk::API::Pages
70 71 72 73 |
# File 'lib/vk/api/client.rb', line 70 def pages # require "vk/api/pages" @pages ||= Vk::API::Pages.new('pages', self) end |
#photos ⇒ Vk::API::Photos
25 26 27 28 |
# File 'lib/vk/api/client.rb', line 25 def photos # require "vk/api/photos" @photos ||= Vk::API::Photos.new('photos', self) end |
#places ⇒ Vk::API::Places
95 96 97 98 |
# File 'lib/vk/api/client.rb', line 95 def places # require "vk/api/places" @places ||= Vk::API::Places.new('places', self) end |
#polls ⇒ Vk::API::Polls
120 121 122 123 |
# File 'lib/vk/api/client.rb', line 120 def polls # require "vk/api/polls" @polls ||= Vk::API::Polls.new('polls', self) end |
#request(method_name, data = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/vk/client.rb', line 69 def request(method_name, data = {}) data = data.to_hash data = data.merge(app_id: Vk.app_id, v: Vk::VK_API) data = data.merge(access_token: access_token) if access_token.present? data.each do |argument, value| data[argument] = value.join(',') if value.is_a?(Array) end logger.info("vk.#{method_name}(#{data.inspect})") http_response = Net::HTTP.post_form(url_for_method(method_name), data).body return unless http_response.present? json_response = JSON.parse(http_response) if json_response['error'] logger.error(json_response['error']['error_msg']) logger.debug(json_response) raise Vk::Error, json_response end logger.debug(json_response) json_response end |
#search ⇒ Vk::API::Search
145 146 147 148 |
# File 'lib/vk/api/client.rb', line 145 def search # require "vk/api/search" @search ||= Vk::API::Search.new('search', self) end |
#secure ⇒ Vk::API::Secure
40 41 42 43 |
# File 'lib/vk/api/client.rb', line 40 def secure # require "vk/api/secure" @secure ||= Vk::API::Secure.new('secure', self) end |
#stats ⇒ Vk::API::Stats
140 141 142 143 |
# File 'lib/vk/api/client.rb', line 140 def stats # require "vk/api/stats" @stats ||= Vk::API::Stats.new('stats', self) end |
#status ⇒ Vk::API::Status
55 56 57 58 |
# File 'lib/vk/api/client.rb', line 55 def status # require "vk/api/status" @status ||= Vk::API::Status.new('status', self) end |
#storage ⇒ Vk::API::Storage
45 46 47 48 |
# File 'lib/vk/api/client.rb', line 45 def storage # require "vk/api/storage" @storage ||= Vk::API::Storage.new('storage', self) end |
#url_for_method(method_name) ⇒ Object
90 91 92 |
# File 'lib/vk/client.rb', line 90 def url_for_method(method_name) URI.parse("#{SCHEME}://#{HOST}:#{PORT}#{PATH}#{method_name}") end |
#users ⇒ Vk::API::Users
10 11 12 13 |
# File 'lib/vk/api/client.rb', line 10 def users # require "vk/api/users" @users ||= Vk::API::Users.new('users', self) end |
#utils ⇒ Vk::API::Utils
155 156 157 158 |
# File 'lib/vk/api/client.rb', line 155 def utils # require "vk/api/utils" @utils ||= Vk::API::Utils.new('utils', self) end |
#video ⇒ Vk::API::Video
85 86 87 88 |
# File 'lib/vk/api/client.rb', line 85 def video # require "vk/api/video" @video ||= Vk::API::Video.new('video', self) end |
#wall ⇒ Vk::API::Wall
20 21 22 23 |
# File 'lib/vk/api/client.rb', line 20 def wall # require "vk/api/wall" @wall ||= Vk::API::Wall.new('wall', self) end |
#widgets ⇒ Vk::API::Widgets
35 36 37 38 |
# File 'lib/vk/api/client.rb', line 35 def # require "vk/api/widgets" @widgets ||= Vk::API::Widgets.new('widgets', self) end |