vkontakte_api 1.4.3 → 1.4.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +20 -3
- data/Guardfile +2 -1
- data/README.md +15 -12
- data/README_en.md +312 -0
- data/lib/vkontakte_api.rb +2 -1
- data/lib/vkontakte_api/api.rb +2 -2
- data/lib/vkontakte_api/client.rb +4 -1
- data/lib/vkontakte_api/namespaces.yml +2 -0
- data/lib/vkontakte_api/version.rb +1 -1
- data/spec/integration_spec.rb +0 -5
- data/spec/multi_json_spec.rb +7 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/support/mechanized_authorization.rb +2 -2
- data/spec/vkontakte_api/api_spec.rb +3 -3
- data/spec/vkontakte_api/logger_spec.rb +3 -3
- data/vkontakte_api.gemspec +15 -8
- metadata +57 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a8b75c1ddf49079f8dbe3479252710f5fdf5e54
|
4
|
+
data.tar.gz: a6fd7a5edb3643998eeea6f66af3f7355dd04594
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0eec2d055546c922ffa54d06ad5c7a04b496fa5b940b61b56a1a3b6f3c2eee8f8d97aaf432b1a9abdfaa278f006bdb516e7e0e2f927d1f9e8247c64fd1715951
|
7
|
+
data.tar.gz: dde7a7fe2380644ce37a25ffdf1bc1932570017bcb0104af8f42836a438e974a8085378e407819554f71bca959b00714671f21a0064274a4af4a1f2fcba58e5a
|
data/.travis.yml
CHANGED
@@ -1,4 +1,21 @@
|
|
1
|
+
language: ruby
|
2
|
+
|
1
3
|
rvm:
|
2
|
-
- 1
|
3
|
-
- 2.
|
4
|
-
- 2.
|
4
|
+
- 2.1
|
5
|
+
- 2.2
|
6
|
+
- 2.3
|
7
|
+
- 2.4
|
8
|
+
- ruby-head
|
9
|
+
- jruby-9.0.0.0
|
10
|
+
- jruby-head
|
11
|
+
|
12
|
+
jdk:
|
13
|
+
- oraclejdk8
|
14
|
+
|
15
|
+
matrix:
|
16
|
+
allow_failures:
|
17
|
+
- rvm: ruby-head
|
18
|
+
- rvm: jruby-head
|
19
|
+
|
20
|
+
before_install:
|
21
|
+
- gem install bundler
|
data/Guardfile
CHANGED
@@ -6,8 +6,9 @@ guard 'rspec', all_on_start: true, all_after_pass: true, cmd: 'bin/rspec' do
|
|
6
6
|
watch('spec/support/mechanized_authorization.rb') { 'spec/integration_spec.rb' }
|
7
7
|
end
|
8
8
|
|
9
|
+
|
9
10
|
guard 'yard' do
|
10
11
|
watch(%r{lib/.+\.rb})
|
11
|
-
end
|
12
|
+
end unless defined?(JRUBY_VERSION)
|
12
13
|
|
13
14
|
notification :terminal_notifier, activate: 'com.googlecode.iTerm2'
|
data/README.md
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
-
## vkontakte_api [![Build Status](https://secure.travis-ci.org/7even/vkontakte_api.png)](http://travis-ci.org/7even/vkontakte_api) [![Gem Version](https://badge.fury.io/rb/vkontakte_api.png)](http://badge.fury.io/rb/vkontakte_api) [![Dependency Status](https://gemnasium.com/7even/vkontakte_api.png)](https://gemnasium.com/7even/vkontakte_api) [![Code Climate](https://codeclimate.com/github/7even/vkontakte_api.png)](https://codeclimate.com/github/7even/vkontakte_api)
|
2
|
-
[![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/7even/vkontakte_api)
|
1
|
+
## vkontakte_api [![Build Status](https://secure.travis-ci.org/7even/vkontakte_api.png)](http://travis-ci.org/7even/vkontakte_api) [![Gem Version](https://badge.fury.io/rb/vkontakte_api.png)](http://badge.fury.io/rb/vkontakte_api) [![Dependency Status](https://gemnasium.com/7even/vkontakte_api.png)](https://gemnasium.com/7even/vkontakte_api) [![Code Climate](https://codeclimate.com/github/7even/vkontakte_api.png)](https://codeclimate.com/github/7even/vkontakte_api) [![Gitter](https://badges.gitter.im/gitterHQ/gitter.svg)](https://gitter.im/7even/vkontakte_api)
|
3
2
|
|
4
3
|
`vkontakte_api` — ruby-адаптер для ВКонтакте API. Он позволяет вызывать методы API, загружать файлы на сервера ВКонтакте, а также поддерживает все 3 доступных способа авторизации (при этом позволяя использовать стороннее решение).
|
5
4
|
|
5
|
+
## English version
|
6
|
+
For English version please go [here](README_en.md).
|
7
|
+
|
6
8
|
## Установка
|
7
9
|
|
8
10
|
``` ruby
|
@@ -24,7 +26,7 @@ $ gem install vkontakte_api
|
|
24
26
|
# создаем клиент
|
25
27
|
@vk = VkontakteApi::Client.new
|
26
28
|
# и вызываем методы API
|
27
|
-
@vk.users.get(
|
29
|
+
@vk.users.get(user_ids: 1)
|
28
30
|
|
29
31
|
# в ruby принято использовать snake_case в названиях методов,
|
30
32
|
# поэтому likes.getList становится likes.get_list
|
@@ -36,7 +38,7 @@ $ gem install vkontakte_api
|
|
36
38
|
|
37
39
|
# если ВКонтакте ожидает получить параметр в виде списка,
|
38
40
|
# разделенного запятыми, то его можно передать массивом
|
39
|
-
users = @vk.users.get(
|
41
|
+
users = @vk.users.get(user_ids: [1, 2, 3])
|
40
42
|
|
41
43
|
# большинство методов возвращает структуры Hashie::Mash
|
42
44
|
# и массивы из них
|
@@ -48,7 +50,7 @@ users.first.last_name # => "Дуров"
|
|
48
50
|
# то блок будет выполнен для каждого элемента,
|
49
51
|
# и метод вернет обработанный массив
|
50
52
|
fields = [:first_name, :last_name, :screen_name]
|
51
|
-
@vk.friends.get(
|
53
|
+
@vk.friends.get(user_id: 2, fields: fields) do |friend|
|
52
54
|
"#{friend.first_name} '#{friend.screen_name}' #{friend.last_name}"
|
53
55
|
end
|
54
56
|
# => ["Павел 'durov' Дуров"]
|
@@ -185,8 +187,8 @@ VK # => NameError: uninitialized constant VK
|
|
185
187
|
|
186
188
|
``` ruby
|
187
189
|
vk = VK::Client.new
|
188
|
-
vk.friends.get(
|
189
|
-
# VkontakteApi::Error: VKontakte returned an error 7: 'Permission to perform this action is denied' after calling method 'friends.get' with parameters {"
|
190
|
+
vk.friends.get(user_id: 1, fields: [:first_name, :last_name, :photo])
|
191
|
+
# VkontakteApi::Error: VKontakte returned an error 7: 'Permission to perform this action is denied' after calling method 'friends.get' with parameters {"user_id"=>"1", "fields"=>"first_name,last_name,photo"}.
|
190
192
|
```
|
191
193
|
|
192
194
|
Особый случай ошибки — 14: необходимо ввести код с captcha.
|
@@ -232,7 +234,7 @@ VkontakteApi.configure do |config|
|
|
232
234
|
config.app_id = '123'
|
233
235
|
config.app_secret = 'AbCdE654'
|
234
236
|
config.redirect_uri = 'http://example.com/oauth/callback'
|
235
|
-
|
237
|
+
|
236
238
|
# faraday-адаптер для сетевых запросов
|
237
239
|
config.adapter = :net_http
|
238
240
|
# HTTP-метод для вызова методов API (:get или :post)
|
@@ -249,21 +251,22 @@ VkontakteApi.configure do |config|
|
|
249
251
|
}
|
250
252
|
}
|
251
253
|
# максимальное количество повторов запроса при ошибках
|
254
|
+
# работает только если переключить http_verb в :get
|
252
255
|
config.max_retries = 2
|
253
|
-
|
256
|
+
|
254
257
|
# логгер
|
255
258
|
config.logger = Rails.logger
|
256
259
|
config.log_requests = true # URL-ы запросов
|
257
260
|
config.log_errors = true # ошибки
|
258
261
|
config.log_responses = false # удачные ответы
|
259
|
-
|
262
|
+
|
260
263
|
# используемая версия API
|
261
264
|
config.api_version = '5.21'
|
262
265
|
end
|
263
266
|
```
|
264
267
|
|
265
268
|
По умолчанию для HTTP-запросов используется `Net::HTTP`; можно выбрать
|
266
|
-
[любой другой адаптер](https://github.com/
|
269
|
+
[любой другой адаптер](https://github.com/lostisland/faraday/blob/master/lib/faraday/adapter.rb),
|
267
270
|
поддерживаемый `faraday`.
|
268
271
|
|
269
272
|
ВКонтакте [позволяет](https://vk.com/dev/api_requests)
|
@@ -306,7 +309,7 @@ $ rails generate vkontakte_api:install
|
|
306
309
|
положите свои изменения в отдельную ветку, покройте их спеками
|
307
310
|
и отправьте мне pull request.
|
308
311
|
|
309
|
-
`vkontakte_api` тестируется под MRI `1.
|
312
|
+
`vkontakte_api` тестируется под MRI `2.1`, `2.2`, `2.3` и `2.4`, а также JRuby `9.x`.
|
310
313
|
Если в одной из этих сред что-то работает неправильно, либо вообще не работает,
|
311
314
|
то это следует считать багом, и написать об этом
|
312
315
|
в [issues на Github](https://github.com/7even/vkontakte_api/issues).
|
data/README_en.md
ADDED
@@ -0,0 +1,312 @@
|
|
1
|
+
## vkontakte_api [![Build Status](https://secure.travis-ci.org/7even/vkontakte_api.png)](http://travis-ci.org/7even/vkontakte_api) [![Gem Version](https://badge.fury.io/rb/vkontakte_api.png)](http://badge.fury.io/rb/vkontakte_api) [![Dependency Status](https://gemnasium.com/7even/vkontakte_api.png)](https://gemnasium.com/7even/vkontakte_api) [![Code Climate](https://codeclimate.com/github/7even/vkontakte_api.png)](https://codeclimate.com/github/7even/vkontakte_api) [![Gitter](https://badges.gitter.im/gitterHQ/gitter.svg)](https://gitter.im/7even/vkontakte_api)
|
2
|
+
|
3
|
+
`vkontakte_api` is a ruby-adapter for VKontakte API. It allows you to call API methods, upload files to VKontakte servers and also supports 3 available authorization methods (while allowing to use third-party solutions).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
``` ruby
|
8
|
+
# Gemfile
|
9
|
+
gem 'vkontakte_api', '~> 1.4'
|
10
|
+
```
|
11
|
+
|
12
|
+
or simply
|
13
|
+
|
14
|
+
``` sh
|
15
|
+
$ gem install vkontakte_api
|
16
|
+
```
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
### Calling API methods
|
21
|
+
|
22
|
+
``` ruby
|
23
|
+
# creating client
|
24
|
+
@vk = VkontakteApi::Client.new
|
25
|
+
# and calling API method
|
26
|
+
@vk.users.get(user_ids: 1)
|
27
|
+
|
28
|
+
# snake_case convention is a de-facto standard
|
29
|
+
# for method names in ruby
|
30
|
+
# so likes.getList becomes likes.get_list
|
31
|
+
@vk.likes.get_list
|
32
|
+
# names of methods that return '1' or '0'
|
33
|
+
# end with '?', and returned values
|
34
|
+
# are typecasted to true or false
|
35
|
+
@vk.is_app_user? # => false
|
36
|
+
|
37
|
+
# if some VKontakte method expects a list of parameters
|
38
|
+
# separated by commas, you can pass an array
|
39
|
+
users = @vk.users.get(user_ids: [1, 2, 3])
|
40
|
+
|
41
|
+
# most methods return a Hashie::Mash structure
|
42
|
+
# or an array of them
|
43
|
+
users.first.uid # => 1
|
44
|
+
users.first.first_name # => "Павел"
|
45
|
+
users.first.last_name # => "Дуров"
|
46
|
+
|
47
|
+
# when a method that returns an array is being called with a block
|
48
|
+
# then block will be executed for each element
|
49
|
+
# and the final result will be a processed array (like Enumerable#map)
|
50
|
+
fields = [:first_name, :last_name, :screen_name]
|
51
|
+
@vk.friends.get(user_id: 2, fields: fields) do |friend|
|
52
|
+
"#{friend.first_name} '#{friend.screen_name}' #{friend.last_name}"
|
53
|
+
end
|
54
|
+
# => ["Павел 'durov' Дуров"]
|
55
|
+
```
|
56
|
+
|
57
|
+
### File upload
|
58
|
+
|
59
|
+
Files can be uploaded to VKontakte in several steps: first you need to call an API method that returns a URL to upload to; then upload the file(s) and in some cases call another finalization API method with uploaded file params. The API methods to call depend on the uploaded file type and are described in the [documentation](https://vk.com/dev/upload_files).
|
60
|
+
|
61
|
+
Files are transferred in a Hash format, where key is the name of the request parameter (described in documentation, for example `photo` when uploading photos), and the value is an array composed of 2 strings: full path to the file and it's MIME-type:
|
62
|
+
|
63
|
+
``` ruby
|
64
|
+
url = 'http://cs303110.vkontakte.ru/upload.php?act=do_add'
|
65
|
+
VkontakteApi.upload(url: url, photo: ['/path/to/file.jpg', 'image/jpeg'])
|
66
|
+
```
|
67
|
+
|
68
|
+
If the file was opened as an IO object, then it can be passed using alternative syntax — IO-object, MIME-type and the file path:
|
69
|
+
|
70
|
+
``` ruby
|
71
|
+
url = 'http://cs303110.vkontakte.ru/upload.php?act=do_add'
|
72
|
+
VkontakteApi.upload(url: url, photo: [file_io, 'image/jpeg', '/path/to/file.jpg'])
|
73
|
+
```
|
74
|
+
|
75
|
+
The `upload` method returns VKontakte server response converted to `Hashie::Mash`; it can be used when calling API method in the last step of upload process.
|
76
|
+
|
77
|
+
### Authorization
|
78
|
+
|
79
|
+
Most of methods require an access token to be called. To get this token, you can use authorization built in `vkontakte_api` or any other solution (for example [OmniAuth](https://github.com/intridea/omniauth)). In the latter case the result of authorization process is a token, which needs to be passed into `VkontakteApi::Client.new`.
|
80
|
+
|
81
|
+
VKontakte API provides 3 types of authorization: for webpages, for client applications (mobile or desktop, having access to authorized browsers) and special authorization type for servers to invoke administration methods without user authorization. More details are available [here](https://vk.com/dev/authentication); let's see how to use them with `vkontakte_api`.
|
82
|
+
|
83
|
+
For the purposes of authorization you have to specify `app_id` (application ID), `app_secret` (secret key) and `redirect_uri` (a URL where the user will be redirected after successful authorization) in the `VkontakteApi.configure` settings. For more information about configuring `vkontakte_api` see below.
|
84
|
+
|
85
|
+
##### Website
|
86
|
+
|
87
|
+
Website authorization goes in 2 steps. First user is redirected to VKontakte website to confirm the rights of a website to access his data. The list of available permissions is avaliable [here](https://vk.com/dev/permissions). Let's suppose one wants to access friends (`friends`) and photos (`photos`) of the user.
|
88
|
+
|
89
|
+
According to the [guidelines](http://tools.ietf.org/html/draft-ietf-oauth-v2-30#section-10.12) in OAuth2 protocol the `state` parameter should be passed with a random number in order to prevent [CSRF](http://ru.wikipedia.org/wiki/%D0%9F%D0%BE%D0%B4%D0%B4%D0%B5%D0%BB%D0%BA%D0%B0_%D0%BC%D0%B5%D0%B6%D1%81%D0%B0%D0%B9%D1%82%D0%BE%D0%B2%D1%8B%D1%85_%D0%B7%D0%B0%D0%BF%D1%80%D0%BE%D1%81%D0%BE%D0%B2).
|
90
|
+
|
91
|
+
``` ruby
|
92
|
+
session[:state] = Digest::MD5.hexdigest(rand.to_s)
|
93
|
+
redirect_to VkontakteApi.authorization_url(scope: [:notify, :friends, :photos], state: session[:state])
|
94
|
+
```
|
95
|
+
|
96
|
+
After successful authorization the user is redirected to `redirect_uri` and the parameters will contain the code that can be used to obtain an access token, as well as the `state`. When `state` differs from the one used in the first step there is a high probability of CSRF-attack — the user should be forced to begin the authorization process from the beginning.
|
97
|
+
|
98
|
+
``` ruby
|
99
|
+
redirect_to login_url, alert: 'Authorization error' if params[:state] != session[:state]
|
100
|
+
```
|
101
|
+
|
102
|
+
`vkontakte_api` provides a `VkontakteApi.authorize` method which makes a request to VKontakte with a given code, gets a token and creates a client:
|
103
|
+
|
104
|
+
``` ruby
|
105
|
+
@vk = VkontakteApi.authorize(code: params[:code])
|
106
|
+
# now API methods can be called on @vk object
|
107
|
+
@vk.is_app_user?
|
108
|
+
```
|
109
|
+
|
110
|
+
Client will contain the id of the user who authorized the application; it can be obtained using the `VkontakteApi::Client#user_id` method:
|
111
|
+
|
112
|
+
``` ruby
|
113
|
+
@vk.user_id # => 123456
|
114
|
+
```
|
115
|
+
|
116
|
+
It's also helpful to keep the obtained token at this point (and the user id if necessary) in the DB or in session so they can be used again in the future:
|
117
|
+
|
118
|
+
``` ruby
|
119
|
+
current_user.token = @vk.token
|
120
|
+
current_user.vk_id = @vk.user_id
|
121
|
+
current_user.save
|
122
|
+
# later
|
123
|
+
@vk = VkontakteApi::Client.new(current_user.token)
|
124
|
+
```
|
125
|
+
|
126
|
+
##### Client application
|
127
|
+
|
128
|
+
Client application authorization is much easier: you don't need a separate request to obtain the token — it's returned right after the user is redirected back to the application.
|
129
|
+
|
130
|
+
``` ruby
|
131
|
+
# user is redirected to the following URL
|
132
|
+
VkontakteApi.authorization_url(type: :client, scope: [:friends, :photos])
|
133
|
+
```
|
134
|
+
|
135
|
+
You should note that `redirect_uri` must be assigned to `http://api.vkontakte.ru/blank.html`, otherwise it won't be possible to call methods available to client applications.
|
136
|
+
|
137
|
+
After user confirms his access rights he will be redirected to `redirect_uri` with the `access_token` parameter containing the token that should be passed to `VkontakteApi::Client.new`.
|
138
|
+
|
139
|
+
##### Application server
|
140
|
+
|
141
|
+
The last type of authorization is the easiest one since it does not require any user involvement.
|
142
|
+
|
143
|
+
``` ruby
|
144
|
+
@vk = VkontakteApi.authorize(type: :app_server)
|
145
|
+
```
|
146
|
+
|
147
|
+
### Other
|
148
|
+
|
149
|
+
If the API client (instance of `VkontakteApi::Client`) was created by calling `VkontakteApi.authorize` method, it will contain the information about current user id (`user_id`) and about expiry time of token (`expires_at`). You can check it using these methods:
|
150
|
+
|
151
|
+
``` ruby
|
152
|
+
vk = VkontakteApi.authorize(code: 'c1493e81f69fce1b43')
|
153
|
+
# => #<VkontakteApi::Client:0x007fa578f00ad0>
|
154
|
+
vk.user_id # => 628985
|
155
|
+
vk.expires_at # => 2012-12-18 23:22:55 +0400
|
156
|
+
# VkontakteApi::Client#expired can be used to check if the token has expired
|
157
|
+
vk.expired? # => false
|
158
|
+
```
|
159
|
+
|
160
|
+
You can also get the list of access rights given by this token, in the form similar to the form of `scope` parameter in authorization process:
|
161
|
+
|
162
|
+
``` ruby
|
163
|
+
vk.scope # => [:friends, :groups]
|
164
|
+
```
|
165
|
+
|
166
|
+
It uses the `getUserSettings` API method with the result cached after the first call.
|
167
|
+
|
168
|
+
To create a `VK` alias for `VkontakteApi` module just call `VkontakteApi.register_alias`:
|
169
|
+
|
170
|
+
``` ruby
|
171
|
+
VkontakteApi.register_alias
|
172
|
+
VK::Client.new # => #<VkontakteApi::Client:0x007fa578d6d948>
|
173
|
+
```
|
174
|
+
|
175
|
+
This alias can be removed with `VkontakteApi.unregister_alias`:
|
176
|
+
|
177
|
+
``` ruby
|
178
|
+
VK.unregister_alias
|
179
|
+
VK # => NameError: uninitialized constant VK
|
180
|
+
```
|
181
|
+
|
182
|
+
### Error handling
|
183
|
+
|
184
|
+
When VKontakte API returns an error, a `VkontakteApi::Error` exception is raised.
|
185
|
+
|
186
|
+
``` ruby
|
187
|
+
vk = VK::Client.new
|
188
|
+
vk.friends.get(user_id: 1, fields: [:first_name, :last_name, :photo])
|
189
|
+
# VkontakteApi::Error: VKontakte returned an error 7: 'Permission to perform this action is denied' after calling method 'friends.get' with parameters {"user_id"=>"1", "fields"=>"first_name,last_name,photo"}.
|
190
|
+
```
|
191
|
+
|
192
|
+
There is special case of errors — 14: the user has to enter a captcha code.
|
193
|
+
In this case captcha parameters can be obtained using `VkontakteApi::Error#captcha_sid` method
|
194
|
+
and `VkontakteApi::Error#captcha_img` — for example,
|
195
|
+
[like this](https://github.com/7even/vkontakte_api/issues/10#issuecomment-11666091).
|
196
|
+
|
197
|
+
### Logging
|
198
|
+
|
199
|
+
`vkontakte_api` logs information about requests when calling methods.
|
200
|
+
By default they are all written in `STDOUT` but you can choose any
|
201
|
+
other data logger, `Rails.logger` for example.
|
202
|
+
|
203
|
+
It is possible to log 3 types of information,
|
204
|
+
and each one has a corresponding key in the global settings.
|
205
|
+
|
206
|
+
| | setting key | default value | message level |
|
207
|
+
| ------------------------ | --------------- | ------------- | ------------- |
|
208
|
+
| request URL | `log_requests` | `true` | `debug` |
|
209
|
+
| JSON response of error | `log_errors` | `true` | `warn` |
|
210
|
+
| JSON successful response | `log_responses` | `false` | `debug` |
|
211
|
+
|
212
|
+
In Rails application with default settings in production only server responses
|
213
|
+
with errors are being saved; in development there are also logged URL addresses of requests.
|
214
|
+
|
215
|
+
### Example
|
216
|
+
|
217
|
+
An example of using `vkontakte_api` together with `eventmachine` can be seen
|
218
|
+
[here](https://github.com/7even/vkontakte_on_em).
|
219
|
+
|
220
|
+
[An example with rails](https://github.com/7even/vkontakte_on_rails) also
|
221
|
+
exists, but it no longer works due to the lack of access rights to call
|
222
|
+
the `newsfeed.get` method from websites.
|
223
|
+
|
224
|
+
## Settings
|
225
|
+
|
226
|
+
Global parameters of `vkontakte_api` should be set in `VkontakteApi.configure` block as follows:
|
227
|
+
|
228
|
+
``` ruby
|
229
|
+
VkontakteApi.configure do |config|
|
230
|
+
# parameters required for authorization with vkontakte_api
|
231
|
+
# (not needed when using a third-party authorization solution)
|
232
|
+
config.app_id = '123'
|
233
|
+
config.app_secret = 'AbCdE654'
|
234
|
+
config.redirect_uri = 'http://example.com/oauth/callback'
|
235
|
+
|
236
|
+
# faraday adapter for network requests
|
237
|
+
config.adapter = :net_http
|
238
|
+
# HTTP method for calling API methods (:get or :post)
|
239
|
+
config.http_verb = :post
|
240
|
+
# parameters for faraday connection
|
241
|
+
config.faraday_options = {
|
242
|
+
ssl: {
|
243
|
+
ca_path: '/usr/lib/ssl/certs'
|
244
|
+
},
|
245
|
+
proxy: {
|
246
|
+
uri: 'http://proxy.example.com',
|
247
|
+
user: 'foo',
|
248
|
+
password: 'bar'
|
249
|
+
}
|
250
|
+
}
|
251
|
+
|
252
|
+
# maximum number of retries after an error
|
253
|
+
# works only when using http_verb :get
|
254
|
+
config.max_retries = 2
|
255
|
+
|
256
|
+
# logger
|
257
|
+
config.logger = Rails.logger
|
258
|
+
config.log_requests = true # request URLs
|
259
|
+
config.log_errors = true # errors
|
260
|
+
config.log_responses = false # successful responses
|
261
|
+
|
262
|
+
# API version
|
263
|
+
config.api_version = '5.21'
|
264
|
+
end
|
265
|
+
```
|
266
|
+
|
267
|
+
By default the `Net::HTTP` Faraday adapter is used but you can choose
|
268
|
+
[any other adapter](https://github.com/lostisland/faraday/blob/master/lib/faraday/adapter.rb)
|
269
|
+
supported by `faraday`.
|
270
|
+
|
271
|
+
VKontakte [allows](https://vk.com/dev/api_requests)
|
272
|
+
using either `GET` or `POST` requests when calling API methods.
|
273
|
+
`vkontakte_api` uses `POST` by default but you can change that by
|
274
|
+
setting `http_verb` to `:get`.
|
275
|
+
|
276
|
+
You can specify custom parameters for faraday-connection if necessary —
|
277
|
+
parameters of a proxy server or a path to SSL certificates for example.
|
278
|
+
|
279
|
+
In order to use a specific API version in every method call you can specify it
|
280
|
+
in the `api_version` key of configuration. By default it is not set.
|
281
|
+
|
282
|
+
To generate a file with default settings in Rails application
|
283
|
+
you can use the `vkontakte_api:install` generator:
|
284
|
+
|
285
|
+
``` sh
|
286
|
+
$ cd /path/to/app
|
287
|
+
$ rails generate vkontakte_api:install
|
288
|
+
```
|
289
|
+
|
290
|
+
## JSON parser
|
291
|
+
|
292
|
+
`vkontakte_api` uses the [Oj](https://github.com/ohler55/oj) parser — it is
|
293
|
+
the only parser that has not thrown any [errors](https://github.com/7even/vkontakte_api/issues/1)
|
294
|
+
while parsing JSON generated by VKontakte API.
|
295
|
+
|
296
|
+
Oj has the top priority in `multi_json` library (a wrapper for different
|
297
|
+
JSON parsers which selects the fastest parser installed on the system and uses it)
|
298
|
+
so when `Oj` is present in your bundle it will also be used by MultiJSON.
|
299
|
+
|
300
|
+
## Roadmap
|
301
|
+
|
302
|
+
* CLI-interface with automatic authorization
|
303
|
+
|
304
|
+
## Development
|
305
|
+
|
306
|
+
If you want to participate in the vkontakte_api development fork
|
307
|
+
the repository, create changes on separate branch, cover them with specs
|
308
|
+
and create a pull request.
|
309
|
+
|
310
|
+
`vkontakte_api` is tested under MRI `1.9.3`, `2.0.0` and `2.1.2`.
|
311
|
+
If it is not working properly in any of these versions it should be considered
|
312
|
+
a bug and reported to [issues on Github](https://github.com/7even/vkontakte_api/issues).
|
data/lib/vkontakte_api.rb
CHANGED
data/lib/vkontakte_api/api.rb
CHANGED
@@ -28,14 +28,14 @@ module VkontakteApi
|
|
28
28
|
token = options.delete(:token)
|
29
29
|
|
30
30
|
Faraday.new(url, VkontakteApi.faraday_options) do |builder|
|
31
|
-
builder.request :oauth2, token unless token.nil?
|
31
|
+
builder.request :oauth2, token, token_type: 'param' unless token.nil?
|
32
32
|
builder.request :multipart
|
33
33
|
builder.request :url_encoded
|
34
34
|
builder.request :retry, VkontakteApi.max_retries
|
35
35
|
|
36
36
|
builder.response :vk_logger
|
37
37
|
builder.response :mashify
|
38
|
-
builder.response :
|
38
|
+
builder.response :multi_json, preserve_raw: true
|
39
39
|
|
40
40
|
builder.adapter VkontakteApi.adapter
|
41
41
|
end
|
data/lib/vkontakte_api/client.rb
CHANGED
@@ -18,10 +18,13 @@ module VkontakteApi
|
|
18
18
|
messages: 4096,
|
19
19
|
wall: 8192,
|
20
20
|
ads: 32768,
|
21
|
+
offline: 65536,
|
21
22
|
docs: 131072,
|
22
23
|
groups: 262144,
|
23
24
|
notifications: 524288,
|
24
|
-
stats: 1048576
|
25
|
+
stats: 1048576,
|
26
|
+
email: 4194304,
|
27
|
+
market: 134217728
|
25
28
|
}
|
26
29
|
|
27
30
|
# An access token needed by authorized requests.
|
data/spec/integration_spec.rb
CHANGED
@@ -21,11 +21,6 @@ describe "Integration" do
|
|
21
21
|
expect(user.last_name).not_to be_empty
|
22
22
|
expect(user.first_name).not_to be_empty
|
23
23
|
end
|
24
|
-
|
25
|
-
it "search newsfeed" do
|
26
|
-
news = vk.newsfeed.search(q: 'vk', count: 1)
|
27
|
-
expect(news).to be_a(Enumerable)
|
28
|
-
end
|
29
24
|
end
|
30
25
|
|
31
26
|
if MechanizedAuthorization.on?
|
data/spec/spec_helper.rb
CHANGED
@@ -2,12 +2,12 @@ require 'mechanize'
|
|
2
2
|
|
3
3
|
module MechanizedAuthorization
|
4
4
|
class << self
|
5
|
-
def client
|
5
|
+
def client(scope: [:friends, :groups])
|
6
6
|
@client ||= begin
|
7
7
|
agent = Mechanize.new
|
8
8
|
|
9
9
|
configure
|
10
|
-
agent.get VkontakteApi.authorization_url(scope:
|
10
|
+
agent.get VkontakteApi.authorization_url(scope: scope, type: :client)
|
11
11
|
|
12
12
|
agent.page.form_with(action: /login.vk.com/) do |form|
|
13
13
|
form.email = settings.email
|
@@ -6,10 +6,10 @@ describe VkontakteApi::API do
|
|
6
6
|
|
7
7
|
@connection = Faraday.new do |builder|
|
8
8
|
builder.response :mashify
|
9
|
-
builder.response :
|
9
|
+
builder.response :multi_json, preserve_raw: true
|
10
10
|
builder.adapter :test do |stub|
|
11
11
|
stub.post('/apiMethod') do
|
12
|
-
[200, {},
|
12
|
+
[200, {}, MultiJson.dump(@result)]
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
@@ -87,7 +87,7 @@ describe VkontakteApi::API do
|
|
87
87
|
handler = connection.builder.handlers.first
|
88
88
|
|
89
89
|
expect(handler.name).to eq('FaradayMiddleware::OAuth2')
|
90
|
-
expect(handler.instance_variable_get(:@args)).to eq([token])
|
90
|
+
expect(handler.instance_variable_get(:@args)).to eq([token, token_type: 'param'])
|
91
91
|
end
|
92
92
|
end
|
93
93
|
end
|
@@ -9,15 +9,15 @@ describe VkontakteApi::Logger do
|
|
9
9
|
VkontakteApi.log_errors = false
|
10
10
|
end
|
11
11
|
|
12
|
-
let(:success_response) {
|
13
|
-
let(:fail_response) {
|
12
|
+
let(:success_response) { MultiJson.dump('a' => 1, 'b' => 2) }
|
13
|
+
let(:fail_response) { MultiJson.dump('error' => 404) }
|
14
14
|
|
15
15
|
let(:connection) do
|
16
16
|
Faraday.new(url: 'http://example.com') do |builder|
|
17
17
|
builder.request :url_encoded
|
18
18
|
builder.response :vk_logger
|
19
19
|
builder.response :mashify
|
20
|
-
builder.response :
|
20
|
+
builder.response :multi_json, preserve_raw: true
|
21
21
|
|
22
22
|
builder.adapter :test do |stub|
|
23
23
|
stub.get('/success') do
|
data/vkontakte_api.gemspec
CHANGED
@@ -18,23 +18,30 @@ Gem::Specification.new do |s|
|
|
18
18
|
|
19
19
|
s.required_ruby_version = '>= 1.9.2'
|
20
20
|
|
21
|
-
s.add_runtime_dependency 'faraday',
|
22
|
-
s.add_runtime_dependency 'faraday_middleware',
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
s.add_runtime_dependency 'faraday', '>= 0.9.0'
|
22
|
+
s.add_runtime_dependency 'faraday_middleware', '>= 0.9.1'
|
23
|
+
unless defined?(JRUBY_VERSION)
|
24
|
+
s.add_runtime_dependency 'oj', '>= 2.12'
|
25
|
+
end
|
26
|
+
s.add_runtime_dependency 'oauth2', '>= 0.8'
|
27
|
+
s.add_runtime_dependency 'hashie', '>= 2.0'
|
28
|
+
s.add_runtime_dependency 'multi_json', '>= 1.3'
|
29
|
+
s.add_runtime_dependency 'faraday_middleware-multi_json', '~> 0.0.6'
|
26
30
|
|
27
31
|
s.add_development_dependency 'rake'
|
28
32
|
s.add_development_dependency 'rspec', '~> 3.0'
|
29
33
|
s.add_development_dependency 'mechanize'
|
30
34
|
s.add_development_dependency 'guard-rspec'
|
31
35
|
s.add_development_dependency 'rb-fsevent', '~> 0.9.1'
|
36
|
+
s.add_development_dependency 'terminal-notifier'
|
32
37
|
s.add_development_dependency 'terminal-notifier-guard'
|
33
38
|
|
34
39
|
s.add_development_dependency 'pry'
|
35
40
|
s.add_development_dependency 'awesome_print'
|
36
41
|
|
37
|
-
|
38
|
-
|
39
|
-
|
42
|
+
unless defined?(JRUBY_VERSION)
|
43
|
+
s.add_development_dependency 'yard'
|
44
|
+
s.add_development_dependency 'redcarpet'
|
45
|
+
s.add_development_dependency 'guard-yard'
|
46
|
+
end
|
40
47
|
end
|
metadata
CHANGED
@@ -1,57 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vkontakte_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.4.
|
4
|
+
version: 1.4.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vsevolod Romashov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-06-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 0.9.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 0.9.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: faraday_middleware
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: 0.9.1
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 0.9.1
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: oj
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '2.12'
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '2.12'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: oauth2
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,6 +80,34 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '2.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: multi_json
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.3'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.3'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: faraday_middleware-multi_json
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 0.0.6
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.0.6
|
83
111
|
- !ruby/object:Gem::Dependency
|
84
112
|
name: rake
|
85
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -150,6 +178,20 @@ dependencies:
|
|
150
178
|
- - "~>"
|
151
179
|
- !ruby/object:Gem::Version
|
152
180
|
version: 0.9.1
|
181
|
+
- !ruby/object:Gem::Dependency
|
182
|
+
name: terminal-notifier
|
183
|
+
requirement: !ruby/object:Gem::Requirement
|
184
|
+
requirements:
|
185
|
+
- - ">="
|
186
|
+
- !ruby/object:Gem::Version
|
187
|
+
version: '0'
|
188
|
+
type: :development
|
189
|
+
prerelease: false
|
190
|
+
version_requirements: !ruby/object:Gem::Requirement
|
191
|
+
requirements:
|
192
|
+
- - ">="
|
193
|
+
- !ruby/object:Gem::Version
|
194
|
+
version: '0'
|
153
195
|
- !ruby/object:Gem::Dependency
|
154
196
|
name: terminal-notifier-guard
|
155
197
|
requirement: !ruby/object:Gem::Requirement
|
@@ -252,6 +294,7 @@ files:
|
|
252
294
|
- Guardfile
|
253
295
|
- MIT-LICENSE
|
254
296
|
- README.md
|
297
|
+
- README_en.md
|
255
298
|
- Rakefile
|
256
299
|
- bin/rspec
|
257
300
|
- lib/generators/vkontakte_api/install/USAGE
|
@@ -274,6 +317,7 @@ files:
|
|
274
317
|
- lib/vkontakte_api/utils.rb
|
275
318
|
- lib/vkontakte_api/version.rb
|
276
319
|
- spec/integration_spec.rb
|
320
|
+
- spec/multi_json_spec.rb
|
277
321
|
- spec/spec_helper.rb
|
278
322
|
- spec/support/mechanized_authorization.rb
|
279
323
|
- spec/vkontakte_api/api_spec.rb
|
@@ -311,12 +355,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
311
355
|
version: '0'
|
312
356
|
requirements: []
|
313
357
|
rubyforge_project:
|
314
|
-
rubygems_version: 2.
|
358
|
+
rubygems_version: 2.5.2
|
315
359
|
signing_key:
|
316
360
|
specification_version: 4
|
317
361
|
summary: Ruby wrapper for VKontakte API
|
318
362
|
test_files:
|
319
363
|
- spec/integration_spec.rb
|
364
|
+
- spec/multi_json_spec.rb
|
320
365
|
- spec/spec_helper.rb
|
321
366
|
- spec/support/mechanized_authorization.rb
|
322
367
|
- spec/vkontakte_api/api_spec.rb
|
@@ -333,4 +378,3 @@ test_files:
|
|
333
378
|
- spec/vkontakte_api/uploading_spec.rb
|
334
379
|
- spec/vkontakte_api/utils_spec.rb
|
335
380
|
- spec/vkontakte_api_spec.rb
|
336
|
-
has_rdoc:
|