Class: Vkontakte::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/vkontakte/client.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id = nil, api_version: Vkontakte::API_VERSION, proxy: nil, timeout: 60) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/vkontakte/client.rb', line 8

def initialize(
  client_id = nil,
  api_version: Vkontakte::API_VERSION,
  proxy: nil,
  timeout: 60
)
  @client_id = client_id
  @api_version = api_version
  @proxy = proxy
  @timeout = timeout
  @authorize = false

  @api = Vkontakte::API.new
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



6
7
8
# File 'lib/vkontakte/client.rb', line 6

def access_token
  @access_token
end

#apiObject (readonly)

Returns the value of attribute api.



6
7
8
# File 'lib/vkontakte/client.rb', line 6

def api
  @api
end

#api_versionObject (readonly)

Returns the value of attribute api_version.



6
7
8
# File 'lib/vkontakte/client.rb', line 6

def api_version
  @api_version
end

#expires_inObject (readonly)

Returns the value of attribute expires_in.



6
7
8
# File 'lib/vkontakte/client.rb', line 6

def expires_in
  @expires_in
end

#user_idObject (readonly)

Returns the value of attribute user_id.



6
7
8
# File 'lib/vkontakte/client.rb', line 6

def user_id
  @user_id
end

Instance Method Details

#authorized?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/vkontakte/client.rb', line 69

def authorized?
  @authorize ? true : false
end

#login!(email, pass, open_captcha: false, permissions: '') ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vkontakte/client.rb', line 23

def login!(email, pass, open_captcha: false, permissions: '')
  @email = email
  @pass = pass

  redirect_uri  = 'https://oauth.vk.com/blank.html'
  display       = 'mobile'
  response_type = 'token'

  query = {
    client_id: @client_id,
    redirect_uri: redirect_uri,
    display: display,
    scope: permissions,
    response_type: response_type,
    v: api_version
  }

  agent = Mechanize.new do |a|
    a.user_agent_alias = 'Linux Firefox'
    a.follow_meta_refresh

    a.agent.set_socks(@proxy.addr, @proxy.port) if @proxy&.socks?
    a.agent.set_proxy(@proxy.addr, @proxy.port) if @proxy&.http?
  end

  # https://vk.com/dev/implicit_flow_user
  #
  # Открытие диалога авторизации OAuth
  #
  query_string = query.map { |k, v| "#{k}=#{v}" }.join('&')
  url = "https://oauth.vk.com/authorize?#{query_string}"

  page = agent.get(url)

   = page.forms.first
  .email = @email
  .pass = @pass
  page = .submit

  raise('Invalid login or password.') unless page.search('.service_msg_warning').empty?

  page = submit_gain_access_form(page, open_captcha) if page.uri.path == '/authorize'

  get_token(page)
end