yandex-direct 0.1.0

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: d241e8ace8881b19c05adb27746f97897fc97db2
4
+ data.tar.gz: eebaf1a1bad019b6cd9232c8f5e6a6de99a0aa6f
5
+ SHA512:
6
+ metadata.gz: 1d722a0fd1940189c9a47099ef47f62b80ed611b7c2657fe640fa5f66887c71f13cdba2314586e1a84a0a0614e150097948dd87cae865542d749cf633442f9b0
7
+ data.tar.gz: b5766f27cbeea29bf764bee499dc5eb933016754008dd3c13dda8bf4cfc2623f85ad350ac0dfb61b6a9a6aa424f7c57c0e937ca03f6771af0247487e43aa3946
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ /Gemfile.lock
2
+ *.iml
3
+ .idea
4
+ /yandex_direct.yml
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yandex-direct.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Evgeniy Shurmin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Yandex::Direct
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/yandex/direct`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'yandex-direct'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install yandex-direct
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/yandex-direct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'yandex/direct'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,15 @@
1
+ development:
2
+ token: token
3
+ login: login
4
+ locale: ru
5
+ sandbox: true
6
+ test:
7
+ token: token
8
+ login: login
9
+ locale: ru
10
+ sandbox: true
11
+ production:
12
+ token: token
13
+ login: login
14
+ locale: ru
15
+ sandbox: true
@@ -0,0 +1,37 @@
1
+ require 'yandex/direct/version'
2
+
3
+ module Yandex
4
+ module Direct
5
+
6
+ autoload :ActionResult, 'yandex/direct/action_result'
7
+ autoload :SelectionCriteria, 'yandex/direct/selection_criteria'
8
+ autoload :ExceptionNotification, 'yandex/direct/exception_notification'
9
+ autoload :Error, 'yandex/direct/error'
10
+ autoload :Base, 'yandex/direct/base'
11
+
12
+ autoload :Model, 'yandex/direct/model'
13
+
14
+ def self.configuration
15
+ if defined? @environment
16
+ raise "not configured Yandex.Direct for #{@environment} enviroment" unless @configuration
17
+ else
18
+ raise 'not configured Yandex.Direct' unless @configuration
19
+ end
20
+ @configuration
21
+ end
22
+
23
+ def self.load(file, env = nil)
24
+ @environment = env.to_s if env
25
+ config = YAML.load_file(file)
26
+ @configuration = defined?(@environment) ? config[@environment] : config
27
+ @configuration['sandbox'] ||= false
28
+ end
29
+
30
+ def self.connection
31
+ @connection ||= Faraday.new(url: (configuration['sandbox'] ? URL_API_SANDBOX : URL_API)) do |faraday|
32
+ faraday.use Faraday::Adapter::NetHttp
33
+ end
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,17 @@
1
+ module Yandex::Direct
2
+ class ActionResult
3
+ include ActiveModel::Model
4
+ attr_accessor :Id, :Warnings, :Errors
5
+ def initialize(*args)
6
+ super
7
+ self.Warnings = Array(self.Warnings).map{|attributes| ExceptionNotification.new(attributes)}
8
+ self.Errors = Array(self.Errors).map{|attributes| ExceptionNotification.new(attributes)}
9
+ end
10
+ def errors?
11
+ Array(self.Errors).compact.any?
12
+ end
13
+ def warnings?
14
+ Array(self.Warnings).compact.any?
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,38 @@
1
+ require 'active_model'
2
+
3
+ module Yandex::Direct
4
+ class Base
5
+ include ActiveModel::Model
6
+ include ActiveModel::Serialization
7
+ include ActiveModel::Validations
8
+ class << self
9
+ def initialize(*args)
10
+ super
11
+ end
12
+ def create(attributes)
13
+ object = self.new(attributes)
14
+ object.save
15
+ end
16
+
17
+ delegate :call, :fields, :limit, :offset, :where, to: :selection_criteria
18
+ def path
19
+ self.name.demodulize.pluralize.downcase
20
+ end
21
+ protected
22
+ def selection_criteria
23
+ SelectionCriteria.new(self)
24
+ end
25
+ end
26
+
27
+ def to_param
28
+ self.serializable_hash(except: [:errors, :validation_context])
29
+ end
30
+
31
+ def save
32
+ if self.valid?
33
+ response = Direct.request('add', self.class.path, {'Campaigns' => [self.to_param]})
34
+ puts response.inspect
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,14 @@
1
+ module Yandex::Direct
2
+ class Error < RuntimeError
3
+ attr_accessor :request_id, :code, :text, :detail
4
+ def initialize(request_id, code, text, detail)
5
+ self.request_id = request_id
6
+ self.code = code
7
+ self.text = text
8
+ self.detail = detail
9
+ end
10
+ def message
11
+ "#{self.text} (#{self.code}): #{self.detail}"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ module Yandex
2
+ module API
3
+ module Direct
4
+ class ExceptionNotification
5
+ include ActiveModel::Model
6
+ attr_accessor :Message, :Details, :Code
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ module Yandex::Direct
2
+ module Model
3
+
4
+ end
5
+ end
@@ -0,0 +1,74 @@
1
+ module Yandex::Direct::Model
2
+ class Campaign < Base
3
+ ATTRIBUTES = :Id, :Name, :ClientInfo, :StartDate, :EndDate, :TimeTargeting, :TimeZone, :NegativeKeywords,
4
+ :BlockedIps, :ExcludedSites, :DailyBudget, :Notification, :Type, :Status, :State, :StatusPayment,
5
+ :StatusClarification, :SourceId, :Statistics, :Currency, :Funds, :RepresentedBy
6
+
7
+ attr_accessor *ATTRIBUTES
8
+
9
+ def attributes
10
+ {
11
+ :Id => nil,
12
+ :Name => nil
13
+ }
14
+ end
15
+
16
+ def self.get(selection_criteria)
17
+ response = Yandex::Direct.request('get', self.path, selection_criteria.fields(*ATTRIBUTES))
18
+ Yandex::Direct.parse(response, 'Campaigns',self)
19
+ end
20
+
21
+ def self.find(id)
22
+ self.where(Ids: Array(id)).call(:get).first
23
+ end
24
+
25
+ def self.archive(selection_criteria)
26
+ response = Yandex::Direct.request('archive', self.path, selection_criteria)
27
+ Yandex::Direct.parse(response, 'ArchiveResults', Yandex::Direct::ActionResult)
28
+ end
29
+
30
+ def archive
31
+ self.class.where(Ids: [self.Id]).call(:archive).first
32
+ end
33
+
34
+ def self.unarchive(selection_criteria)
35
+ response = Yandex::Direct.request('unarchive', self.path, selection_criteria)
36
+ Yandex::Direct.parse(response, 'UnarchiveResults', Yandex::Direct::ActionResult)
37
+ end
38
+
39
+ def unarchive
40
+ self.class.where(Ids: [self.Id]).call(:unarchive).first
41
+ end
42
+
43
+ def self.resume(selection_criteria)
44
+ response = Yandex::Direct.request('resume', self.path, selection_criteria)
45
+ Yandex::Direct.parse(response, 'ResumeResults', Yandex::Direct::ActionResult)
46
+ end
47
+
48
+ def resume
49
+ self.class.where(Ids: [self.Id]).call(:resume).first
50
+ end
51
+
52
+ def self.suspend(selection_criteria)
53
+ response = Yandex::Direct.request('suspend', self.path, selection_criteria)
54
+ Yandex::Direct.parse(response, 'SuspendResults', Yandex::Direct::ActionResult)
55
+ end
56
+
57
+ def suspend
58
+ self.class.where(Ids: [self.Id]).call(:suspend).first
59
+ end
60
+
61
+ def self.delete(selection_criteria)
62
+ response = Yandex::Direct.request('delete', self.path, selection_criteria)
63
+ Yandex::Direct.parse(response, 'DeleteResults', Yandex::Direct::ActionResult)
64
+ end
65
+
66
+ def delete
67
+ self.class.where(Ids: [self.Id]).call(:delete).first
68
+ end
69
+
70
+ # TODO: add, update
71
+ # TODO: TextCampaign, DynamicTextCampaign, MobileAppCampaign
72
+
73
+ end
74
+ end
@@ -0,0 +1,41 @@
1
+ module Yandex::Direct
2
+ class SelectionCriteria
3
+ protected
4
+ attr_accessor :selection_criteria, :field_names, :page_limit, :page_offset
5
+ public
6
+ attr_accessor :object
7
+ def initialize(object)
8
+ self.object = object
9
+ self.field_names = nil
10
+ end
11
+ def fields(*field_names)
12
+ self.field_names = Array(field_names)
13
+ self
14
+ end
15
+ def limit(value)
16
+ self.page_limit = value
17
+ self
18
+ end
19
+ def offset(value)
20
+ self.page_offset = value
21
+ self
22
+ end
23
+ def where(criteria = {})
24
+ (self.selection_criteria ||= {}).merge!(criteria)
25
+ self
26
+ end
27
+ def call(method, options = nil)
28
+ puts "[#{self.object.name}](#{method}): #{self.to_param}"
29
+ self.object.method(method).call(*[self, options].compact)
30
+ end
31
+ def to_param
32
+ params = {
33
+ 'SelectionCriteria' => (self.selection_criteria.nil? ? {} : self.selection_criteria),
34
+ }
35
+ params.merge!({'FieldNames' => Array(self.field_names).flatten.uniq}) unless self.field_names.nil?
36
+ params.merge!({'Page' => {'Offset' => self.page_offset}}) if self.page_offset
37
+ params.merge!({'Page' => {'Limit' => self.page_limit}}) if self.page_limit
38
+ params
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,5 @@
1
+ module Yandex
2
+ module Direct
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'yandex/direct/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'yandex-direct'
8
+ spec.version = Yandex::Direct::VERSION
9
+ spec.authors = ['Evgeniy Shurmin']
10
+ spec.email = ['eshurmin@gmail.com']
11
+
12
+ spec.description = %q{Yandex.Direct integration}
13
+ spec.summary = %q{Yandex.Direct integration}
14
+ spec.homepage = 'https://github.com/jpascal/yandex-direct'
15
+
16
+ spec.license = 'MIT'
17
+
18
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
19
+ # delete this section to allow pushing this gem to any host.
20
+ if spec.respond_to?(:metadata)
21
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
22
+ else
23
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ spec.add_development_dependency 'bundler', '~> 1.10'
32
+ spec.add_development_dependency 'rake', '~> 10.0'
33
+ spec.add_development_dependency 'rspec'
34
+ spec.add_dependency 'activesupport'
35
+ spec.add_dependency 'activemodel'
36
+ spec.add_dependency 'faraday'
37
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yandex-direct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Evgeniy Shurmin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-08 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: activesupport
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
+ - !ruby/object:Gem::Dependency
70
+ name: activemodel
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: faraday
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Yandex.Direct integration
98
+ email:
99
+ - eshurmin@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - bin/console
110
+ - bin/setup
111
+ - example/yandex_direct.yml
112
+ - lib/yandex/direct.rb
113
+ - lib/yandex/direct/action_result.rb
114
+ - lib/yandex/direct/base.rb
115
+ - lib/yandex/direct/error.rb
116
+ - lib/yandex/direct/exception_notification.rb
117
+ - lib/yandex/direct/model.rb
118
+ - lib/yandex/direct/model/campaign.rb
119
+ - lib/yandex/direct/selection_criteria.rb
120
+ - lib/yandex/direct/version.rb
121
+ - yandex-direct.gemspec
122
+ homepage: https://github.com/jpascal/yandex-direct
123
+ licenses:
124
+ - MIT
125
+ metadata:
126
+ allowed_push_host: https://rubygems.org
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.2.2
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Yandex.Direct integration
147
+ test_files: []
148
+ has_rdoc: