esi 0.2.4 → 0.2.5
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 +4 -4
- data/lib/esi/client.rb +15 -8
- data/lib/esi/response.rb +18 -17
- data/lib/esi/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: ff611fd04267a5b4e29486f6d69f0f3894a23cd9
|
4
|
+
data.tar.gz: 6449c39160dbe9d1f35098e07a1167be92adf969
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0feccab7a6b84be7c6d1805c45e0d87c7dc7c3f121466ec87fdd98d91a33d56fe2f124b61f14b088d7224db74839b574cfa0eda40c930002915f5ac788e37f89
|
7
|
+
data.tar.gz: c6237a76a7d66b8e20d272526d50ade2c0324de9b996aaea60610ad319ec317d63a4ae904aba2146fb704aa3f1b40ea4254839772ddbfd488ad366ae5b6aa5d6
|
data/lib/esi/client.rb
CHANGED
@@ -53,23 +53,30 @@ module Esi
|
|
53
53
|
end
|
54
54
|
|
55
55
|
def request_paginated(call, &block)
|
56
|
-
|
56
|
+
response = nil
|
57
57
|
page = 1
|
58
58
|
|
59
59
|
ActiveSupport::Notifications.instrument('esi.client.request.paginated') do
|
60
60
|
loop do
|
61
61
|
call.page = page
|
62
|
-
|
63
|
-
|
64
|
-
|
62
|
+
page_response = request(call, &block)
|
63
|
+
|
64
|
+
if page_response.data.blank?
|
65
|
+
break
|
66
|
+
elsif response
|
67
|
+
response.merge(page_response)
|
68
|
+
else
|
69
|
+
response = page_response
|
70
|
+
end
|
71
|
+
|
65
72
|
page += 1
|
66
73
|
end
|
67
74
|
end
|
68
75
|
|
69
|
-
|
76
|
+
response
|
70
77
|
end
|
71
78
|
|
72
|
-
def request(call,
|
79
|
+
def request(call, &block)
|
73
80
|
response = nil
|
74
81
|
last_ex = nil
|
75
82
|
options = { timeout: Esi.config.timeout }
|
@@ -128,10 +135,10 @@ module Esi
|
|
128
135
|
if last_ex
|
129
136
|
logger.error "Request failed with #{last_ex.class}"
|
130
137
|
raise ApiRequestError.new(last_ex)
|
131
|
-
else
|
132
|
-
debug "Request successful"
|
133
138
|
end
|
134
139
|
|
140
|
+
debug "Request successful"
|
141
|
+
|
135
142
|
ActiveSupport::Notifications.instrument('esi.client.response.initialize') do
|
136
143
|
response = Response.new(response)
|
137
144
|
end
|
data/lib/esi/response.rb
CHANGED
@@ -4,32 +4,30 @@ module Esi
|
|
4
4
|
class Response
|
5
5
|
extend Forwardable
|
6
6
|
|
7
|
-
attr_reader :original_response, :
|
7
|
+
attr_reader :original_response, :data
|
8
8
|
def_delegators :original_response, :status, :body, :headers
|
9
9
|
def_delegators :data, :each
|
10
10
|
|
11
11
|
def initialize(response)
|
12
12
|
@original_response = response
|
13
|
-
@
|
14
|
-
@json = normalize_keys(@json) if @json.is_a?(Hash)
|
13
|
+
@data = normalize_results
|
15
14
|
end
|
16
15
|
|
17
|
-
def
|
18
|
-
@data
|
19
|
-
|
20
|
-
@json[0].is_a?(Hash) ? @json.map { |e| RecursiveOpenStruct.new(e, recurse_over_arrays: true) } : @json
|
21
|
-
else
|
22
|
-
RecursiveOpenStruct.new(@json, recurse_over_arrays: true)
|
23
|
-
end
|
24
|
-
end
|
16
|
+
def merge(other_response)
|
17
|
+
@data += other_response.data
|
18
|
+
self
|
25
19
|
end
|
26
20
|
|
27
|
-
def
|
28
|
-
MultiJson.dump(
|
21
|
+
def to_json(pretty: true)
|
22
|
+
MultiJson.dump(data, pretty: pretty)
|
29
23
|
end
|
30
24
|
|
31
25
|
def cached_until
|
32
|
-
|
26
|
+
@cached_until ||= headers[:expires] ? Time.parse(headers[:expires]) : nil
|
27
|
+
end
|
28
|
+
|
29
|
+
def response_json
|
30
|
+
@response_json ||= MultiJson.load(body, symbolize_keys: true) rescue {}
|
33
31
|
end
|
34
32
|
|
35
33
|
def method_missing(method, *args, &block)
|
@@ -42,9 +40,12 @@ module Esi
|
|
42
40
|
|
43
41
|
private
|
44
42
|
|
45
|
-
def
|
46
|
-
|
47
|
-
|
43
|
+
def normalize_results
|
44
|
+
response_json.is_a?(Hash) ? normalize_entry(response_json) : response_json.map { |e| normalize_entry(e) }
|
45
|
+
end
|
46
|
+
|
47
|
+
def normalize_entry(entry)
|
48
|
+
entry.is_a?(Hash) ? RecursiveOpenStruct.new(entry.transform_keys { |k| underscore(k).to_sym }, recurse_over_arrays: true) : entry
|
48
49
|
end
|
49
50
|
|
50
51
|
def underscore(str)
|
data/lib/esi/version.rb
CHANGED