Class: VkApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/vk-api/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/vk-api/client.rb', line 5

def initialize(params)
  @params = {
    redirect_url: 'https://oauth.vk.com/blank.html',
    version: '5.28',
    settings: 'notify,friends,offline',
    display: 'page',
    host: 'https://api.vk.com'
  }

  @params.merge! params
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



2
3
4
# File 'lib/vk-api/client.rb', line 2

def access_token
  @access_token
end

#expires_inObject (readonly)

Returns the value of attribute expires_in.



3
4
5
# File 'lib/vk-api/client.rb', line 3

def expires_in
  @expires_in
end

Instance Method Details

#api(method, params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vk-api/client.rb', line 17

def api(method, params = {})
  params[:version] ||= @params[:version]
  params[:access_token] ||= @access_token

  url = URI.encode_www_form(params)
  url = "#{@params[:host]}/method/#{method}?#{url}"

  res = browser.request url
  if res['error']
    if res['error']['code'] == 14 && @params[:antigate]
      min = @params[:antigate_min]
      if !min || min > Antigate.balance(@params[:antigate])
        gate = Antigate.wrap @params[:antigate]
        code = gate.recognize(res['error']['captcha_url'], 'jpg')[1]
        params[:captcha_sid] = res['error']['captcha_sid']
        params[:captcha_key] = code
        api method, params
      end
    else
      raise VkApi::ApiError.new({ name: 'API', description: 'error', more: res['error'] })
    end
  else
    res['response']
  end
end

#client_auth(login, pass, params) ⇒ Object

Raises:



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vk-api/client.rb', line 43

def client_auth(, pass, params)
  @params.merge! params

  browser. , pass
  browser.security_hack 

  @expires_in = browser.response['expires_in']
  @access_token = browser.response['access_token']

  raise VkApi::AuthError.new({name: 'Auth', description: 'login or password is incorrect'}) unless @access_token

  return @access_token, @expires_in
end