direct_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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: df6294498951193b3284ba81e703628276ace4b6
4
+ data.tar.gz: 931fa14348cc45587ed9bc88342488af1adbfa2b
5
+ SHA512:
6
+ metadata.gz: 337fc7adfbe1b359b815202200d0adaeb6f08510ca344e858ffd2ed3fb1f1753643c2f36b7781c2005cabb70b327fb7f4a94ad0317f8d3519657dbe9687cf66f
7
+ data.tar.gz: 03e2aa833a032bb8b719f8cd121f824069f1b4b3afb52786a39d373db4a2f3a7b81929dca547e631822aa90ee7539ca785d3132bfc06c5f94d0bcef979a18490
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in direct_api.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,38 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ direct_api (0.0.1)
5
+ json (~> 2.0, >= 2.0.0)
6
+ rest-client (~> 2.0, >= 2.0.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ domain_name (0.5.20170404)
12
+ unf (>= 0.0.5, < 1.0.0)
13
+ http-cookie (1.0.3)
14
+ domain_name (~> 0.5)
15
+ json (2.1.0)
16
+ mime-types (3.1)
17
+ mime-types-data (~> 3.2015)
18
+ mime-types-data (3.2016.0521)
19
+ netrc (0.11.0)
20
+ rake (12.3.0)
21
+ rest-client (2.0.2)
22
+ http-cookie (>= 1.0.2, < 2.0)
23
+ mime-types (>= 1.16, < 4.0)
24
+ netrc (~> 0.8)
25
+ unf (0.1.4)
26
+ unf_ext
27
+ unf_ext (0.0.7.5)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ bundler (~> 1.6)
34
+ direct_api!
35
+ rake (~> 12.3.0, >= 12.3.0)
36
+
37
+ BUNDLED WITH
38
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Eugeniy Belyaev
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,25 @@
1
+ # Yandex Direct API
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ gem 'direct_api'
8
+
9
+ And then execute:
10
+
11
+ $ bundle
12
+
13
+ Or install it yourself as:
14
+
15
+ $ gem install direct_api
16
+
17
+ ## Usage
18
+
19
+ ## Contributing
20
+
21
+ 1. Fork it ( https://github.com/[my-github-username]/direct_api/fork )
22
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
23
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
24
+ 4. Push to the branch (`git push origin my-new-feature`)
25
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path('../lib', __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require 'direct_api/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'direct_api'
10
+ spec.version = DirectApi::VERSION
11
+ spec.authors = ['Gennady Novoselov']
12
+ spec.email = ['gennady@novoselov.biz']
13
+ spec.summary = 'Yandex Direct api via oauth2'
14
+ spec.description = 'Yandex Direct api via oauth2'
15
+ spec.homepage = 'https://github.com/gnovoselov/direct-api'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = Dir['**/*']
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_runtime_dependency 'json', '~> 2.0', '>= 2.0.0'
24
+ spec.add_runtime_dependency 'rest-client', '~> 2.0', '>= 2.0.0'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.6'
27
+ spec.add_development_dependency 'rake', '~> 12.3.0', '>= 12.3.0'
28
+ end
data/lib/direct_api.rb ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'restclient'
4
+ require 'json'
5
+ require 'direct_api/adapters/campaigns_adapter'
6
+ require 'direct_api/adapters/adgroups_adapter'
7
+ require 'direct_api/adapters/ads_adapter'
8
+
9
+ # Yandex Direct API module
10
+ module DirectApi
11
+
12
+ autoload :Response, 'direct_api/response'
13
+ autoload :Request, 'direct_api/request'
14
+ autoload :Session, 'direct_api/session'
15
+
16
+ autoload :ConnectionError, 'direct_api/errors/connection_error'
17
+ autoload :RequestError, 'direct_api/errors/request_error'
18
+
19
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DirectApi
4
+ # AdGroups methods
5
+ class AdgroupsAdapter
6
+
7
+ ADGROUP_FIELD_NAMES = %w[Id CampaignId Status Name RegionIds
8
+ NegativeKeywords TrackingParams Type].freeze
9
+ MOBILE_APP_ADGROUP_FIELD_NAMES = %w[StoreUrl TargetDeviceType TargetCarrier
10
+ TargetOperatingSystemVersion AppIconModeration
11
+ AppAvailabilityStatus AppOperatingSystemType].freeze
12
+ DYNAMIC_TEXT_ADGROUP_FIELD_NAMES = %w[DomainUrl].freeze
13
+
14
+ def build_request(client_settings, params = {})
15
+ [:adgroups, build(params).merge(client_settings)]
16
+ end
17
+
18
+ private
19
+
20
+ def build(options = {})
21
+ {
22
+ method: :get,
23
+ params: {
24
+ SelectionCriteria: options[:params],
25
+ FieldNames: options.fetch(:field_names, ADGROUP_FIELD_NAMES),
26
+ MobileAppAdGroupFieldNames: options.fetch(:mobile_app_adgroup_field_names,
27
+ MOBILE_APP_ADGROUP_FIELD_NAMES),
28
+ DynamicTextAdGroupFieldNames: options.fetch(:dynamic_text_adgroup_field_names,
29
+ DYNAMIC_TEXT_ADGROUP_FIELD_NAMES)
30
+ }
31
+ }
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DirectApi
4
+ # Ads methods
5
+ class AdsAdapter
6
+
7
+ AD_FIELD_NAMES = %w[AdCategories AgeLabel AdGroupId CampaignId Id State Status
8
+ StatusClarification Type Subtype].freeze
9
+ TEXT_AD_FIELD_NAMES = %w[Title Text Href Mobile DisplayDomain DisplayUrlPath
10
+ DisplayUrlPathModeration VCardId VCardModeration SitelinkSetId
11
+ SitelinksModeration AdImageHash AdImageModeration AdExtensions].freeze
12
+ MOBILE_APP_AD_FIELD_NAMES = %w[Title Text Features Action AdImageHash
13
+ AdImageModeration TrackingUrl].freeze
14
+ DYNAMIC_TEXT_AD_FIELD_NAMES = %w[Text VCardId VCardModeration SitelinkSetId SitelinksModeration
15
+ AdImageHash AdImageModeration AdExtensions].freeze
16
+ TEXT_IMAGE_AD_FIELD_NAMES = %w[AdImageHash Href].freeze
17
+ MOBILE_APP_IMAGE_AD_FIELD_NAMES = %w[AdImageHash TrackingUrl].freeze
18
+
19
+ def build_request(client_settings, params = {})
20
+ [:ads, build(params).merge(client_settings)]
21
+ end
22
+
23
+ private
24
+
25
+ def build(options = {})
26
+ {
27
+ method: :get,
28
+ params: extract_options(options)
29
+ }
30
+ end
31
+
32
+ def extract_options(p)
33
+ {
34
+ SelectionCriteria: p[:params],
35
+ FieldNames: p.fetch(:field_names, AD_FIELD_NAMES),
36
+ TextAdFieldNames: p.fetch(:text_ad_field_names, TEXT_AD_FIELD_NAMES),
37
+ MobileAppAdFieldNames: p.fetch(:mobile_app_ad_field_names, MOBILE_APP_AD_FIELD_NAMES),
38
+ DynamicTextAdFieldNames: p.fetch(:dynamic_text_ad_field_names, DYNAMIC_TEXT_AD_FIELD_NAMES),
39
+ TextImageAdFieldNames: p.fetch(:text_image_ad_field_names, TEXT_IMAGE_AD_FIELD_NAMES),
40
+ MobileAppImageAdFieldNames:
41
+ p.fetch(:mobile_app_image_ad_field_names, MOBILE_APP_IMAGE_AD_FIELD_NAMES)
42
+ }
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DirectApi
4
+ # Campaigns methods
5
+ class CampaignsAdapter
6
+
7
+ CAMPAIGN_FIELD_NAMES = %w[BlockedIps ExcludedSites Currency DailyBudget Notification EndDate
8
+ Funds ClientInfo Id Name NegativeKeywords RepresentedBy StartDate
9
+ Statistics State Status StatusPayment StatusClarification SourceId
10
+ TimeTargeting TimeZone Type].freeze
11
+ TEXT_CAMPAIGN_FIELD_NAMES = %w[CounterIds RelevantKeywords Settings BiddingStrategy].freeze
12
+ MOBILE_APP_CAMPAIGN_FIELD_NAMES = %w[Settings BiddingStrategy].freeze
13
+ DYNAMIC_TEXT_CAMPAIGN_FIELD_NAMES = %w[CounterIds Settings BiddingStrategy].freeze
14
+
15
+ def build_request(client_settings, params = {})
16
+ [:campaigns, build(params).merge(client_settings)]
17
+ end
18
+
19
+ private
20
+
21
+ def build(options = {})
22
+ {
23
+ method: :get,
24
+ params: extract_options(options)
25
+ }
26
+ end
27
+
28
+ def extract_options(p)
29
+ {
30
+ SelectionCriteria: p[:params],
31
+ FieldNames: p.fetch(:field_names, CAMPAIGN_FIELD_NAMES),
32
+ TextCampaignFieldNames: p.fetch(:text_campaign_field_names, TEXT_CAMPAIGN_FIELD_NAMES),
33
+ MobileAppCampaignFieldNames:
34
+ p.fetch(:mobile_app_campaign_field_names, MOBILE_APP_CAMPAIGN_FIELD_NAMES),
35
+ DynamicTextCampaignFieldNames:
36
+ p.fetch(:dynamic_text_campaign_field_names, DYNAMIC_TEXT_CAMPAIGN_FIELD_NAMES)
37
+ }
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DirectApi
4
+ # Connection Error
5
+ class ConnectionError < StandardError
6
+
7
+ def initialize(e)
8
+ @exception = e
9
+ end
10
+
11
+ def message
12
+ @exception.message
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DirectApi
4
+ # Request Error
5
+ class RequestError < StandardError
6
+
7
+ def initialize(e)
8
+ super build_message e
9
+ end
10
+
11
+ private
12
+
13
+ def build_message(e)
14
+ body = JSON.parse e.response
15
+ if body['error']
16
+ "#{body['error']} : #{body['error_description']}"
17
+ else
18
+ body.map { |field, error| "#{field}: #{error}" }.join(', ')
19
+ end
20
+ rescue
21
+ e.response
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DirectApi
4
+ # Yandex Direct request module
5
+ module Request
6
+
7
+ API_URI = 'https://api.direct.yandex.com/json/v5/'
8
+ SANDBOX_API_URI = 'https://api-sandbox.direct.yandex.com/json/v5/'
9
+
10
+ module_function
11
+
12
+ def process(path, params = {})
13
+ response_parser.parse(
14
+ make_request(path, params).to_s
15
+ )
16
+ end
17
+
18
+ def make_request(path, params = {})
19
+
20
+ RestClient.post(*build(path, params))
21
+ rescue RestClient::Unauthorized,
22
+ RestClient::Forbidden,
23
+ RestClient::BadRequest,
24
+ RestClient::ResourceNotFound => e
25
+ raise DirectApi::RequestError, e.message
26
+ rescue SocketError => e
27
+ raise DirectApi::ConnectionError, e.message
28
+
29
+ end
30
+
31
+ def build(path, params)
32
+ headers = {}
33
+ path = get_uri(params.delete(:sandbox)) + path.to_s
34
+
35
+ headers['Accept-Language'] = params.delete(:locale)
36
+ headers['Client-Login'] = params.delete(:client_login)
37
+ headers['Authorization'] = "Bearer #{params.delete(:token)}"
38
+
39
+ [path, params.to_json, headers]
40
+ end
41
+
42
+ def get_uri(sandbox)
43
+ sandbox ? SANDBOX_API_URI : API_URI
44
+ end
45
+
46
+ def response_parser
47
+ Response
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DirectApi
4
+ # Yandex Direct response parser
5
+ module Response
6
+
7
+ module_function
8
+
9
+ def parse(response)
10
+ if response.is_a?(Hash)
11
+ lower_keys(response)
12
+ elsif response.is_a?(Array)
13
+ response.map { |r| parse(r) }
14
+ elsif response.is_a?(String)
15
+ parse(json_parse(response))
16
+ else
17
+ response
18
+ end
19
+ rescue JSON::ParserError
20
+ response
21
+ end
22
+
23
+ def json_parse(response)
24
+ JSON.parse(response)
25
+ end
26
+
27
+ def lower_keys(h)
28
+ h.each_with_object({}) do |(key, value), memo|
29
+ memo[key.underscore] = parse(value)
30
+ end
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DirectApi
4
+ # Direct Api session object
5
+ class Session
6
+
7
+ def initialize(api_data)
8
+ @api_data = api_data
9
+ end
10
+
11
+ def campaigns(token, client_login, params = {})
12
+ process_request(token, client_login, campaigns_adapter, params)
13
+ end
14
+
15
+ def adgroups(token, client_login, params = {})
16
+ process_request(token, client_login, adgroups_adapter, params)
17
+ end
18
+
19
+ def ads(token, client_login, params = {})
20
+ process_request(token, client_login, ads_adapter, params)
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :api_data
26
+
27
+ def process_request(token, client_login, adapter, params)
28
+ requester.process(*adapter.build_request(client_settings(token, client_login), params))
29
+ end
30
+
31
+ def client_settings(token, client_login)
32
+ {
33
+ token: token,
34
+ client_login: client_login,
35
+ sandbox: api_data[:sandbox],
36
+ locale: api_data[:locale]
37
+ }
38
+ end
39
+
40
+ def campaigns_adapter
41
+ @campaigns_adapter ||= CampaignsAdapter.new
42
+ end
43
+
44
+ def adgroups_adapter
45
+ @adgroups_adapter ||= AdgroupsAdapter.new
46
+ end
47
+
48
+ def ads_adapter
49
+ @ads_adapter ||= AdsAdapter.new
50
+ end
51
+
52
+ def requester
53
+ Request
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DirectApi
4
+
5
+ VERSION = '0.0.1'
6
+
7
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: direct_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Gennady Novoselov
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
+ description: Yandex Direct api via oauth2
88
+ email:
89
+ - gennady@novoselov.biz
90
+ executables: []
91
+ extensions: []
92
+ extra_rdoc_files: []
93
+ files:
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - direct_api.gemspec
100
+ - lib/direct_api.rb
101
+ - lib/direct_api/adapters/adgroups_adapter.rb
102
+ - lib/direct_api/adapters/ads_adapter.rb
103
+ - lib/direct_api/adapters/campaigns_adapter.rb
104
+ - lib/direct_api/errors/connection_error.rb
105
+ - lib/direct_api/errors/request_error.rb
106
+ - lib/direct_api/request.rb
107
+ - lib/direct_api/response.rb
108
+ - lib/direct_api/session.rb
109
+ - lib/direct_api/version.rb
110
+ homepage: https://github.com/gnovoselov/direct-api
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.6.11
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Yandex Direct api via oauth2
134
+ test_files: []