ya-api-direct 0.2.1 → 0.2.2

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: 55bee9a56313eefa9d774f6edc2422328da00766
4
- data.tar.gz: 1fb1aa4fe1bee7e0693ada9645a45014ec340488
3
+ metadata.gz: ebb64ae11e79bffedda4ce6ae5bf6e7eb7cd488c
4
+ data.tar.gz: 689f39880dc909582cafd22cb662c1d489bee15c
5
5
  SHA512:
6
- metadata.gz: 52caccd119c78cc82c6c291403426965a365dbc0444839e7ef1c0423ec604097c6253b9c6297140543d8f3102cd7b34282c53ff30aad691d3b33d35dfb45de35
7
- data.tar.gz: 29faa276241e4cf1fb55b612ce78283a38b70ac5a7fb9a897ef50a174a9a12a0254d79d11436ac264e8b60d94f358a41441c49ad77a09a5fb16dd9e1e8f09fd6
6
+ metadata.gz: 02b61c8fc58852a3ba0b37f449e7361981581d59814169e76d27db0b974cddb5a795234f86da5102794bb23dfe67b646431c3cac8785378514f41c64449d7120
7
+ data.tar.gz: 9c6e71af381828cc106ebc7d5c6ae04ed032145e0097be62a6868c4c21bf4896e5dd61781175912ed6e97a016cb01b6841da7e9fbed51411c23c6ffc544aae8e
data/README.md CHANGED
@@ -43,7 +43,7 @@ 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.
@@ -74,10 +74,13 @@ Date returned with first call of caching method is stored in ``cache_timestamp``
74
74
  ## Units data
75
75
 
76
76
  Units data of last request is stored in ``units_data`` property. There're 3 keys in this hash:
77
+
77
78
  * ``just_used``
78
79
  * ``units_left``
79
80
  * ``units_limit``
80
81
 
82
+ They were added only in Yandex Direct API 5. Calling the methods from 4 or 4 Live doesn't update them.
83
+
81
84
  ## Useful links
82
85
 
83
86
  * [Intruduction to Yandex Direct API](https://yandex.ru/adv/edu/direct-api)
data/Rakefile CHANGED
@@ -2,7 +2,8 @@ require "bundler/gem_tasks"
2
2
  require "rake/testtask"
3
3
 
4
4
  task :spec do
5
- Dir.glob('./spec/spec_*.rb').each { |file| require file}
5
+ Dir.glob('./spec/**/spec_*.rb').each { |file| require file}
6
6
  end
7
7
 
8
+ task test: [:spec]
8
9
  task default: [:spec]
data/bin/console CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
-
2
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
3
3
  require "bundler/setup"
4
4
  require "ya/api/direct"
5
5
 
@@ -4,6 +4,8 @@ require "ya/api/direct/direct_service_v4"
4
4
  require "ya/api/direct/direct_service_v5"
5
5
  require "ya/api/direct/exception"
6
6
 
7
+ require 'time'
8
+
7
9
  module Ya::API::Direct
8
10
  AllowedAPIVersions = [:v5, :v4]
9
11
 
@@ -66,9 +68,9 @@ module Ya::API::Direct
66
68
  when :v5
67
69
  result = @gateway.request("checkDictionaries", {}, "changes", :v5)
68
70
  timestamp = result[:data]['result']['Timestamp']
71
+ update_units_data result[:units_data]
69
72
  end
70
73
  @cache_timestamp = Time.parse(timestamp)
71
- update_units_data result[:units_data]
72
74
  @cache_timestamp
73
75
  end
74
76
 
@@ -10,19 +10,21 @@ module Ya::API::Direct
10
10
  end
11
11
 
12
12
  protected
13
-
14
- def exec_request(method, request_body)
15
- client.gateway.request method, request_body, @version
16
- end
17
-
18
13
  def init_methods
19
14
  @method_items.each do |method|
20
15
  self.class.send :define_method, method do |params = {}|
21
16
  result = exec_request(method, params || {})
22
- @client.update_units_data result[:units_data]
17
+ callback_by_result result
23
18
  result[:data]
24
19
  end
25
20
  end
26
21
  end
22
+
23
+ def exec_request(method, request_body)
24
+ client.gateway.request method, request_body, @version
25
+ end
26
+
27
+ def callback_by_result(result={})
28
+ end
27
29
  end
28
30
  end
@@ -15,5 +15,11 @@ module Ya::API::Direct
15
15
  def exec_request(method, request_body={})
16
16
  @client.gateway.request method, request_body, @service_url, @version
17
17
  end
18
+
19
+ def callback_by_result(result={})
20
+ if result.has_key? :units_data
21
+ @client.update_units_data result[:units_data]
22
+ end
23
+ end
18
24
  end
19
25
  end
@@ -25,7 +25,7 @@ module Ya::API::Direct
25
25
  http.verify_mode = @config[:ssl] ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
26
26
  response = http.request(request)
27
27
  if response.kind_of? Net::HTTPSuccess
28
- UrlHelper.parse_data response
28
+ UrlHelper.parse_data response, ver
29
29
  else
30
30
  raise response.inspect
31
31
  end
@@ -54,6 +54,7 @@ module Ya::API::Direct
54
54
  # @return [Hash] Units data, extracted from header
55
55
  def self.extract_response_units(response_header)
56
56
  matched = RegExUnits.match response_header["Units"]
57
+ matched.nil? ? {} :
57
58
  {
58
59
  just_used: matched[1].to_i,
59
60
  units_left: matched[2].to_i,
@@ -63,13 +64,14 @@ module Ya::API::Direct
63
64
 
64
65
  private
65
66
 
66
- def self.parse_data(response)
67
+ def self.parse_data(response, ver)
67
68
  response_body = JSON.parse(response.body)
68
69
  validate_response! response_body
69
- {
70
- data: response_body,
71
- units_data: self.extract_response_units(response)
72
- }
70
+ result = { data: response_body }
71
+ if [:v5].include? ver
72
+ result.merge!({ units_data: self.extract_response_units(response) })
73
+ end
74
+ result
73
75
  end
74
76
 
75
77
  def self.validate_response!(response_body)
@@ -1,7 +1,7 @@
1
1
  module Ya
2
2
  module Api
3
3
  module Direct
4
- VERSION = "0.2.1"
4
+ VERSION = "0.2.2"
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ya-api-direct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - '"Rikki'