Class: Vk::Client

Inherits:
Object
  • Object
show all
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

Author:

  • Alex Semyonov

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

#logger

DSL collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token = ENV['VK_ACCESS_TOKEN']) ⇒ Client

Returns a new instance of Client.

Parameters:

  • access_token (#to_s) (defaults to: ENV['VK_ACCESS_TOKEN'])

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_tokenString

Returns:

  • (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

Parameters:

  • viewer_id (Fixnum, String)

    viewer’s identifier


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

Returns:


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

Returns:

  • (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

.oauth_clientOAuth2::Client

Returns:

  • (OAuth2::Client)

54
55
56
57
# File 'lib/vk/client.rb', line 54

def self.oauth_client
  require 'vk/client/oauth2'
  @oauth_client ||= Vk::Client::OAuth2.new
end

Instance Method Details

#accountVk::API::Account

Returns:


100
101
102
103
# File 'lib/vk/api/client.rb', line 100

def 
  # require "vk/api/account"
  @account ||= Vk::API::Account.new('account', self)
end

#adsVk::API::Ads

Returns:


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

#appsVk::API::Apps

Returns:


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

#audioVk::API::Audio

Returns:


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

#authVk::API::Auth

Returns:


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

#baseVk::API::Base

Returns:


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

#boardVk::API::Board

Returns:


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

#databaseVk::API::Database

Returns:


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

#docsVk::API::Docs

Returns:


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

#faveVk::API::Fave

Returns:


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

#friendsVk::API::Friends

Returns:


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

#giftsVk::API::Gifts

Returns:


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

#groupsVk::API::Groups

Returns:


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

#leadsVk::API::Leads

Returns:


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

#likesVk::API::Likes

Returns:


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

#marketVk::API::Market

Returns:


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

#messagesVk::API::Messages

Returns:


105
106
107
108
# File 'lib/vk/api/client.rb', line 105

def messages
  # require "vk/api/messages"
  @messages ||= Vk::API::Messages.new('messages', self)
end

#newsfeedVk::API::Newsfeed

Returns:


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

#notesVk::API::Notes

Returns:


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

#notificationsVk::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

#oauthVk::API::Oauth

Returns:


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

#ordersVk::API::Orders

Returns:


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

#pagesVk::API::Pages

Returns:


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

#photosVk::API::Photos

Returns:


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

#placesVk::API::Places

Returns:


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

#pollsVk::API::Polls

Returns:


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

Parameters:

  • method_name (String)
  • data (Hash, #to_hash) (defaults to: {})

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

#searchVk::API::Search

Returns:


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

#secureVk::API::Secure

Returns:


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

#statsVk::API::Stats

Returns:


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

#statusVk::API::Status

Returns:


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

#storageVk::API::Storage

Returns:


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

Parameters:

  • method_name (URL::HTTP)

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

#usersVk::API::Users

Returns:


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

#utilsVk::API::Utils

Returns:


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

#videoVk::API::Video

Returns:


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

#wallVk::API::Wall

Returns:


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

#widgetsVk::API::Widgets

Returns:


35
36
37
38
# File 'lib/vk/api/client.rb', line 35

def widgets
  # require "vk/api/widgets"
  @widgets ||= Vk::API::Widgets.new('widgets', self)
end