metrica_api 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +20 -0
- data/README.md +49 -0
- data/lib/metrica_api/errors/connection_error.rb +7 -0
- data/lib/metrica_api/errors/request_error.rb +7 -0
- data/lib/metrica_api/request.rb +45 -0
- data/lib/metrica_api/response.rb +26 -0
- data/lib/metrica_api/sections/counters.rb +22 -0
- data/lib/metrica_api/sections/reporting.rb +30 -0
- data/lib/metrica_api/session.rb +20 -0
- data/lib/metrica_api/version.rb +7 -0
- data/lib/metrica_api.rb +52 -0
- data/metrica_api.gemspec +35 -0
- metadata +188 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8c3c643b1ea64446fe23364c2a604315c42b955d
|
4
|
+
data.tar.gz: 5ba637f4d9e0f9a6cb3bfe2a6f0a033a9045e1af
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: db478488dc9de228d540d06bc557068d339a52380ff2e5add8d21eaf56a9fd1b80acaf37d05b52cde0c3ca058f29bc9655df5e267c410c81af055705659e8e0f
|
7
|
+
data.tar.gz: cc951ae6daa065a07fccbe0f47f7bf165e96fa6335f78b00bb5fe1656510123ec7909919223cc473748e3597d9279077a77113244a4939ea37af5c594a86d039
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Ruby client for Yandex Metrica API
|
2
|
+
|
3
|
+
[](https://travis-ci.org/resivalex/metrica_api) [](https://codeclimate.com/github/resivalex/metrica_api/maintainability)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```
|
10
|
+
gem 'metrica_api', '~> 0.0.1'
|
11
|
+
```
|
12
|
+
|
13
|
+
Or install from command line:
|
14
|
+
|
15
|
+
```
|
16
|
+
$ gem install metrica_api
|
17
|
+
```
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
# You need an access token to use API
|
23
|
+
api = MetricaApi.counters(token)
|
24
|
+
api.data(field: 'goals') # => { 'counters' => [...], ... }
|
25
|
+
```
|
26
|
+
|
27
|
+
## Exceptions
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
def read_counters
|
31
|
+
|
32
|
+
MetricaApi.counters(token).data
|
33
|
+
|
34
|
+
rescue MetricaApi::RequestError, MetricaApi::ConnectionError => e
|
35
|
+
|
36
|
+
puts e.message, e.backtrace
|
37
|
+
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
## Testing
|
42
|
+
|
43
|
+
```
|
44
|
+
bundle exec rspec
|
45
|
+
```
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
Create a pull-request or make an issue
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module MetricaApi
|
4
|
+
# Yandex Metrica request module
|
5
|
+
module Request
|
6
|
+
|
7
|
+
module_function
|
8
|
+
|
9
|
+
def make_request(options, params = {})
|
10
|
+
|
11
|
+
params[:oauth_token] = options.fetch(:oauth_token)
|
12
|
+
response = RestClient::Request.execute(build(options, params))
|
13
|
+
MetricaApi.logger << response if MetricaApi.logger
|
14
|
+
response
|
15
|
+
rescue RestClient::Unauthorized,
|
16
|
+
RestClient::Forbidden,
|
17
|
+
RestClient::BadRequest,
|
18
|
+
RestClient::ResourceNotFound => e
|
19
|
+
raise MetricaApi::RequestError,
|
20
|
+
"url: #{build_url(options)}?#{params.to_query}, original: #{e.message}"
|
21
|
+
rescue SocketError => e
|
22
|
+
raise MetricaApi::ConnectionError, e.message
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
def build(options, params)
|
27
|
+
params[:oauth_token] = options.fetch(:oauth_token)
|
28
|
+
{
|
29
|
+
method: :get,
|
30
|
+
url: build_url(options),
|
31
|
+
headers: { params: params },
|
32
|
+
log: MetricaApi.logger
|
33
|
+
}.compact
|
34
|
+
end
|
35
|
+
|
36
|
+
def build_url(options)
|
37
|
+
api_section = options[:api_section]
|
38
|
+
version = options[:v] || DEFAULT_VERSION
|
39
|
+
method = options[:method]
|
40
|
+
format = options[:format] || DEFAULT_FORMAT
|
41
|
+
"#{API_HOST}/#{api_section}/#{version}/#{method}.#{format}"
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module MetricaApi
|
4
|
+
# Yandex Metrica response parser
|
5
|
+
module Response
|
6
|
+
|
7
|
+
module_function
|
8
|
+
|
9
|
+
def parse(response)
|
10
|
+
if response.is_a?(Array)
|
11
|
+
response.map { |r| parse(r) }
|
12
|
+
elsif response.is_a?(String)
|
13
|
+
JSON.parse(response)
|
14
|
+
else
|
15
|
+
response
|
16
|
+
end
|
17
|
+
rescue JSON::ParserError
|
18
|
+
response
|
19
|
+
end
|
20
|
+
|
21
|
+
def json_parse(response)
|
22
|
+
JSON.parse(response)
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module MetricaApi
|
4
|
+
module Sections
|
5
|
+
# Couters API
|
6
|
+
class Counters
|
7
|
+
|
8
|
+
def initialize(token)
|
9
|
+
@session = Session.new(token)
|
10
|
+
end
|
11
|
+
|
12
|
+
def data(params = {})
|
13
|
+
session.call({ api_section: 'management', method: 'counters' }, params)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
attr_reader :session
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module MetricaApi
|
4
|
+
module Sections
|
5
|
+
# Reporting API
|
6
|
+
class Reporting
|
7
|
+
|
8
|
+
def initialize(token)
|
9
|
+
@session = Session.new(token)
|
10
|
+
end
|
11
|
+
|
12
|
+
def data(params)
|
13
|
+
call('data', params)
|
14
|
+
end
|
15
|
+
|
16
|
+
def drilldown(params)
|
17
|
+
call('data/drilldown', params)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
attr_reader :session
|
23
|
+
|
24
|
+
def call(method, params)
|
25
|
+
session.call({ api_section: 'stat', method: method }, params)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module MetricaApi
|
4
|
+
# Metrica Api session object
|
5
|
+
class Session
|
6
|
+
|
7
|
+
def initialize(token)
|
8
|
+
@token = token
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(options, params)
|
12
|
+
MetricaApi.call(options.merge(oauth_token: token), params)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
attr_reader :token
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
data/lib/metrica_api.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'restclient'
|
4
|
+
require 'json'
|
5
|
+
require 'metrica_api/sections/reporting'
|
6
|
+
require 'metrica_api/sections/counters'
|
7
|
+
|
8
|
+
# Yandex Metrica API module
|
9
|
+
module MetricaApi
|
10
|
+
|
11
|
+
API_HOST = 'https://api-metrika.yandex.ru'
|
12
|
+
DEFAULT_VERSION = 'v1'
|
13
|
+
DEFAULT_FORMAT = 'json'
|
14
|
+
|
15
|
+
autoload :Response, 'metrica_api/response'
|
16
|
+
autoload :Request, 'metrica_api/request'
|
17
|
+
autoload :Session, 'metrica_api/session'
|
18
|
+
|
19
|
+
autoload :ConnectionError, 'metrica_api/errors/connection_error'
|
20
|
+
autoload :RequestError, 'metrica_api/errors/request_error'
|
21
|
+
|
22
|
+
autoload :Counters, 'metrica_api/sections/counters'
|
23
|
+
autoload :Reporting, 'metrica_api/sections/reporting'
|
24
|
+
|
25
|
+
mattr_accessor :logger
|
26
|
+
|
27
|
+
module_function
|
28
|
+
|
29
|
+
# @param options [Hash<:api_section, :method, :oauth_token[, :v[, :format]]>] navigation to method
|
30
|
+
# @param params [Hash] params for method
|
31
|
+
# @result [Hash] response as ruby plain object
|
32
|
+
def call(options, params)
|
33
|
+
response_parser.parse(request.make_request(options, params).to_s)
|
34
|
+
end
|
35
|
+
|
36
|
+
def counters(token)
|
37
|
+
Sections::Counters.new(token)
|
38
|
+
end
|
39
|
+
|
40
|
+
def reporting(token)
|
41
|
+
Sections::Reporting.new(token)
|
42
|
+
end
|
43
|
+
|
44
|
+
def response_parser
|
45
|
+
Response
|
46
|
+
end
|
47
|
+
|
48
|
+
def request
|
49
|
+
Request
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
data/metrica_api.gemspec
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# coding: utf-8
|
3
|
+
|
4
|
+
lib = File.expand_path('lib', __dir__)
|
5
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
6
|
+
require 'metrica_api/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = 'metrica_api'
|
10
|
+
spec.version = MetricaApi::VERSION
|
11
|
+
spec.authors = ['OneRetarget.com']
|
12
|
+
spec.email = ['help@oneretarget.com']
|
13
|
+
spec.summary = 'Ruby client for Yandex Metrica API'
|
14
|
+
spec.description = <<~DESC
|
15
|
+
Ruby client for Yandex Metrica API.
|
16
|
+
Make requests to counter and reporting sections of API.
|
17
|
+
OneRetarget.com - advertising automation service
|
18
|
+
DESC
|
19
|
+
spec.homepage = 'https://github.com/resivalex/metrica_api'
|
20
|
+
spec.license = 'MIT'
|
21
|
+
|
22
|
+
spec.files = Dir['**/*']
|
23
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
24
|
+
spec.test_files = spec.files.grep(%r{^spec/})
|
25
|
+
spec.require_paths = ['lib']
|
26
|
+
|
27
|
+
spec.add_runtime_dependency 'json', '~> 2.0', '>= 2.0.0'
|
28
|
+
spec.add_runtime_dependency 'rest-client', '~> 2.0', '>= 2.0.0'
|
29
|
+
|
30
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
31
|
+
spec.add_development_dependency 'rake', '~> 12.3.0', '>= 12.3.0'
|
32
|
+
spec.add_development_dependency 'rspec', '~> 3.7.0', '>= 3.7.0'
|
33
|
+
spec.add_development_dependency 'rubocop', '~> 0.54.0'
|
34
|
+
spec.add_development_dependency 'webmock', '~> 2.3.2', '>= 2.3.2'
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,188 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: metrica_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- OneRetarget.com
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-05-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 2.0.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.0'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.0.0
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rest-client
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '2.0'
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 2.0.0
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '2.0'
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 2.0.0
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: bundler
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '1.6'
|
60
|
+
type: :development
|
61
|
+
prerelease: false
|
62
|
+
version_requirements: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - "~>"
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '1.6'
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
name: rake
|
69
|
+
requirement: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - "~>"
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: 12.3.0
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: 12.3.0
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - "~>"
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: 12.3.0
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: 12.3.0
|
87
|
+
- !ruby/object:Gem::Dependency
|
88
|
+
name: rspec
|
89
|
+
requirement: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - "~>"
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 3.7.0
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 3.7.0
|
97
|
+
type: :development
|
98
|
+
prerelease: false
|
99
|
+
version_requirements: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 3.7.0
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: 3.7.0
|
107
|
+
- !ruby/object:Gem::Dependency
|
108
|
+
name: rubocop
|
109
|
+
requirement: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - "~>"
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: 0.54.0
|
114
|
+
type: :development
|
115
|
+
prerelease: false
|
116
|
+
version_requirements: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - "~>"
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: 0.54.0
|
121
|
+
- !ruby/object:Gem::Dependency
|
122
|
+
name: webmock
|
123
|
+
requirement: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - "~>"
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: 2.3.2
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: 2.3.2
|
131
|
+
type: :development
|
132
|
+
prerelease: false
|
133
|
+
version_requirements: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - "~>"
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: 2.3.2
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: 2.3.2
|
141
|
+
description: |
|
142
|
+
Ruby client for Yandex Metrica API.
|
143
|
+
Make requests to counter and reporting sections of API.
|
144
|
+
OneRetarget.com - advertising automation service
|
145
|
+
email:
|
146
|
+
- help@oneretarget.com
|
147
|
+
executables: []
|
148
|
+
extensions: []
|
149
|
+
extra_rdoc_files: []
|
150
|
+
files:
|
151
|
+
- Gemfile
|
152
|
+
- LICENSE.txt
|
153
|
+
- README.md
|
154
|
+
- lib/metrica_api.rb
|
155
|
+
- lib/metrica_api/errors/connection_error.rb
|
156
|
+
- lib/metrica_api/errors/request_error.rb
|
157
|
+
- lib/metrica_api/request.rb
|
158
|
+
- lib/metrica_api/response.rb
|
159
|
+
- lib/metrica_api/sections/counters.rb
|
160
|
+
- lib/metrica_api/sections/reporting.rb
|
161
|
+
- lib/metrica_api/session.rb
|
162
|
+
- lib/metrica_api/version.rb
|
163
|
+
- metrica_api.gemspec
|
164
|
+
homepage: https://github.com/resivalex/metrica_api
|
165
|
+
licenses:
|
166
|
+
- MIT
|
167
|
+
metadata: {}
|
168
|
+
post_install_message:
|
169
|
+
rdoc_options: []
|
170
|
+
require_paths:
|
171
|
+
- lib
|
172
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
173
|
+
requirements:
|
174
|
+
- - ">="
|
175
|
+
- !ruby/object:Gem::Version
|
176
|
+
version: '0'
|
177
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
178
|
+
requirements:
|
179
|
+
- - ">="
|
180
|
+
- !ruby/object:Gem::Version
|
181
|
+
version: '0'
|
182
|
+
requirements: []
|
183
|
+
rubyforge_project:
|
184
|
+
rubygems_version: 2.6.11
|
185
|
+
signing_key:
|
186
|
+
specification_version: 4
|
187
|
+
summary: Ruby client for Yandex Metrica API
|
188
|
+
test_files: []
|