metrika 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/metrika/helpers/authorization.rb +11 -7
- data/lib/metrika/helpers/request.rb +3 -15
- data/metrika.gemspec +4 -2
- data/spec/cases/metrika/authorization_spec.rb +29 -7
- data/spec/cases/metrika/counters_spec.rb +24 -0
- data/spec/fixtures/cassettes/counters.yml +82 -0
- data/spec/helper.rb +9 -3
- metadata +5 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
@@ -1,30 +1,34 @@
|
|
1
1
|
module Metrika
|
2
2
|
module Helpers
|
3
3
|
module Authorization
|
4
|
-
attr_reader :token
|
5
|
-
|
6
4
|
DEFAULT_OAUTH_OPTIONS = {
|
7
5
|
:site => 'http://api-metrika.yandex.ru',
|
8
6
|
:authorize_url => 'https://oauth.yandex.ru/authorize',
|
9
7
|
:token_url => 'https://oauth.yandex.ru/token'
|
10
8
|
}
|
11
9
|
|
12
|
-
def authorize_token(
|
13
|
-
@token = (self.client.auth_code.get_token(
|
10
|
+
def authorize_token(auth_code)
|
11
|
+
@token = (self.client.auth_code.get_token(auth_code) rescue nil)
|
14
12
|
end
|
15
13
|
|
16
14
|
def authorization_url
|
17
15
|
self.client.auth_code.authorize_url
|
18
16
|
end
|
19
17
|
|
20
|
-
def restore_token(
|
21
|
-
@token = OAuth2::AccessToken.new(self.client,
|
18
|
+
def restore_token(access_token)
|
19
|
+
@token = OAuth2::AccessToken.new(self.client, access_token)
|
22
20
|
end
|
23
21
|
|
24
22
|
protected
|
25
23
|
|
26
24
|
def client
|
27
|
-
@client ||= OAuth2::Client.new(@application_id, @application_password, DEFAULT_OAUTH_OPTIONS)
|
25
|
+
@client ||= OAuth2::Client.new(@application_id, @application_password, DEFAULT_OAUTH_OPTIONS.dup)
|
26
|
+
end
|
27
|
+
|
28
|
+
def token
|
29
|
+
raise Metrika::Errors::UnauthorizedError.new("Access token is not initialized") if @token.nil?
|
30
|
+
|
31
|
+
@token
|
28
32
|
end
|
29
33
|
end
|
30
34
|
end
|
@@ -10,22 +10,14 @@ module Metrika
|
|
10
10
|
|
11
11
|
protected
|
12
12
|
|
13
|
-
def get(path, params = {}, options = {})
|
14
|
-
# raise Metrika::Errors::NoTokenError unless self.token
|
15
|
-
|
16
|
-
|
13
|
+
def get(path, params = {}, options = {})
|
17
14
|
response = self.token.get(path, DEFAULT_OPTIONS.merge(:params => params).merge(options))
|
18
|
-
# rescue OAuth2::Error => e
|
19
|
-
|
20
|
-
# end
|
21
15
|
|
22
16
|
# self.raise_errors(response)
|
23
17
|
Yajl::Parser.parse(response.body)
|
24
18
|
end
|
25
19
|
|
26
20
|
def post(path, body = {}, options = {})
|
27
|
-
# raise Metrika::Errors::NoTokenError unless self.token
|
28
|
-
|
29
21
|
encoded_body = Yajl::Encoder.encode(body)
|
30
22
|
response = self.token.post(path, DEFAULT_OPTIONS.merge(:body => encoded_body).merge(options))
|
31
23
|
|
@@ -34,9 +26,7 @@ module Metrika
|
|
34
26
|
end
|
35
27
|
|
36
28
|
|
37
|
-
def put(path, body = {}, options = {})
|
38
|
-
# raise Metrika::Errors::NoTokenError unless self.token
|
39
|
-
|
29
|
+
def put(path, body = {}, options = {})
|
40
30
|
encoded_body = Yajl::Encoder.encode(body)
|
41
31
|
response = self.token.put(path, DEFAULT_OPTIONS.merge(:body => encoded_body).merge(options))
|
42
32
|
|
@@ -44,9 +34,7 @@ module Metrika
|
|
44
34
|
Yajl::Parser.parse(response.body)
|
45
35
|
end
|
46
36
|
|
47
|
-
def delete(path, options={})
|
48
|
-
# raise Metrika::Errors::NoTokenError unless self.token
|
49
|
-
|
37
|
+
def delete(path, options={})
|
50
38
|
response = self.token.delete(path, DEFAULT_OPTIONS.merge(options))
|
51
39
|
|
52
40
|
# self.raise_errors(response)
|
data/metrika.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "metrika"
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Igor Alexandrov"]
|
12
|
-
s.date = "2012-10-
|
12
|
+
s.date = "2012-10-26"
|
13
13
|
s.email = "igor.alexandrov@gmail.com"
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"LICENSE",
|
@@ -36,7 +36,9 @@ Gem::Specification.new do |s|
|
|
36
36
|
"lib/metrika/helpers/request.rb",
|
37
37
|
"metrika.gemspec",
|
38
38
|
"spec/cases/metrika/authorization_spec.rb",
|
39
|
+
"spec/cases/metrika/counters_spec.rb",
|
39
40
|
"spec/cases/metrika_spec.rb",
|
41
|
+
"spec/fixtures/cassettes/counters.yml",
|
40
42
|
"spec/helper.rb"
|
41
43
|
]
|
42
44
|
s.homepage = "http://github.com/igor-alexandrov/metrika"
|
@@ -9,21 +9,43 @@ describe Metrika do
|
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
before(:each) do
|
13
|
+
@client = Metrika::Client.new
|
14
|
+
end
|
15
|
+
|
16
|
+
context '-- unauthorized' do
|
17
|
+
it 'should raise an error on GET' do
|
18
|
+
lambda {
|
19
|
+
@client.send(:get, 'url')
|
20
|
+
}.should raise_error(Metrika::Errors::UnauthorizedError)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should raise an error on POST' do
|
24
|
+
lambda {
|
25
|
+
@client.send(:post, 'url')
|
26
|
+
}.should raise_error(Metrika::Errors::UnauthorizedError)
|
15
27
|
end
|
16
28
|
|
29
|
+
it 'should raise an error on PUT' do
|
30
|
+
lambda {
|
31
|
+
@client.send(:put, 'url')
|
32
|
+
}.should raise_error(Metrika::Errors::UnauthorizedError)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should raise an error on DELETE' do
|
36
|
+
lambda {
|
37
|
+
@client.send(:delete, 'url')
|
38
|
+
}.should raise_error(Metrika::Errors::UnauthorizedError)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context '#authorization_url' do
|
17
43
|
it 'should look like authorization url' do
|
18
44
|
@client.authorization_url.should match /\Ahttps:\/\/oauth.yandex.ru\/authorize.+#{APPLICATION_ID}\z/
|
19
45
|
end
|
20
46
|
end
|
21
47
|
|
22
48
|
context '#restore_token' do
|
23
|
-
before(:each) do
|
24
|
-
@client = Metrika::Client.new
|
25
|
-
end
|
26
|
-
|
27
49
|
it 'should not raise an error' do
|
28
50
|
lambda {
|
29
51
|
@client.restore_token(ACCESS_TOKEN)
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe Metrika do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
Metrika.configure do |config|
|
7
|
+
config.application_id = APPLICATION_ID
|
8
|
+
config.application_password = APPLICATION_PASSWORD
|
9
|
+
end
|
10
|
+
|
11
|
+
@client = Metrika::Client.new
|
12
|
+
@client.restore_token(ACCESS_TOKEN)
|
13
|
+
end
|
14
|
+
|
15
|
+
context '#get_counters' do
|
16
|
+
it 'should return array of counters' do
|
17
|
+
VCR.use_cassette('counters') do
|
18
|
+
counters = @client.get_counters
|
19
|
+
counters.should be_instance_of(Array)
|
20
|
+
counters.size.should > 0
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: http://api-metrika.yandex.ru/counters
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: ''
|
9
|
+
headers:
|
10
|
+
Accept:
|
11
|
+
- application/x-yametrika+json
|
12
|
+
Content-Type:
|
13
|
+
- application/x-yametrika+json
|
14
|
+
Authorization:
|
15
|
+
- Bearer 926b254ca2f24869999a3d404a01f616
|
16
|
+
Accept-Encoding:
|
17
|
+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
18
|
+
User-Agent:
|
19
|
+
- Ruby
|
20
|
+
response:
|
21
|
+
status:
|
22
|
+
code: 200
|
23
|
+
message: !binary |-
|
24
|
+
T0s=
|
25
|
+
headers:
|
26
|
+
!binary "UHJhZ21h":
|
27
|
+
- !binary |-
|
28
|
+
bm8tY2FjaGU=
|
29
|
+
!binary "Q2FjaGUtQ29udHJvbA==":
|
30
|
+
- !binary |-
|
31
|
+
bm8tY2FjaGUsIG5vLXN0b3JlLCBtYXgtYWdlPTAsIG11c3QtcmV2YWxpZGF0
|
32
|
+
ZQ==
|
33
|
+
!binary "RXhwaXJlcw==":
|
34
|
+
- !binary |-
|
35
|
+
VGh1LCAwMSBKYW4gMTk3MCAwMDowMDowMCBHTVQ=
|
36
|
+
!binary "RGF0ZQ==":
|
37
|
+
- !binary |-
|
38
|
+
RnJpLCAyNiBPY3QgMjAxMiAwODoxOToxMyBHTVQ=
|
39
|
+
!binary "U2VydmVy":
|
40
|
+
- !binary |-
|
41
|
+
QXBhY2hlLzIuMg==
|
42
|
+
!binary "Q29udGVudC1UeXBl":
|
43
|
+
- !binary |-
|
44
|
+
YXBwbGljYXRpb24veC15YW1ldHJpa2EranNvbg==
|
45
|
+
!binary "VHJhbnNmZXItRW5jb2Rpbmc=":
|
46
|
+
- !binary |-
|
47
|
+
Y2h1bmtlZA==
|
48
|
+
body:
|
49
|
+
encoding: ASCII-8BIT
|
50
|
+
string: !binary |-
|
51
|
+
eyJjb3VudGVycyI6W3siY29kZV9zdGF0dXMiOiJDU19PSyIsIm5hbWUiOm51
|
52
|
+
bGwsInBlcm1pc3Npb24iOiJvd24iLCJzaXRlIjoid3d3LnJlcXVlc3RiaWxs
|
53
|
+
aW5nLmNvbSIsInR5cGUiOiJzaW1wbGUiLCJpZCI6MjM3OTkxLCJvd25lcl9s
|
54
|
+
b2dpbiI6Imlnb3ItYWxleGFuZHJvdiJ9LHsiY29kZV9zdG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QuNC1IiwicGVybWlzc2lvbiI6InZpZXciLCJzaXRlIjoiZWR1Y2F0aW9u
|
71
|
+
LnN1cGVyaW5mb3JtLnJ1IiwidHlwZSI6InNpbXBsZSIsImlkIjoxNjE2Mzc5
|
72
|
+
LCJvd25lcl9sb2dpbiI6ImluZm9saW8tc2VvIn0seyJjb2RlX3N0YXR1cyI6
|
73
|
+
IkNTX0VSUl9DT05ORUNUIiwibmFtZSI6InRlc3QiLCJwZXJtaXNzaW9uIjoi
|
74
|
+
b3duIiwic2l0ZSI6Ind3dy50ZXN0LnJ1IiwidHlwZSI6InNpbXBsZSIsImlk
|
75
|
+
IjoxNzI5MTcxMywib3duZXJfbG9naW4iOiJpZ29yLWFsZXhhbmRyb3YifSx7
|
76
|
+
ImNvZGVfc3RhdHVzIjoiQ1NfT0siLCJuYW1lIjoid3d3LnNkZWxraS5ydSIs
|
77
|
+
InBlcm1pc3Npb24iOiJvd24iLCJzaXRlIjoid3d3LnNkZWxraS5ydSIsInR5
|
78
|
+
cGUiOiJzaW1wbGUiLCJpZCI6MTUwNjgxMSwib3duZXJfbG9naW4iOiJpZ29y
|
79
|
+
LWFsZXhhbmRyb3YifV19
|
80
|
+
http_version:
|
81
|
+
recorded_at: Fri, 26 Oct 2012 08:19:15 GMT
|
82
|
+
recorded_with: VCR 2.2.5
|
data/spec/helper.rb
CHANGED
@@ -10,15 +10,21 @@ VCR.configure do |c|
|
|
10
10
|
c.cassette_library_dir = 'spec/fixtures/cassettes'
|
11
11
|
c.hook_into(:webmock)
|
12
12
|
c.ignore_localhost = true
|
13
|
-
c.default_cassette_options = {
|
13
|
+
c.default_cassette_options = {:record => :once}
|
14
14
|
end
|
15
15
|
|
16
16
|
RSpec.configure do |c|
|
17
17
|
c.extend VCR::RSpec::Macros
|
18
18
|
end
|
19
19
|
|
20
|
-
# You think that I am bit crazy to save all these here? No, it is just a development authorization from Yandex
|
21
20
|
APPLICATION_ID = '663576cbd55948a4ae45424fb508ef97'
|
22
21
|
APPLICATION_PASSWORD = 'fc2f76dc877e41a4a6cbe78d73faff85'
|
23
22
|
|
24
|
-
ACCESS_TOKEN = '926b254ca2f24869999a3d404a01f616'
|
23
|
+
# ACCESS_TOKEN = '926b254ca2f24869999a3d404a01f616'
|
24
|
+
token_file = File.expand_path('../.access_token', __FILE__)
|
25
|
+
if File.exists?(token_file)
|
26
|
+
ACCESS_TOKEN = File.read(token_file).strip
|
27
|
+
else
|
28
|
+
ACCESS_TOKEN = 'FAKETOKEN'
|
29
|
+
puts "You are using a fake access token. You can only use API responds recorded by VCR"
|
30
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: metrika
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: oauth2
|
@@ -135,7 +135,9 @@ files:
|
|
135
135
|
- lib/metrika/helpers/request.rb
|
136
136
|
- metrika.gemspec
|
137
137
|
- spec/cases/metrika/authorization_spec.rb
|
138
|
+
- spec/cases/metrika/counters_spec.rb
|
138
139
|
- spec/cases/metrika_spec.rb
|
140
|
+
- spec/fixtures/cassettes/counters.yml
|
139
141
|
- spec/helper.rb
|
140
142
|
homepage: http://github.com/igor-alexandrov/metrika
|
141
143
|
licenses:
|
@@ -152,7 +154,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
152
154
|
version: '0'
|
153
155
|
segments:
|
154
156
|
- 0
|
155
|
-
hash:
|
157
|
+
hash: -3365005199933718832
|
156
158
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
157
159
|
none: false
|
158
160
|
requirements:
|