odnoklassniki 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +3 -0
- data/.travis.yml +12 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +106 -0
- data/Rakefile +9 -0
- data/lib/odnoklassniki.rb +27 -0
- data/lib/odnoklassniki/client.rb +75 -0
- data/lib/odnoklassniki/config.rb +31 -0
- data/lib/odnoklassniki/connection.rb +29 -0
- data/lib/odnoklassniki/error.rb +182 -0
- data/lib/odnoklassniki/request.rb +88 -0
- data/lib/odnoklassniki/version.rb +3 -0
- data/odnoklassniki.gemspec +32 -0
- data/test/lib/odnoklassniki/client_test.rb +45 -0
- data/test/lib/odnoklassniki/config_test.rb +34 -0
- data/test/lib/odnoklassniki/error_test.rb +13 -0
- data/test/lib/odnoklassniki/request_test.rb +33 -0
- data/test/lib/odnoklassniki_test.rb +29 -0
- data/test/test_helper.rb +16 -0
- data/test/vcr_cassettes/client_wrong_credentials_getCurrentUser.yml +48 -0
- data/test/vcr_cassettes/client_wrong_credentials_post.yml +50 -0
- data/test/vcr_cassettes/client_wrong_credentials_token.yml +57 -0
- data/test/vcr_cassettes/wrong_credentials_getCurrentUser.yml +47 -0
- data/test/vcr_cassettes/wrong_credentials_token.yml +53 -0
- metadata +255 -0
@@ -0,0 +1,88 @@
|
|
1
|
+
require_relative 'connection'
|
2
|
+
require 'multi_json'
|
3
|
+
|
4
|
+
module Odnoklassniki
|
5
|
+
class Request
|
6
|
+
|
7
|
+
include Odnoklassniki::Connection
|
8
|
+
|
9
|
+
def initialize(credentials)
|
10
|
+
@access_token = credentials[:access_token]
|
11
|
+
@client_secret = credentials[:client_secret]
|
12
|
+
@application_key = credentials[:application_key]
|
13
|
+
end
|
14
|
+
|
15
|
+
# Performs a get request
|
16
|
+
def get(path, params={})
|
17
|
+
respond perform_request(:get, path, params)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Performs post request
|
21
|
+
def post(path, params={})
|
22
|
+
respond perform_request(:post, path, params)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def perform_request(method, path, params)
|
28
|
+
signed_params = signed params
|
29
|
+
|
30
|
+
connection.send(method) do |req|
|
31
|
+
req.url path
|
32
|
+
if method == :get
|
33
|
+
req.params = signed_params
|
34
|
+
else
|
35
|
+
req.body = signed_params unless params.empty?
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def respond(response)
|
41
|
+
parsed_body = \
|
42
|
+
begin
|
43
|
+
MultiJson.load(response.body)
|
44
|
+
rescue MultiJson::DecodeError
|
45
|
+
#Есть случаи отдачи кривого JSON от одноклассников
|
46
|
+
gsubed = response.body.
|
47
|
+
gsub(/[^"]}/, '"}').
|
48
|
+
gsub(/([^"}]),"([^}])/, '\1","\2')
|
49
|
+
MultiJson.load(gsubed)
|
50
|
+
end
|
51
|
+
|
52
|
+
fail_or_return_body(response.status, parsed_body)
|
53
|
+
rescue MultiJson::DecodeError => e
|
54
|
+
raise Odnoklassniki::Error::ClientError.new(e.message)
|
55
|
+
end
|
56
|
+
|
57
|
+
def fail_or_return_body(code, body)
|
58
|
+
error = error(code, body)
|
59
|
+
fail(error) if error
|
60
|
+
body
|
61
|
+
end
|
62
|
+
|
63
|
+
def error(code, body)
|
64
|
+
if ![200, 201].include?(code)
|
65
|
+
if klass = Odnoklassniki::Error::ERRORS[code]
|
66
|
+
klass.from_response(body)
|
67
|
+
end
|
68
|
+
else
|
69
|
+
if body.is_a?(Hash) && body['error_code']
|
70
|
+
Odnoklassniki::Error::ClientError.from_response(body)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def signed(params)
|
76
|
+
params = params.merge(application_key: @application_key)
|
77
|
+
params.merge(sig: signature(params), access_token: @access_token)
|
78
|
+
end
|
79
|
+
|
80
|
+
def signature(params)
|
81
|
+
sorted_concatenated_params =
|
82
|
+
Hash[params.sort].map { |k, v| "#{k}=#{v}" }.join
|
83
|
+
secret_part = Digest::MD5.hexdigest("#{@access_token}#{@client_secret}")
|
84
|
+
Digest::MD5.hexdigest("#{sorted_concatenated_params}#{secret_part}")
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'odnoklassniki/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'odnoklassniki'
|
7
|
+
s.version = Odnoklassniki::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ['gazay']
|
10
|
+
s.licenses = ['MIT']
|
11
|
+
s.email = ['alex.gaziev@gmail.com']
|
12
|
+
s.homepage = 'https://github.com/gazay/odnoklassniki'
|
13
|
+
s.summary = %q{Ruby wrapper for Odnoklassniki API}
|
14
|
+
s.description = %q{Ruby wrapper for REST Odnoklassniki API calls. GET and POST calls.}
|
15
|
+
|
16
|
+
s.rubyforge_project = 'odnoklassniki'
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.require_paths = ['lib']
|
20
|
+
s.add_dependency "faraday", '~> 0.9', '>= 0.9.0'
|
21
|
+
s.add_dependency "faraday_middleware", '~> 0.9', '>= 0.9.0'
|
22
|
+
s.add_dependency "multi_json", '~> 1.10', '>= 1.10.0'
|
23
|
+
s.add_development_dependency 'pry', '0.10.1'
|
24
|
+
s.add_development_dependency 'byebug', '3.5.1'
|
25
|
+
s.add_development_dependency 'pry-byebug', '3.0.0'
|
26
|
+
s.add_development_dependency 'minitest', '5.5.1'
|
27
|
+
s.add_development_dependency 'minitest-reporters', '1.0.10'
|
28
|
+
s.add_development_dependency 'simplecov', '0.9.1'
|
29
|
+
s.add_development_dependency 'rake', '10.1.0'
|
30
|
+
s.add_development_dependency 'vcr', '2.9.3'
|
31
|
+
s.add_development_dependency 'webmock', '1.20.4'
|
32
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require_relative '../../test_helper'
|
2
|
+
|
3
|
+
class TestOdnoklassnikiClient < Minitest::Test
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@client = Odnoklassniki::Client.new({
|
7
|
+
access_token: 'token',
|
8
|
+
client_id: 'client_id',
|
9
|
+
client_secret: 'client_secret',
|
10
|
+
application_key: 'application_key'
|
11
|
+
})
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_get_with_wrong_credentials
|
15
|
+
VCR.use_cassette('client_wrong_credentials_getCurrentUser') do
|
16
|
+
error = assert_raises Odnoklassniki::Error::ClientError do
|
17
|
+
@client.instance_variable_set(:@refreshed, true)
|
18
|
+
@client.get('users.getCurrentUser')
|
19
|
+
end
|
20
|
+
assert_equal 'PARAM_API_KEY : Application not exist', error.message
|
21
|
+
assert_equal 101, error.code
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_post_with_wrong_credentials
|
26
|
+
VCR.use_cassette('client_wrong_credentials_post') do
|
27
|
+
error = assert_raises Odnoklassniki::Error::ClientError do
|
28
|
+
@client.instance_variable_set(:@refreshed, true)
|
29
|
+
@client.post('mediatopic.post')
|
30
|
+
end
|
31
|
+
assert_match /No application key/, error.message
|
32
|
+
assert_equal 101, error.code
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_refresh_token_with_wrong_credentials
|
37
|
+
VCR.use_cassette('client_wrong_credentials_token') do
|
38
|
+
error = assert_raises Odnoklassniki::Error::ClientError do
|
39
|
+
@client.refresh_token!
|
40
|
+
end
|
41
|
+
assert_equal nil, error.code
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require_relative '../../test_helper'
|
2
|
+
|
3
|
+
class TestOdnoklassnikiConfig < Minitest::Test
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@options = {
|
7
|
+
access_token: '1',
|
8
|
+
client_id: '2',
|
9
|
+
client_secret: '3',
|
10
|
+
application_key: '4'
|
11
|
+
}
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_configure
|
15
|
+
config = Odnoklassniki::Config.configure do |c|
|
16
|
+
@options.each do |k, v|
|
17
|
+
c.send("#{k}=", v)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
assert_equal config.options, @options
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_new
|
24
|
+
config = Odnoklassniki::Config.new(@options)
|
25
|
+
assert_equal config.options, @options
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_options
|
29
|
+
opts = @options.merge({some_unused_key: 123})
|
30
|
+
config = Odnoklassniki::Config.new(opts)
|
31
|
+
assert_equal config.options, @options
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require_relative '../../test_helper'
|
2
|
+
|
3
|
+
class TestOdnoklassnikiError < Minitest::Test
|
4
|
+
|
5
|
+
def test_from_response
|
6
|
+
error = Odnoklassniki::Error.
|
7
|
+
from_response({ 'error_msg' => 'message', 'error_code' => 123 })
|
8
|
+
assert_instance_of Odnoklassniki::Error, error
|
9
|
+
assert_equal error.code, 123
|
10
|
+
assert_equal error.message, 'message'
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require_relative '../../test_helper'
|
2
|
+
|
3
|
+
class TestOdnoklassnikiRequest < Minitest::Test
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@request = Odnoklassniki::Request.new({
|
7
|
+
access_token: 'token',
|
8
|
+
client_secret: 'client_secret',
|
9
|
+
application_key: 'application_key'
|
10
|
+
})
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_get_with_wrong_credentials
|
14
|
+
VCR.use_cassette('wrong_credentials_getCurrentUser') do
|
15
|
+
error = assert_raises Odnoklassniki::Error::ClientError do
|
16
|
+
@request.get('/api/users/getCurrentUser')
|
17
|
+
end
|
18
|
+
assert_equal 'PARAM_API_KEY : Application not exist', error.message
|
19
|
+
assert_equal 101, error.code
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_post_with_wrong_credentials
|
24
|
+
VCR.use_cassette('wrong_credentials_token') do
|
25
|
+
error = assert_raises Odnoklassniki::Error::ClientError do
|
26
|
+
@request.post('/oauth/token.do')
|
27
|
+
end
|
28
|
+
assert_match /Provide OAUTH request parameters!/, error.message
|
29
|
+
assert_equal error.code, nil
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative '../test_helper'
|
2
|
+
|
3
|
+
class TestOdnoklassniki < Minitest::Test
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@options = {
|
7
|
+
access_token: '1',
|
8
|
+
client_id: '2',
|
9
|
+
client_secret: '3',
|
10
|
+
application_key: '4'
|
11
|
+
}
|
12
|
+
Odnoklassniki.configure do |c|
|
13
|
+
@options.each { |k, v| c.send("#{k}=", v) }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_new
|
18
|
+
assert_instance_of Odnoklassniki::Client, Odnoklassniki.new
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_configure
|
22
|
+
assert_equal Odnoklassniki::Client.new.instance_variable_get(:@access_token), '1'
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_options
|
26
|
+
assert_equal Odnoklassniki.options, @options
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
if ENV['COV'] == '1'
|
2
|
+
require 'simplecov'
|
3
|
+
SimpleCov.start
|
4
|
+
end
|
5
|
+
|
6
|
+
require 'minitest/autorun'
|
7
|
+
require "minitest/reporters"
|
8
|
+
Minitest::Reporters.use!
|
9
|
+
|
10
|
+
require 'vcr'
|
11
|
+
VCR.configure do |config|
|
12
|
+
config.cassette_library_dir = "test/vcr_cassettes"
|
13
|
+
config.hook_into :webmock # or :fakeweb
|
14
|
+
end
|
15
|
+
|
16
|
+
require_relative '../lib/odnoklassniki'
|
@@ -0,0 +1,48 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: http://api.odnoklassniki.ru/api/users/getCurrentUser?access_token=token&application_key=application_key&sig=4784959f6620c78221bff139d6cf9309
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: ''
|
9
|
+
headers:
|
10
|
+
Accept:
|
11
|
+
- application/json
|
12
|
+
User-Agent:
|
13
|
+
- odnoklassniki ruby gem/0.0.1
|
14
|
+
Accept-Encoding:
|
15
|
+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
16
|
+
response:
|
17
|
+
status:
|
18
|
+
code: 200
|
19
|
+
message: OK
|
20
|
+
headers:
|
21
|
+
Server:
|
22
|
+
- Apache-Coyote/1.1
|
23
|
+
Invocation-Error:
|
24
|
+
- '101'
|
25
|
+
Pragma:
|
26
|
+
- no-cache
|
27
|
+
Expires:
|
28
|
+
- Thu, 01 Jan 1970 00:00:00 GMT
|
29
|
+
Cache-Control:
|
30
|
+
- no-cache
|
31
|
+
- no-store
|
32
|
+
Content-Type:
|
33
|
+
- application/json;charset=utf-8
|
34
|
+
Content-Language:
|
35
|
+
- en-US
|
36
|
+
Content-Length:
|
37
|
+
- '88'
|
38
|
+
Date:
|
39
|
+
- Wed, 04 Feb 2015 14:30:21 GMT
|
40
|
+
Connection:
|
41
|
+
- close
|
42
|
+
body:
|
43
|
+
encoding: UTF-8
|
44
|
+
string: '{"error_code":101,"error_data":null,"error_msg":"PARAM_API_KEY : Application
|
45
|
+
not exist"}'
|
46
|
+
http_version:
|
47
|
+
recorded_at: Wed, 04 Feb 2015 14:30:21 GMT
|
48
|
+
recorded_with: VCR 2.9.3
|
@@ -0,0 +1,50 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: post
|
5
|
+
uri: http://api.odnoklassniki.ru/api/mediatopic/post
|
6
|
+
body:
|
7
|
+
encoding: UTF-8
|
8
|
+
string: ''
|
9
|
+
headers:
|
10
|
+
Accept:
|
11
|
+
- application/json
|
12
|
+
User-Agent:
|
13
|
+
- odnoklassniki ruby gem/0.0.1
|
14
|
+
Content-Length:
|
15
|
+
- '0'
|
16
|
+
Accept-Encoding:
|
17
|
+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
18
|
+
response:
|
19
|
+
status:
|
20
|
+
code: 200
|
21
|
+
message: OK
|
22
|
+
headers:
|
23
|
+
Server:
|
24
|
+
- Apache-Coyote/1.1
|
25
|
+
Invocation-Error:
|
26
|
+
- '101'
|
27
|
+
Pragma:
|
28
|
+
- no-cache
|
29
|
+
Expires:
|
30
|
+
- Thu, 01 Jan 1970 00:00:00 GMT
|
31
|
+
Cache-Control:
|
32
|
+
- no-cache
|
33
|
+
- no-store
|
34
|
+
Content-Type:
|
35
|
+
- application/json;charset=utf-8
|
36
|
+
Content-Language:
|
37
|
+
- en-US
|
38
|
+
Content-Length:
|
39
|
+
- '85'
|
40
|
+
Date:
|
41
|
+
- Wed, 04 Feb 2015 14:30:21 GMT
|
42
|
+
Connection:
|
43
|
+
- close
|
44
|
+
body:
|
45
|
+
encoding: UTF-8
|
46
|
+
string: '{"error_code":101,"error_data":null,"error_msg":"PARAM_API_KEY : No
|
47
|
+
application key"}'
|
48
|
+
http_version:
|
49
|
+
recorded_at: Wed, 04 Feb 2015 14:30:22 GMT
|
50
|
+
recorded_with: VCR 2.9.3
|
@@ -0,0 +1,57 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: post
|
5
|
+
uri: http://api.odnoklassniki.ru/oauth/token.do
|
6
|
+
body:
|
7
|
+
encoding: UTF-8
|
8
|
+
string: access_token=token&application_key=application_key&client_id=client_id&client_secret=client_secret&grant_type=refresh_token&refresh_token=token&sig=c40601c8f1fdd56139160d7140af3ed0
|
9
|
+
headers:
|
10
|
+
Accept:
|
11
|
+
- application/json
|
12
|
+
User-Agent:
|
13
|
+
- odnoklassniki ruby gem/0.0.1
|
14
|
+
Content-Type:
|
15
|
+
- application/x-www-form-urlencoded
|
16
|
+
Accept-Encoding:
|
17
|
+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
18
|
+
response:
|
19
|
+
status:
|
20
|
+
code: 200
|
21
|
+
message: OK
|
22
|
+
headers:
|
23
|
+
Server:
|
24
|
+
- Apache-Coyote/1.1
|
25
|
+
Set-Cookie:
|
26
|
+
- JSESSIONID=9190C3142FFCE4EF71502920B5597956; Path=/; HttpOnly
|
27
|
+
Content-Type:
|
28
|
+
- text/html;charset=utf-8
|
29
|
+
Content-Language:
|
30
|
+
- en-US
|
31
|
+
Content-Length:
|
32
|
+
- '146'
|
33
|
+
Date:
|
34
|
+
- Wed, 04 Feb 2015 14:30:22 GMT
|
35
|
+
Connection:
|
36
|
+
- close
|
37
|
+
body:
|
38
|
+
encoding: UTF-8
|
39
|
+
string: |2
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
<html>
|
47
|
+
|
48
|
+
<head>
|
49
|
+
</head>
|
50
|
+
<body>
|
51
|
+
|
52
|
+
Error : Failed to convert value of type [java.lang.String] to required type [java.lang.Long]
|
53
|
+
</body>
|
54
|
+
</html>
|
55
|
+
http_version:
|
56
|
+
recorded_at: Wed, 04 Feb 2015 14:30:23 GMT
|
57
|
+
recorded_with: VCR 2.9.3
|