yandex-direct 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d241e8ace8881b19c05adb27746f97897fc97db2
4
- data.tar.gz: eebaf1a1bad019b6cd9232c8f5e6a6de99a0aa6f
3
+ metadata.gz: b0cec38936d361a0171b18dc978934f4dd823835
4
+ data.tar.gz: aaf0817a6842d9845e963a441ba0a9320fe69b5a
5
5
  SHA512:
6
- metadata.gz: 1d722a0fd1940189c9a47099ef47f62b80ed611b7c2657fe640fa5f66887c71f13cdba2314586e1a84a0a0614e150097948dd87cae865542d749cf633442f9b0
7
- data.tar.gz: b5766f27cbeea29bf764bee499dc5eb933016754008dd3c13dda8bf4cfc2623f85ad350ac0dfb61b6a9a6aa424f7c57c0e937ca03f6771af0247487e43aa3946
6
+ metadata.gz: 715f250adb7d2d04019b78ca052d4dbe1d274e6b5033a1524fc8ae421696043b406e54142c61a504d035242fd9a6c01dab8e860aaadc466511ab75b490714e4b
7
+ data.tar.gz: 9bd982d87357b40a829f138577c4183fdfafc8dce59f6254f61b3efc754adb0bac2f4de998af7c2eb68b5b2dd2b3b4b24423632bb298bec568f84376437845dc
data/README.md CHANGED
@@ -1,14 +1,12 @@
1
1
  # Yandex::Direct
2
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
3
+ Allow you work with any Yandex.Direct API
6
4
 
7
5
  ## Installation
8
6
 
9
7
  Add this line to your application's Gemfile:
10
8
 
11
- ```ruby
9
+ ```
12
10
  gem 'yandex-direct'
