ya-api-direct 0.2.5 → 0.2.10

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
- SHA1:
3
- metadata.gz: dc5086aa88e50438f6a110d4c2cffdc090a5b3d6
4
- data.tar.gz: b97cce9be358c5936070625f8a3c0662b0ecd42f
2
+ SHA256:
3
+ metadata.gz: c3834e0a678f9eb91e7523dbca830f99d72660940079dfcc9340082d64ab2ec7
4
+ data.tar.gz: bf72e62469f98178a4a1ee62fe1c27849d8f23826db06c07354b87aaec6c9b47
5
5
  SHA512:
6
- metadata.gz: 933afe225577e340ef49b21e6d47f94b3b6965c799bb3932c48196b1683407488e4fc11bedb967cb9c6a3bd12be90ad216150f252514b886e6d7952c721552bf
7
- data.tar.gz: 2a7e97c13b7b0f046ba061b8dced54eec9c7539904f88a285d0e276ab75c102c1300c45146df2439eccd4dd8d06c371ca9bfc8896a217537c99d3778a35f5ec8
6
+ metadata.gz: fbd2d4b2b37ef73cf0b8269de00dd00a91b289948dea5285ca43172a26e5a3570664aa78b907f985c75962e617f6c5e08dd6812492ad261506e49e53151f9e45
7
+ data.tar.gz: c56f376feb05d6b7ccdc3d5d54e78e46773ba8defa047484a6a82b860bcfd7d184672c46a34b636527b13854087ec20da0f239de0956f31c2024b8ce7bf422d6
data/Gemfile CHANGED
@@ -1,8 +1,9 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gem 'jruby-openssl', platforms: :jruby
4
- gem 'rake'
4
+ gem 'rake', '>= 12.3.3'
5
5
  gem 'yard'
6
+ #gem 'tsv'
6
7
 
7
8
  group :development do
8
9
  # reserved
@@ -15,4 +16,4 @@ group :test do
15
16
  gem 'yardstick'
16
17
  end
17
18
 
