Class: Vk::Client

Inherits:
Object
  • Object
show all
Includes:
PromptExtension
Defined in:
lib/vk/client.rb,
lib/vk/client/oauth2.rb

Overview

Class for requesting vk.com api data

Author:

  • Alexander Semyonov

Defined Under Namespace

Classes: OAuth2

Constant Summary collapse

SCHEME =
'https'
HOST =
'api.vk.com'
PATH =
'/method/'
PORT =
443

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token = ) ⇒ Client

Returns a new instance of Client.

Parameters:

  • access_token (#to_s) (defaults to: )


62
63
64
# File 'lib/vk/client.rb', line 62

def initialize(access_token = ENV['VK_ACCESS_TOKEN'])
  @access_token = access_token.to_s
end

Instance Attribute Details

#access_tokenString

Returns:

  • (String)


72
73
74
# File 'lib/vk/client.rb', line 72

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



26
27
28
# File 'lib/vk/client.rb', line 26

def self.auth_key(viewer_id)
  Digest::MD5.hexdigest("#{Vk.app_id}_#{viewer_id}_#{Vk.app_secret}")
end

.authenticated!Vk::Client

Returns:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vk/client.rb', line 40

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
  fail '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)


30
31
32
# File 'lib/vk/client.rb', line 30

def self.authenticated?(viewer_id, auth_key)
  auth_key == self.auth_key(viewer_id)
end

.dsl!Object



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

def self.dsl!
  require 'vk/dsl'
  include Vk::DSL
end

.oauth_clientOAuth2::Client

Returns:

  • (OAuth2::Client)


56
57
58
59
# File 'lib/vk/client.rb', line 56

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

Instance Method Details

#dsl!Object



66
67
68
69
# File 'lib/vk/client.rb', line 66

def dsl!
  self.class.dsl!
  self
end

#request(method_name, data = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vk/client.rb', line 74

def request(method_name, data = {})
  data = data.merge(app_id: Vk.app_id, v: Vk::VK_API)
  data = data.merge(access_token: access_token) if access_token.present?
  Vk.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']
    Vk.logger.error(json_response['error']['error_msg'])
    Vk.logger.debug(json_response)
    raise Vk::Error.new(json_response)
  end
  Vk.logger.debug(json_response)
  json_response['response']
end

#url_for_method(method_name) ⇒ Object

Parameters:

  • method_name (URL::HTTP)


91
92
93
# File 'lib/vk/client.rb', line 91

def url_for_method(method_name)
  URI.parse("#{SCHEME}://#{HOST}:#{PORT}#{PATH}#{method_name}")
end