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 +4 -4
- data/README.md +4 -1
- data/Rakefile +2 -1
- data/bin/console +1 -1
- data/lib/ya/api/direct/client.rb +3 -1
- data/lib/ya/api/direct/direct_service_base.rb +8 -6
- data/lib/ya/api/direct/direct_service_v5.rb +6 -0
- data/lib/ya/api/direct/gateway.rb +1 -1
- data/lib/ya/api/direct/url_helper.rb +7 -5
- data/lib/ya/api/direct/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ebb64ae11e79bffedda4ce6ae5bf6e7eb7cd488c
|
4
|
+
data.tar.gz: 689f39880dc909582cafd22cb662c1d489bee15c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
data/bin/console
CHANGED
data/lib/ya/api/direct/client.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
71
|
-
|
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)
|