metrika 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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+ gem 'oauth2'
3
+ gem 'yajl-ruby'
4
+
5
+ # Add dependencies to develop your gem here.
6
+ # Include everything needed to run rake, tests, features, etc.
7
+ group :development do
8
+ gem "shoulda", ">= 0"
9
+ gem "rdoc", "~> 3.12"
10
+ gem "bundler", "~> 1.1.5"
11
+ gem "jeweler", "~> 1.8.4"
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,49 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.2.8)
5
+ i18n (~> 0.6)
6
+ multi_json (~> 1.0)
7
+ faraday (0.8.4)
8
+ multipart-post (~> 1.1)
9
+ git (1.2.5)
10
+ httpauth (0.1)
11
+ i18n (0.6.1)
12
+ jeweler (1.8.4)
13
+ bundler (~> 1.0)
14
+ git (>= 1.2.5)
15
+ rake
16
+ rdoc
17
+ json (1.7.5)
18
+ jwt (0.1.5)
19
+ multi_json (>= 1.0)
20
+ multi_json (1.3.6)
21
+ multipart-post (1.1.5)
22
+ oauth2 (0.8.0)
23
+ faraday (~> 0.8)
24
+ httpauth (~> 0.1)
25
+ jwt (~> 0.1.4)
26
+ multi_json (~> 1.0)
27
+ rack (~> 1.2)
28
+ rack (1.4.1)
29
+ rake (0.9.2.2)
30
+ rdoc (3.12)
31
+ json (~> 1.4)
32
+ shoulda (3.1.1)
33
+ shoulda-context (~> 1.0)
34
+ shoulda-matchers (~> 1.2)
35
+ shoulda-context (1.0.0)
36
+ shoulda-matchers (1.3.0)
37
+ activesupport (>= 3.0.0)
38
+ yajl-ruby (1.1.0)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ bundler (~> 1.1.5)
45
+ jeweler (~> 1.8.4)
46
+ oauth2
47
+ rdoc (~> 3.12)
48
+ shoulda
49
+ yajl-ruby
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Igor Alexandrov
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,27 @@
1
+ Metrika
2
+ =======
3
+
4
+ Wrapper for Yandex.Metrika API
5
+
6
+ ### Info
7
+
8
+ **Under heavy development!**
9
+
10
+ ### Usage
11
+
12
+ c = Metrika::Client.new('your_app_id', 'your_app_password')
13
+
14
+ # if you don't have token yet
15
+ c.authorization_url
16
+ c.authorize_token('authorization_code')
17
+
18
+ # if you alredy have token
19
+ c.restore_token('token_code')
20
+
21
+ # counters
22
+ c.get_counters
23
+ c.creater_counter(params)
24
+ c.get_counter(id)
25
+ c.update_counter(id, params)
26
+ c.delete_counter(id)
27
+ c.check_counter(id)
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "metrika"
18
+ gem.homepage = "http://github.com/igor-alexandrov/metrika"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Wrapper for Yandex.Metrika API}
21
+ gem.email = "igor.alexandrov@gmail.com"
22
+ gem.authors = ["Igor Alexandrov"]
23
+ # dependencies defined in Gemfile
24
+ end
25
+ Jeweler::RubygemsDotOrgTasks.new
26
+
27
+ require 'rake/testtask'
28
+ Rake::TestTask.new(:test) do |test|
29
+ test.libs << 'lib' << 'test'
30
+ test.pattern = 'test/**/test_*.rb'
31
+ test.verbose = true
32
+ end
33
+
34
+ task :default => :test
35
+
36
+ require 'rdoc/task'
37
+ Rake::RDocTask.new do |rdoc|
38
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
39
+
40
+ rdoc.rdoc_dir = 'rdoc'
41
+ rdoc.title = "metrika #{version}"
42
+ rdoc.rdoc_files.include('README*')
43
+ rdoc.rdoc_files.include('lib/**/*.rb')
44
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/lib/metrika.rb ADDED
@@ -0,0 +1,35 @@
1
+ METRIKA_PATH = File.dirname(__FILE__) + "/metrika/"
2
+
3
+ require METRIKA_PATH + 'helpers/authorization'
4
+ require METRIKA_PATH + 'helpers/parser'
5
+ require METRIKA_PATH + 'helpers/request'
6
+
7
+ require METRIKA_PATH + 'api/methods'
8
+ require METRIKA_PATH + 'client'
9
+ require METRIKA_PATH + 'errors'
10
+
11
+ require 'oauth2'
12
+ require 'yajl'
13
+
14
+ module Metrika
15
+
16
+ class << self
17
+ attr_accessor :application_id, :application_password
18
+
19
+ # config/initializers/metrika.rb (for instance)
20
+ #
21
+ # Metrika.configure do |config|
22
+ # config.application_id = 'application_id'
23
+ # config.application_password = 'application_password'
24
+ # end
25
+ #
26
+ # elsewhere
27
+ #
28
+ # client = Metrika::Client.new
29
+
30
+ def configure
31
+ yield self
32
+ true
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,41 @@
1
+ module Metrika
2
+ module Api
3
+ module Methods
4
+ def get_counters
5
+ self.get(self.counters_path)['counters']
6
+ end
7
+
8
+ def counters_path
9
+ "/counters"
10
+ end
11
+
12
+ def get_counter(id)
13
+ self.get(self.counter_path(id))['counter']
14
+ end
15
+
16
+ def create_counter(params)
17
+ self.post(self.counters_path, params)['counter']
18
+ end
19
+
20
+ def update_counter(id, params)
21
+ self.put(self.counter_path(id), params)['counter']
22
+ end
23
+
24
+ def delete_counter(id)
25
+ self.delete(self.counter_path(id))['counter']
26
+ end
27
+
28
+ def counter_path(id)
29
+ "/counter/#{id}"
30
+ end
31
+
32
+ def check_counter(id)
33
+ self.get(self.counter_check_path(id))['counter']
34
+ end
35
+
36
+ def counter_check_path(id)
37
+ "/counter/#{id}/check"
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,14 @@
1
+ module Metrika
2
+ class Client
3
+ include Helpers::Authorization
4
+ include Helpers::Request
5
+ include Helpers::Parser
6
+
7
+ include Api::Methods
8
+
9
+ def initialize(application_id = Metrika.application_id, application_password = Metrika.application_password)
10
+ @application_id = application_id
11
+ @application_password = application_password
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ module Metrika
2
+ module Errors
3
+ class MetrikaError < StandardError
4
+ attr_reader :data
5
+
6
+ def initialize(data)
7
+ @data = data
8
+ super
9
+ end
10
+ end
11
+
12
+ class UnauthorizedError < MetrikaError; end
13
+ class GeneralError < MetrikaError; end
14
+ class AccessDeniedError < MetrikaError; end
15
+ class NoTokenError < StandardError; end
16
+ class NotFoundError < StandardError; end
17
+ end
18
+ end
@@ -0,0 +1,31 @@
1
+ module Metrika
2
+ module Helpers
3
+ module Authorization
4
+ attr_reader :token
5
+
6
+ DEFAULT_OAUTH_OPTIONS = {
7
+ :site => 'http://api-metrika.yandex.ru',
8
+ :authorize_url => 'https://oauth.yandex.ru/authorize',
9
+ :token_url => 'https://oauth.yandex.ru/token'
10
+ }
11
+
12
+ def authorize_token(authorization_code)
13
+ @token = (self.client.auth_code.get_token(authorization_code) rescue nil)
14
+ end
15
+
16
+ def authorization_url
17
+ self.client.auth_code.authorize_url
18
+ end
19
+
20
+ def restore_token(token_code)
21
+ @token = OAuth2::AccessToken.new(self.client, token_code)
22
+ end
23
+
24
+ protected
25
+
26
+ def client
27
+ @client ||= OAuth2::Client.new(@application_id, @application_password, DEFAULT_OAUTH_OPTIONS)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,11 @@
1
+ module Metrika
2
+ module Helpers
3
+ module Parser
4
+
5
+ protected
6
+ def parse_json(data)
7
+ Yajl::Parser.parse(data)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,57 @@
1
+ module Metrika
2
+ module Helpers
3
+ module Request
4
+ DEFAULT_OPTIONS = {
5
+ :headers => {
6
+ 'Accept' => 'application/x-yametrika+json',
7
+ 'Content-Type' => 'application/x-yametrika+json'
8
+ }
9
+ }
10
+
11
+ protected
12
+
13
+ def get(path, params = {}, options = {})
14
+ # raise Metrika::Errors::NoTokenError unless self.token
15
+
16
+
17
+ response = self.token.get(path, DEFAULT_OPTIONS.merge(:params => params).merge(options))
18
+ # rescue OAuth2::Error => e
19
+
20
+ # end
21
+
22
+ # self.raise_errors(response)
23
+ Yajl::Parser.parse(response.body)
24
+ end
25
+
26
+ def post(path, body = {}, options = {})
27
+ # raise Metrika::Errors::NoTokenError unless self.token
28
+
29
+ encoded_body = Yajl::Encoder.encode(body)
30
+ response = self.token.post(path, DEFAULT_OPTIONS.merge(:body => encoded_body).merge(options))
31
+
32
+ # self.raise_errors(response)
33
+ Yajl::Parser.parse(response.body)
34
+ end
35
+
36
+
37
+ def put(path, body = {}, options = {})
38
+ # raise Metrika::Errors::NoTokenError unless self.token
39
+
40
+ encoded_body = Yajl::Encoder.encode(body)
41
+ response = self.token.put(path, DEFAULT_OPTIONS.merge(:body => encoded_body).merge(options))
42
+
43
+ # self.raise_errors(response)
44
+ Yajl::Parser.parse(response.body)
45
+ end
46
+
47
+ def delete(path, options={})
48
+ # raise Metrika::Errors::NoTokenError unless self.token
49
+
50
+ response = self.token.delete(path, DEFAULT_OPTIONS.merge(options))
51
+
52
+ # self.raise_errors(response)
53
+ Yajl::Parser.parse(response.body)
54
+ end
55
+ end
56
+ end
57
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'metrika'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestMetrika < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: metrika
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Igor Alexandrov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: oauth2
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: yajl-ruby
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: shoulda
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rdoc
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '3.12'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '3.12'
78
+ - !ruby/object:Gem::Dependency
79
+ name: bundler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 1.1.5
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 1.1.5
94
+ - !ruby/object:Gem::Dependency
95
+ name: jeweler
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 1.8.4
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 1.8.4
110
+ description:
111
+ email: igor.alexandrov@gmail.com
112
+ executables: []
113
+ extensions: []
114
+ extra_rdoc_files:
115
+ - LICENSE.txt
116
+ - README.md
117
+ files:
118
+ - .document
119
+ - Gemfile
120
+ - Gemfile.lock
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - VERSION
125
+ - lib/metrika.rb
126
+ - lib/metrika/api/methods.rb
127
+ - lib/metrika/client.rb
128
+ - lib/metrika/errors.rb
129
+ - lib/metrika/helpers/authorization.rb
130
+ - lib/metrika/helpers/parser.rb
131
+ - lib/metrika/helpers/request.rb
132
+ - test/helper.rb
133
+ - test/test_metrika.rb
134
+ homepage: http://github.com/igor-alexandrov/metrika
135
+ licenses:
136
+ - MIT
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ! '>='
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ segments:
148
+ - 0
149
+ hash: -798621709870789579
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ none: false
152
+ requirements:
153
+ - - ! '>='
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 1.8.24
159
+ signing_key:
160
+ specification_version: 3
161
+ summary: Wrapper for Yandex.Metrika API
162
+ test_files: []