18
- gemspec
19
+ gemspec
data/README.md CHANGED
@@ -36,14 +36,14 @@ request = {
36
36
 
37
37
  options = { token: Token }
38
38
  @direct = Ya::API::Direct::Client.new(options)
39
- json = direct.campaigns.get request
39
+ json = @direct.campaigns.get request
40
40
  ```
41
41
 
42
42
  Call methods from versions 4 and 4 Live:
43
43
 
44
44
  ```ruby
45
45
  @direct = Ya::API::Direct::Client.new({ token: Token })
46
- json = direct.v4.GetCampaignsList
46
+ json = @direct.v4.GetCampaignsList
47
47
  ```
48
48
 
49
49
  All names of controllers and methods are equal to ones from Direct API help. 4 vs 4 Live is autodetected.
@@ -2,7 +2,7 @@ require "ya/api/direct/constants"
2
2
  require "ya/api/direct/gateway"
3
3
  require "ya/api/direct/direct_service_v4"
4
4
  require "ya/api/direct/direct_service_v5"
5
- require "ya/api/direct/exception"
5
+ require "ya/api/direct/exceptions"
6
6
 
7
7
  require 'time'
8
8
 
@@ -14,7 +14,7 @@ module Ya::API::Direct
14
14
  class Client
15
15
  attr_reader :cache_timestamp, :units_data, :gateway,
16
16
  :v4, :v5
17
-
17
+
18
18
  # Initializes a new Client object
19
19
  #
20
20
  # @param config [Hash]
@@ -75,7 +75,7 @@ module Ya::API::Direct
75
75
  end
76
76
 
77
77
  private
78
-
78
+
79
79
  def init_v4
80
80
  @v4 = DirectServiceV4.new self, (API_V4 + API_V4_LIVE)
81
81
  end
@@ -90,4 +90,4 @@ module Ya::API::Direct
90
90
  end
91
91
  end
92
92
  end
93
- end
93
+ end
@@ -2,44 +2,90 @@ module Ya
2
2
  module API
3
3
  module Direct
4
4
  API_V5 = {
5
- "Campaigns" => [
6
- "add", "update", "delete", "suspend", "resume", "archive", "unarchive", "get"
7
- ],
8
5
  "AdGroups" => [
9
- "add", "update", "delete", "get"
6
+ "add", "update", "delete", "get"
7
+ ],
8
+ "AgencyClients" => [
9
+ "add", "update", "get"
10
10
  ],
11
11
  "Ads" => [
12
12
  "add", "update", "delete", "suspend", "resume", "archive", "unarchive", "moderate", "get"
13
13
  ],
14
+ "AdExtensions" => [
15
+ "add", "delete", "get"
16
+ ],
17
+ "AdImages" => [
18
+ "add", "delete", "get"
19
+ ],
20
+ "AudienceTargets" => [
21
+ "add", "delete", "suspend", "resume", "get", "setBids"
22
+ ],
14
23
  "Bids" => [
15
24
  "set", "setAuto", "get"
16
25
  ],
17
26
  "BidModifiers" => [
18
- "add", "delete", "get", "set", "toggle"
19
- ],
27
+ "add", "delete", "get", "set", "toggle"
28
+ ],
29
+ "Businesses" => [
30
+ "get"
31
+ ],
20
32
  "Campaigns" => [
21
33
  "add", "update", "delete", "suspend", "resume", "archive", "unarchive", "get"
22
- ],
34
+ ],
23
35
  "Changes" => [
24
36
  "checkDictionaries", "checkCampaigns", "check"
25
- ],
37
+ ],
38
+ "Clients" => [
39
+ "get", "update"
40
+ ],
41
+ "Creatives" => [
42
+ "get"
43
+ ],
26
44
  "Dictionaries" => [
27
- "get"
45
+ "get"
46
+ ],
47
+ "DynamicFeedAdTargets" => [
48
+ "add", "delete", "suspend", "resume", "get", "setBids"
28
49
  ],
29
50
  "DynamicTextAdTargets" => [
30
51
  "add", "delete", "suspend", "resume", "get", "setBids"
31
52
  ],
53
+ "Feeds" => [
54
+ "add", "delete", "get", "update"
55
+ ],
32
56
  "Keywords" => [
33
57
  "add", "update", "delete", "suspend", "resume", "get"
34
58
  ],
59
+ "KeywordBids" => [
60
+ "set", "setAuto", "get"
61
+ ],
62
+ "KeywordsResearch" => [
63
+ "hasSearchVolume", "deduplicate"
64
+ ],
65
+ "Leads" => [
66
+ "get"
67
+ ],
68
+ "NegativeKeywordSharedSets" => [
69
+ "add", "update", "delete", "get"
70
+ ],
71
+ "RetargetingLists" => [
72
+ "add", "update", "delete", "get"
73
+ ],
35
74
  "Sitelinks" => [
36
75
  "add", "get", "delete"
37
76
  ],
77
+ "SmartAdTargets" => [
78
+ "add", "update", "setBids", "delete", "suspend", "resume", "get"
79
+ ],
80
+ "TurboPages" => [
81
+ "get"
82
+ ],
38
83
  "VCards" => [
39
84
  "add", "get", "delete"
40
- ]
85
+ ],
86
+ "Reports" => ["get"]
41
87
  }
42
-
88
+
43
89
  API_V4 = [
44
90
  "GetBalance",
45
91
  "GetSummaryStat",
@@ -68,7 +114,7 @@ module Ya
68
114
  "GetSubClients",
69
115
  "GetClientInfo",
70
116
  "UpdateClientInfo",
71
-
117
+
72
118
  "CreateNewSubclient",
73
119
  "GetClientsList",
74
120
 
@@ -86,6 +132,7 @@ module Ya
86
132
  API_V4_LIVE = [
87
133
  "CreateOrUpdateCampaign",
88
134
  "GetCampaignsList",
135
+ "GetSummaryStat",
89
136
  "GetCampaignsListFilter",
90
137
  "GetCampaignsParams",
91
138
  "DeleteCampaign",
@@ -133,4 +180,4 @@ module Ya
133
180
  ]
134
181
  end
135
182
  end
136
- end
183
+ end
@@ -0,0 +1,33 @@
1
+ require "ya/api/direct/constants"
2
+
3
+ module Ya::API::Direct
4
+ class Exception < StandardError
5
+ attr_reader :error
6
+
7
+ def initialize( error )
8
+ @error = error
9
+ end
10
+
11
+ def to_s
12
+ [ @error['error_code'], @error['error_string'], @error['error_detail'], @error['request_id'] ].join(' : ')
13
+ end
14
+ end
15
+
16
+ class RequestError < StandardError
17
+ attr_reader :method, :version, :request, :response
18
+
19
+ def initialize( method: , version: , request: , response: )
20
+ @method = method
21
+ @version = version
22
+ @request = request
23
+ @response = response
24
+ end
25
+
26
+ def to_s
27
+ if response.body && ['text/json', 'application/json'].include?( response["Content-Type"] ) && error = JSON.parse( response.body )['error']
28
+ err_msg = error.values_at('error_code', 'error_string', 'error_detail', 'request_id').reject(&:blank?).join(' : ')
29
+ end
30
+ [ ['HTTP ', response.code].join, err_msg ].compact.join(': ')
31
+ end
32
+ end
33
+ end
@@ -4,10 +4,11 @@ require "json"
4
4
 
5
5
  require "ya/api/direct/constants"
6
6
  require "ya/api/direct/url_helper"
7
+ require "ya/api/direct/exceptions"
7
8
 
8
9
  module Ya::API::Direct
9
10
  class Gateway
10
- attr_reader :config
11
+ attr_reader :config, :response
11
12
  def initialize(config)
12
13
  @config = config
13
14
  end
@@ -23,11 +24,25 @@ module Ya::API::Direct
23
24
  http = Net::HTTP.new(uri.host, uri.port)
24
25
  http.use_ssl = true
25
26
  http.verify_mode = @config[:ssl] ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
26
- response = http.request(request)
27
+
28
+ begin
29
+ response = http.request(request)
30
+ if (response.kind_of? Net::HTTPCreated) || (response.kind_of? Net::HTTPAccepted)
31
+ sleep response['retryIn'].to_i
32
+ end
33
+ end while(response.kind_of? Net::HTTPCreated) || (response.kind_of? Net::HTTPAccepted)
34
+
35
+ @response = response
36
+
27
37
  if response.kind_of? Net::HTTPSuccess
28
38
  UrlHelper.parse_data response, ver
29
39
  else
30
- raise response.inspect
40
+ raise RequestError.new(
41
+ method: method,
42
+ version: ver,
43
+ request: request,
44
+ response: response
45
+ )
31
46
  end
32
47
  end
33
48
 
@@ -43,9 +58,10 @@ module Ya::API::Direct
43
58
  end
44
59
 
45
60
  def generate_body(method, params, ver)
61
+ params_key = [:v4, :v4live].include?(ver) ? 'param' : 'params'
46
62
  body = {
47
- "method" => method,
48
- "params" => params
63
+ 'method' => method,
64
+ params_key => params
49
65
  }
50
66
  if [:v4, :v4live].include? ver
51
67
  body.merge!({
@@ -56,4 +72,4 @@ module Ya::API::Direct
56
72
  body
57
73
  end
58
74
  end
59
- end
75
+ end
@@ -1,6 +1,7 @@
1
1
  require "json"
2
2
 
3
- require "ya/api/direct/exception"
3
+ require "ya/api/direct/exceptions"
4
+
4
5
 
5
6
  module Ya::API::Direct
6
7
  RegExUnits = Regexp.new /(\d+)\/(\d+)\/(\d+)/
@@ -9,7 +10,7 @@ module Ya::API::Direct
9
10
  # @author Rikki Mongoose
10
11
  class UrlHelper
11
12
  # Generate Yandex Direct API url for a call
12
- #
13
+ #
13
14
  # @param mode [Symbol] Direct API mode, `:sandbox` or `:production`
14
15
  # @param mode [Symbol] Direct API version, `:v5` or `:v4` or `:v4live`
15
16
  # @param mode [Symbol] Direct API service to use
@@ -45,10 +46,11 @@ module Ya::API::Direct
45
46
  site: site,
46
47
  service: service
47
48
  }
49
+
48
50
  end
49
51
 
50
52
  # Extract Yandex Direct API units values from responce HTTP header
51
- #
53
+ #
52
54
  # @param responce_header [Hash] Yandex Direct API response header
53
55
  # @return [Hash] Units data, extracted from header
54
56
  def self.extract_response_units(response_header)
@@ -64,20 +66,38 @@ module Ya::API::Direct
64
66
  private
65
67
 
66
68
  def self.parse_data(response, ver)
67
- response_body = JSON.parse(response.body)
68
- validate_response! response_body
69
- result = { data: response_body }
70
- if [:v5].include? ver
71
- result.merge!({ units_data: self.extract_response_units(response) })
69
+ if response["Content-Type"].start_with?("application/json")
70
+ response_body = JSON.parse(response.body)
71
+ validate_response! response_body
72
+ result = { data: response_body }
73
+ if [:v5].include? ver
74
+ result.merge!({ units_data: self.extract_response_units(response) })
75
+ end
76
+ result
77
+ else
78
+ {data: from_tsv_to_json(response.body)}
72
79
  end
73
- result
74
80
  end
75
81
 
76
82
  def self.validate_response!(response_body)
77
83
  if response_body.has_key? 'error'
78
- response_error = response_body['error']
79
- raise Exception.new(response_error['error_detail'], response_error['error_string'], response_error['error_code'])
84
+ raise Exception.new( response_body['error'] )
85
+ end
86
+ end
87
+
88
+ def self.from_tsv_to_json(response_body)
89
+ report_name = response_body.slice!(/^(.+?)\n/)
90
+ keys = response_body.slice!(/^(.+?)\n/).split("\t")
91
+ rows = response_body.match(/rows:(.+?)\n/)[1].to_i
92
+ values = []
93
+ rows.times do |row|
94
+ hash_values = {}
95
+ response_body.slice!(/^(.+?)\n/).split("\t").each_with_index do |value, index|
96
+ hash_values[keys[index].gsub("\n", "")] = value.gsub("\n", "")
97
+ end
98
+ values.push(hash_values)
80
99
  end
100
+ values
81
101
  end
82
102
  end
83
- end
103
+ end
@@ -1,7 +1,7 @@
1
1
  module Ya
2
2
  module Api
3
3
  module Direct
4
- VERSION = "0.2.5"
4
+ VERSION = "0.2.10"
5
5
  end
6
6
  end
7
7
  end
data/lib/ya/api/direct.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  require "ya/api/direct/version"
2
2
  require "ya/api/direct/client"
3
- require "ya/api/direct/exception"
3
+ require "ya/api/direct/exceptions"
@@ -6,7 +6,7 @@ require 'ya/api/direct/version'
6
6
  Gem::Specification.new do |s|
7
7
  s.name = 'ya-api-direct'
8
8
  s.version = Ya::Api::Direct::VERSION
9
- s.date = '2016-10-02'
9
+ s.date = '2021-12-12'
10
10
  s.summary = "Yandex Direct API v5, v4 and v4 Live"
11
11
  s.description = "Ruby implementation for Yandex Direct API of versions 5, 4 and 4 Live"
12
12
  s.authors = "Rikki Mongoose"
@@ -18,8 +18,9 @@ Gem::Specification.new do |s|
18
18
  s.bindir = "exe"
19
19
  s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
+ s.required_ruby_version = ">= 2.5.0"
21
22
 
22
- s.add_development_dependency "bundler", "~> 1.12"
23
- s.add_development_dependency "rake", "~> 10.0"
23
+ s.add_development_dependency "bundler", ">= 2.2.33"
24
+ s.add_development_dependency "rake", ">= 12.3.3"
24
25
  s.add_development_dependency "minitest", "~> 5.0"
25
- end
26
+ end
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ya-api-direct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rikki Mongoose
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-02 00:00:00.000000000 Z
11
+ date: 2021-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.12'
19
+ version: 2.2.33
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.12'
26
+ version: 2.2.33
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: 12.3.3
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: 12.3.3
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '5.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '5.0'
55
55
  description: Ruby implementation for Yandex Direct API of versions 5, 4 and 4 Live
@@ -58,9 +58,9 @@ executables: []
58
58
  extensions: []
59
59
  extra_rdoc_files: []
60
60
  files:
61
- - .gitignore
62
- - .rspec
63
- - .travis.yml
61
+ - ".gitignore"
62
+ - ".rspec"
63
+ - ".travis.yml"
64
64
  - CODE_OF_CONDUCT.md
65
65
  - Gemfile
66
66
  - LICENSE
@@ -74,7 +74,7 @@ files:
74
74
  - lib/ya/api/direct/direct_service_base.rb
75
75
  - lib/ya/api/direct/direct_service_v4.rb
76
76
  - lib/ya/api/direct/direct_service_v5.rb
77
- - lib/ya/api/direct/exception.rb
77
+ - lib/ya/api/direct/exceptions.rb
78
78
  - lib/ya/api/direct/gateway.rb
79
79
  - lib/ya/api/direct/url_helper.rb
80
80
  - lib/ya/api/direct/version.rb
@@ -89,17 +89,16 @@ require_paths:
89
89
  - lib
90
90
  required_ruby_version: !ruby/object:Gem::Requirement
91
91
  requirements:
92
- - - '>='
92
+ - - ">="
93
93
  - !ruby/object:Gem::Version
94
- version: '0'
94
+ version: 2.5.0
95
95
  required_rubygems_version: !ruby/object:Gem::Requirement
96
96
  requirements:
97
- - - '>='
97
+ - - ">="
98
98
  - !ruby/object:Gem::Version
99
99
  version: '0'
100
100
  requirements: []
101
- rubyforge_project:
102
- rubygems_version: 2.0.14.1
101
+ rubygems_version: 3.1.2
103
102
  signing_key:
104
103
  specification_version: 4
105
104
  summary: Yandex Direct API v5, v4 and v4 Live
@@ -1,17 +0,0 @@
1
- require "ya/api/direct/constants"
2
-
3
- module Ya::API::Direct
4
- class Exception < StandardError
5
- attr_reader :error_detail, :error_code, :error_str
6
-
7
- def initialize(error_detail, error_str, error_code)
8
- @error_detail = error_detail
9
- @error_str = error_str
10
- @error_code = error_code
11
- end
12
-
13
- def to_s
14
- "#{@error_str} : #{@error_detail} : #{@error_code}"
15
- end
16
- end
17
- end