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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c1d7139866bf6408889132991fc7a0de63bdd024
4
- data.tar.gz: 7876fa78a888c60ce74628b9e9f7e1fa19739b90
3
+ metadata.gz: ff611fd04267a5b4e29486f6d69f0f3894a23cd9
4
+ data.tar.gz: 6449c39160dbe9d1f35098e07a1167be92adf969
5
5
  SHA512:
6
- metadata.gz: 2e775911db17e7679f86a21b526bd53bae9402c341258efb34f6691766fa027b03fdd3a8068ca73afeb243c958efaef927ebe28b4821de49d2597a5d379f35ea
7
- data.tar.gz: 437dac54b79c67420cd0ff20dbd2294e642996c8549860970d0555b0bd01f6b65ffe14ec171b721919293dc37218e3459b19c9b20773220159efd37f7b026bef
6
+ metadata.gz: 0feccab7a6b84be7c6d1805c45e0d87c7dc7c3f121466ec87fdd98d91a33d56fe2f124b61f14b088d7224db74839b574cfa0eda40c930002915f5ac788e37f89
7
+ data.tar.gz: c6237a76a7d66b8e20d272526d50ade2c0324de9b996aaea60610ad319ec317d63a4ae904aba2146fb704aa3f1b40ea4254839772ddbfd488ad366ae5b6aa5d6
@@ -53,23 +53,30 @@ module Esi
53
53
  end
54
54
 
55
55
  def request_paginated(call, &block)
56
- items = []
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
- response = request(call, &block)
63
- break if response.data.blank?
64
- items += response.data
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
- items
76
+ response
70
77
  end
71
78
 
72
- def request(call, url=nil, &block)
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
@@ -4,32 +4,30 @@ module Esi
4
4
  class Response
5
5
  extend Forwardable
6
6
 
7
- attr_reader :original_response, :json
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
- @json = MultiJson.load(body, symbolize_keys: true) rescue {}
14
- @json = normalize_keys(@json) if @json.is_a?(Hash)
13
+ @data = normalize_results
15
14
  end
16
15
 
17
- def data
18
- @data ||= begin
19
- if @json.is_a?(Array)
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 to_s
28
- MultiJson.dump(json, pretty: true)
21
+ def to_json(pretty: true)
22
+ MultiJson.dump(data, pretty: pretty)
29
23
  end
30
24
 
31
25
  def cached_until
32
- original_response.headers[:expires] ? Time.parse(original_response.headers[:expires]) : nil
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 normalize_keys(data)
46
- data.dup.each_key { |k| data[underscore(k).to_sym] = data.delete(k) }
47
- data
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)
@@ -1,3 +1,3 @@
1
1
  module Esi
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: esi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Hiemstra