Class: Vk::Client
- Inherits:
-
Object
- Object
- Vk::Client
- Includes:
- PromptExtension
- Defined in:
- lib/vk/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
Class Method Summary collapse
-
.auth_key(viewer_id) ⇒ Object
Generates auth_key for viewer.
- .authenticated! ⇒ Vk::Client
- .authenticated?(viewer_id, auth_key) ⇒ Boolean
- .dsl! ⇒ Object
- .oauth_client ⇒ OAuth2::Client
Instance Method Summary collapse
- #dsl! ⇒ Object
-
#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.
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_token ⇒ 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
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
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
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 |
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 |