13
11
  ```
14
12
 
@@ -20,22 +18,59 @@ Or install it yourself as:
20
18
 
21
19
  $ gem install yandex-direct
22
20
 
23
- ## Usage
21
+ ## Configuration
24
22
 
25
- TODO: Write usage instructions here
23
+ ### Ruby:
26
24
 
27
- ## Development
25
+ Create configuration file yandex_direct.yml
28
26
 
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.
27
+ token: token
28
+ login: login
29
+ locale: ru
30
+ verbose: true
31
+ sandbox: true
30
32
 
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).
33
+ Load configuration
34
+
35
+ Yandex::Direct.config "yandex_direct.yml"
32
36
 
33
- ## Contributing
37
+ ### Ruby On Rails:
38
+
39
+ Create configuration file config/yandex_direct.yml
40
+
41
+ development:
42
+ token: token
43
+ login: login
44
+ locale: ru
45
+ verbose: true
46
+ sandbox: true
47
+
48
+ Create yandex_direct.rb in config/initializers
34
49
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/yandex-direct.
50
+ Yandex::Direct.config File.join(Rails.root, "config", "yandex_direct.yml"), Rails.env
36
51
 
52
+ ## Example:
37
53
 
38
- ## License
54
+ require 'yandex-direct'
55
+ Yandex::Direct.load "yandex_direct.yml"
39
56
 
40
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
57
+ campaign = Yandex::Direct::Campaign.where(Ids: [345545]).limit(1).call(:get).first
58
+ puts campaign.inspect
59
+
60
+ campaign = Yandex::Direct::Campaign.get(345545)
61
+ puts campaign.inspect
62
+
63
+ ## Todo list
64
+
65
+ - write tests
66
+ - add others Yandex.Direct model
67
+ - add validations
68
+ - add relations between models
69
+
70
+ ## Contributing
41
71
 
72
+ 1. Fork it ( https://github.com/jpascal/yandex-direct/fork )
73
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
74
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
75
+ 4. Push to the branch (`git push origin my-new-feature`)
76
+ 5. Create a new Pull Request
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task default: :spec
@@ -1,37 +1,16 @@
1
+ require 'yaml'
1
2
  require 'yandex/direct/version'
3
+ require 'yandex/direct/config'
4
+ require 'yandex/direct/client'
5
+ require 'yandex/direct/request'
6
+ require 'yandex/direct/models'
2
7
 
3
8
  module Yandex
4
9
  module Direct
5
-
6
10
  autoload :ActionResult, 'yandex/direct/action_result'
7
11
  autoload :SelectionCriteria, 'yandex/direct/selection_criteria'
8
12
  autoload :ExceptionNotification, 'yandex/direct/exception_notification'
9
13
  autoload :Error, 'yandex/direct/error'
10
14
  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
15
  end
37
16
  end
@@ -1,4 +1,5 @@
1
1
  require 'active_model'
2
+ require 'active_model/serializers/json'
2
3
 
3
4
  module Yandex::Direct
4
5
  class Base
@@ -0,0 +1,13 @@
1
+ require 'faraday'
2
+
3
+ module Yandex
4
+ module Direct
5
+ URL_API = 'https://api.direct.yandex.com/json/v5'.freeze
6
+ URL_API_SANDBOX = 'https://api-sandbox.direct.yandex.com/json/v5'.freeze
7
+ def self.client
8
+ @client ||= Faraday.new(url: (configuration['sandbox'] ? URL_API_SANDBOX : URL_API)) do |faraday|
9
+ faraday.use Faraday::Adapter::NetHttp
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ require 'yaml'
2
+
3
+ module Yandex
4
+ module Direct
5
+ def self.configuration
6
+ if defined? @environment
7
+ raise "not configured Yandex.Direct for #{@environment} enviroment" unless @configuration
8
+ else
9
+ raise 'not configured Yandex.Direct' unless @configuration
10
+ end
11
+ @configuration
12
+ end
13
+ def self.config(file, env = nil)
14
+ @environment = env.to_s if env
15
+ config = YAML.load_file(file)
16
+ @configuration = defined?(@environment) ? config[@environment] : config
17
+ @configuration['sandbox'] ||= false
18
+ @configuration
19
+ end
20
+ end
21
+ end
@@ -1,10 +1,8 @@
1
1
  module Yandex
2
- module API
3
- module Direct
4
- class ExceptionNotification
5
- include ActiveModel::Model
6
- attr_accessor :Message, :Details, :Code
7
- end
2
+ module Direct
3
+ class ExceptionNotification
4
+ include ActiveModel::Model
5
+ attr_accessor :Message, :Details, :Code
8
6
  end
9
7
  end
10
8
  end
@@ -0,0 +1,3 @@
1
+ module Yandex::Direct
2
+ autoload :Campaign, 'yandex/direct/models/campaign'
3
+ end
@@ -0,0 +1,76 @@
1
+ module Yandex
2
+ module Direct
3
+ class Campaign < Base
4
+ ATTRIBUTES = :Id, :Name, :ClientInfo, :StartDate, :EndDate, :TimeTargeting, :TimeZone, :NegativeKeywords,
5
+ :BlockedIps, :ExcludedSites, :DailyBudget, :Notification, :Type, :Status, :State, :StatusPayment,
6
+ :StatusClarification, :SourceId, :Statistics, :Currency, :Funds, :RepresentedBy
7
+
8
+ attr_accessor *ATTRIBUTES
9
+
10
+ def attributes
11
+ {
12
+ :Id => nil,
13
+ :Name => nil
14
+ }
15
+ end
16
+
17
+ def self.get(selection_criteria)
18
+ response = Yandex::Direct.request('get', self.path, selection_criteria.fields(*ATTRIBUTES))
19
+ Yandex::Direct.parse(response, 'Campaigns',self)
20
+ end
21
+
22
+ def self.find(id)
23
+ self.where(Ids: Array(id)).call(:get).first
24
+ end
25
+
26
+ def self.archive(selection_criteria)
27
+ response = Yandex::Direct.request('archive', self.path, selection_criteria)
28
+ Yandex::Direct.parse(response, 'ArchiveResults', Yandex::Direct::ActionResult)
29
+ end
30
+
31
+ def archive
32
+ self.class.where(Ids: [self.Id]).call(:archive).first
33
+ end
34
+
35
+ def self.unarchive(selection_criteria)
36
+ response = Yandex::Direct.request('unarchive', self.path, selection_criteria)
37
+ Yandex::Direct.parse(response, 'UnarchiveResults', Yandex::Direct::ActionResult)
38
+ end
39
+
40
+ def unarchive
41
+ self.class.where(Ids: [self.Id]).call(:unarchive).first
42
+ end
43
+
44
+ def self.resume(selection_criteria)
45
+ response = Yandex::Direct.request('resume', self.path, selection_criteria)
46
+ Yandex::Direct.parse(response, 'ResumeResults', Yandex::Direct::ActionResult)
47
+ end
48
+
49
+ def resume
50
+ self.class.where(Ids: [self.Id]).call(:resume).first
51
+ end
52
+
53
+ def self.suspend(selection_criteria)
54
+ response = Yandex::Direct.request('suspend', self.path, selection_criteria)
55
+ Yandex::Direct.parse(response, 'SuspendResults', Yandex::Direct::ActionResult)
56
+ end
57
+
58
+ def suspend
59
+ self.class.where(Ids: [self.Id]).call(:suspend).first
60
+ end
61
+
62
+ def self.delete(selection_criteria)
63
+ response = Yandex::Direct.request('delete', self.path, selection_criteria)
64
+ Yandex::Direct.parse(response, 'DeleteResults', Yandex::Direct::ActionResult)
65
+ end
66
+
67
+ def delete
68
+ self.class.where(Ids: [self.Id]).call(:delete).first
69
+ end
70
+
71
+ # TODO: add, update
72
+ # TODO: TextCampaign, DynamicTextCampaign, MobileAppCampaign
73
+
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,31 @@
1
+ require 'faraday'
2
+
3
+ module Yandex
4
+ module Direct
5
+ def self.parse(hash, key, klass)
6
+ hash.fetch(key,[]).map{|attributes| klass.new(attributes)}
7
+ end
8
+
9
+ def self.request(method, path, params = nil)
10
+ response = self.client.post(path, {
11
+ 'method' => method,
12
+ 'params' => (params.respond_to?(:to_param) ? params.to_param : params)
13
+ }.to_json, {
14
+ 'Authorization' => "Bearer #{configuration['token']}",
15
+ 'Client-Login' => configuration['login'],
16
+ 'Accept-Language' => configuration['locale'],
17
+ 'Content-Type' => 'application/json; charset=utf-8'
18
+ })
19
+
20
+ raise "Yandex.Direct response with status #{response.status}" unless response.success?
21
+
22
+ response = JSON.parse(response.body)
23
+
24
+ if (error = response['error'])
25
+ raise Error.new(error['request_id'], error['error_code'], error['error_string'], error['error_detail'])
26
+ end
27
+
28
+ response['result']
29
+ end
30
+ end
31
+ end
@@ -1,5 +1,5 @@
1
1
  module Yandex
2
2
  module Direct
3
- VERSION = '0.1.0'
3
+ VERSION = '0.1.1'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yandex-direct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Shurmin
@@ -112,10 +112,13 @@ files:
112
112
  - lib/yandex/direct.rb
113
113
  - lib/yandex/direct/action_result.rb
114
114
  - lib/yandex/direct/base.rb
115
+ - lib/yandex/direct/client.rb
116
+ - lib/yandex/direct/config.rb
115
117
  - lib/yandex/direct/error.rb
116
118
  - lib/yandex/direct/exception_notification.rb
117
- - lib/yandex/direct/model.rb
118
- - lib/yandex/direct/model/campaign.rb
119
+ - lib/yandex/direct/models.rb
120
+ - lib/yandex/direct/models/campaign.rb
121
+ - lib/yandex/direct/request.rb
119
122
  - lib/yandex/direct/selection_criteria.rb
120
123
  - lib/yandex/direct/version.rb
121
124
  - yandex-direct.gemspec
@@ -1,5 +0,0 @@
1
- module Yandex::Direct
2
- module Model
3
-
4
- end
5
- end
@@ -1,74 +0,0 @@
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