adriver_api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 26aae93fcbcaa1e982e6e6bb25b3caf72e3a22e6
4
+ data.tar.gz: f5aada894b4b215b0e1aa818daf4e5c9c5d71bd3
5
+ SHA512:
6
+ metadata.gz: f4295c8864a05ff54ebf766d4afac3ba205846481d3c25c3c893831c6ddb802021c17bdf7b5e077b3817387f27dd1bfcc577da9ab8d8fd1f41fe2408f8745df4
7
+ data.tar.gz: b57f9539081657ad2571a9d8b3d12cceb8289b0d8bcc9e7ef1cceb808851e4f5af073a51ae605397232e0f68042f88658df77ebe8ed80bfb5d34c1ca0e9016f3
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in adriver_api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2017 qaa12
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # AdriverApi
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'adriver_api'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install adriver_api
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/adriver_api/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'adriver_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "adriver_api"
8
+ spec.version = AdriverApi::VERSION
9
+ spec.authors = ["Alexey Yudin"]
10
+ spec.email = ["qaa12work@gmail.com"]
11
+ spec.summary = %q{A simple Ruby wrapper for the Adriver API.}
12
+ spec.description = %q{}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_dependency "faraday"
24
+ spec.add_dependency "faraday_middleware"
25
+ end
@@ -0,0 +1,54 @@
1
+ module AdriverApi
2
+ module ApiMethods
3
+
4
+ # List of all user ad companies
5
+ # Список всех рекламных кампаний данного аккаунта
6
+ def ads(raw: false)
7
+ response = make_request('ads', user_id: @user_id.to_s)
8
+ return response if raw
9
+ end
10
+
11
+
12
+ # List of all delegated to user ad companies
13
+ # Список всех рекламных кампаний делегированных данному аккаунту
14
+ def ads_delegated(raw: false)
15
+ response = make_request('ads/delegated', user_id: @user_id.to_s)
16
+ return response if raw
17
+ response.body['feed']['entry'].map{|x| x['id'].split('/').last}
18
+ end
19
+
20
+ # Info about ad
21
+ def ad(ad_id, raw: false)
22
+ response = make_request("ads/#{ad_id}")
23
+ return response if raw
24
+ h = {}
25
+ h[:id] = response.body['entry']['content']['ad']['id']
26
+ h[:name] = response.body['entry']['content']['ad']['name']
27
+ h[:enable] = response.body['entry']['content']['ad']['enable']
28
+ h[:adplacements] = response.body['entry']['content']['ad']['adPlacements']['href'].map{|x| x.split('/').last}
29
+ h
30
+ end
31
+
32
+ # Сценарии
33
+ def adplacements(ad_id, raw: false)
34
+ response = make_request('adplacements', ad_id: ad_id)
35
+ return response if raw
36
+ [t.body['feed']['entry']].flatten.map{|x| {id: x['content']['adPlacement']['id'], name: x['content']['adPlacement']['name'], banners: x['content']['adPlacement']['banners'] ? x['content']['adPlacement']['banners']['href'].map{|x2| x2.split('/').last} : []} }
37
+ end
38
+
39
+ def banner(ban_id)
40
+ response = make_request("banners/#{ban_id}")
41
+ end
42
+
43
+ def stat_banner(ban_id, period: :daily, start_date: (Date.today - 365), stop_date: Date.today, raw: false)
44
+ response = make_request("stat/banners/#{ban_id}", start_date: start_date, stop_date: stop_date, granularity: period)
45
+ return response if raw
46
+ h = {}
47
+ response.body['entry']['content']['statObject']['stat']['item'].each{|x| h[x['date']] = {views: x['exp'], clicks: x['click']} }
48
+ h
49
+ end
50
+
51
+ end
52
+ end
53
+
54
+ # Brand.all_parents.each{|x| t=x.presets.new; t.name='default'; t.is_default=true; t.ys_competitors = x.competitors.pluck(:id).join(','); t.wts_competitors = t.ys_competitors; t.save}
@@ -0,0 +1,68 @@
1
+ #TODO:
2
+ # logger
3
+ # raw data
4
+ # 401 for specific resources
5
+ require File.expand_path('../api_methods', __FILE__)
6
+
7
+ module AdriverApi
8
+ class Client
9
+
10
+ attr_reader :login
11
+ attr_reader :connection
12
+
13
+ include ApiMethods
14
+
15
+ def initialize(login, pass, debug: false)
16
+ @login = login
17
+ @password = pass
18
+ @debug = debug
19
+ @connection = init_connection
20
+ refresh_token
21
+ end
22
+
23
+ private
24
+
25
+ def make_request(url, **opts)
26
+ url = "#{URL}/#{url}"
27
+ begin
28
+ retries ||= 0
29
+ response = @connection.get url, opts
30
+ rescue ::AdriverApi::UnAuthorized => e
31
+ refresh_token
32
+ retry if (retries += 1) < 3
33
+ raise e
34
+ end
35
+ response
36
+ end
37
+
38
+ def init_connection
39
+ options = {
40
+ :headers => {
41
+ 'Content-Type' => 'application/atom+xml',
42
+ 'X-Auth-Login' => @login.to_s,
43
+ 'X-Auth-UserID' => @user_id.to_s,
44
+ 'X-Auth-Passwd' => @token || @password.to_s
45
+ }
46
+ # :proxy => proxy,
47
+ }
48
+
49
+ Faraday::Connection.new(options) do |connection|
50
+ connection.use Faraday::Request::UrlEncoded
51
+ connection.use Faraday::Response::ParseXml
52
+ connection.use FaradayMiddleware::RaiseHttpException
53
+ connection.response :logger, @logger, :bodies => true if @debug
54
+ connection.adapter Faraday.default_adapter
55
+ end
56
+ end
57
+
58
+ def refresh_token
59
+ url = "#{URL}/login"
60
+ @token = nil
61
+ @connection = init_connection
62
+ response = @connection.get url
63
+ @user_id = response.body["entry"]["userId"]
64
+ @token = response.body["entry"]["token"]
65
+ @connection = init_connection
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,20 @@
1
+ module AdriverApi
2
+
3
+ class Error < StandardError; end
4
+
5
+ # Raised when Adriver returns the HTTP status code 400
6
+ class BadRequest < Error; end
7
+
8
+ # Raised when Adriver returns the HTTP status code 404
9
+ class NotFound < Error; end
10
+
11
+ # Raised when Adriver returns the HTTP status code 401
12
+ class UnAuthorized < Error; end
13
+
14
+ # Raised when Adriver returns the HTTP status code 401 and response.body says: 'Not authorized to work with object'
15
+ class AccessDenied < Error; end
16
+
17
+ # Raised when Adriver returns the HTTP status code 500
18
+ class InternalServerError < Error; end
19
+
20
+ end
@@ -0,0 +1,24 @@
1
+ require 'faraday'
2
+
3
+ module FaradayMiddleware
4
+ class RaiseHttpException < Faraday::Middleware
5
+ def call(env)
6
+ @app.call(env).on_complete do |response|
7
+ case response[:status].to_i
8
+ when 400
9
+ raise AdriverApi::BadRequest
10
+ when 401
11
+ raise AdriverApi::AccessDenied if response.body.include? 'Not authorized to work with object'
12
+ raise AdriverApi::UnAuthorized
13
+ when 404
14
+ raise AdriverApi::NotFound
15
+ end
16
+ end
17
+ end
18
+
19
+ def initialize(app)
20
+ super app
21
+ @parser = nil
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module AdriverApi
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,14 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require "adriver_api/version"
4
+ require "adriver_api/error"
5
+ require 'adriver_api/raise_http_exception'
6
+ require "adriver_api/client"
7
+
8
+ module AdriverApi
9
+ URL = 'https://api.adriver.ru'
10
+
11
+ def self.client
12
+
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adriver_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexey Yudin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday_middleware
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: ''
70
+ email:
71
+ - qaa12work@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - adriver_api.gemspec
82
+ - lib/adriver_api.rb
83
+ - lib/adriver_api/api_methods.rb
84
+ - lib/adriver_api/client.rb
85
+ - lib/adriver_api/error.rb
86
+ - lib/adriver_api/raise_http_exception.rb
87
+ - lib/adriver_api/version.rb
88
+ homepage: ''
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.6.10
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: A simple Ruby wrapper for the Adriver API.
112
+ test_files: []