zooming-proxy 0.1.0 → 0.2.0

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: 9ce7fec1ef62580c41fd24d84a9831239006d6dc
4
- data.tar.gz: dd8b3d9f1a65bd359c72dde212b995d00c397e44
3
+ metadata.gz: 6aec5a165d48e311fb815794e281f8598ad51035
4
+ data.tar.gz: 89cd684060e0cd20a72584981f0ffc1f22935bac
5
5
  SHA512:
6
- metadata.gz: 05cb4f51b3b89fd1ab364d0f17bd6656058950daa20f1cd980515a73a107ba4b1439c5f00c816e98f23e771b41672be2f10af88937470fdbbe6e01523f86e4c6
7
- data.tar.gz: ff528c17789806d933e245ddaa4f1039ce8eff2068059a25ef712e476918444dc7b5d640c1dfb53d9ce770f347db6798f6a97385d501c484d2b67353b49289b7
6
+ metadata.gz: e24978b2333fe58ef3dfe5685394d3870b2d87cd05023375190e7feb6921cc0dc41acc0aaf06bc2b38e6c91a419ec630d8ddb37c8f6373a38a84acecf05341a1
7
+ data.tar.gz: befdb7b2aa145c69033d98c86cd0ad33349f483158f8677fb148121b471cabdb250a4fe7adbd6b4d6aadfb6317a998d8c0d98718f832ffd218dcc36b9aa4f20b
@@ -6,3 +6,6 @@ Metrics/LineLength:
6
6
 
7
7
  Metrics/MethodLength:
8
8
  Max: 15
9
+
10
+ Metrics/ClassLength:
11
+ Max: 150
@@ -1,6 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'json'
4
+ require 'typhoeus'
5
+
6
+ Typhoeus::Config.memoize = true
4
7
 
5
8
  module ZoomingProxy
6
9
  # Middleware
@@ -59,25 +62,33 @@ module ZoomingProxy
59
62
 
60
63
  body['_embedded'] ||= {}
61
64
  headers['X-HAL-Zooming'] = depth
65
+ body = parallel_zooms(body, headers, depth)
62
66
 
63
- requested_links(body, headers).each { |link| body = zoom_rel(headers, body, depth, link) }
64
67
  zoomed_response(status, headers, body, depth)
65
68
  end
66
69
 
67
- def zoom_rel(headers, body, depth, link)
68
- relation, hrefs = link
69
- array = hrefs.respond_to?(:to_ary)
70
+ def parallel_zooms(body, headers, depth)
71
+ hydra = Typhoeus::Hydra.new
72
+ requested_links(body, headers).map do |url, attributes|
73
+ request = request_rel(url, headers)
74
+ request.on_complete do |res|
75
+ body = zoom_rel(attributes[:relation], res, body, attributes[:array], depth)
76
+ end
77
+ hydra.queue(request)
78
+ end
79
+ hydra.run
70
80
 
71
- [*hrefs].each do |href|
72
- res = request_rel(href, headers)
73
- embedded_body = handle_nested_embeds(res, depth)
81
+ body
82
+ end
74
83
 
75
- if array
76
- body['_embedded'][relation] ||= []
77
- body['_embedded'][relation] << embedded_body
78
- else
79
- body['_embedded'][relation] = embedded_body
80
- end
84
+ def zoom_rel(relation, response, body, array, depth)
85
+ embedded_body = handle_nested_embeds(response, depth)
86
+
87
+ if array
88
+ body['_embedded'][relation] ||= []
89
+ body['_embedded'][relation] << embedded_body
90
+ else
91
+ body['_embedded'][relation] = embedded_body
81
92
  end
82
93
 
83
94
  body
@@ -88,7 +99,7 @@ module ZoomingProxy
88
99
  uri.scheme = scheme
89
100
  uri.host = host
90
101
 
91
- Typhoeus.get(uri, headers: headers)
102
+ Typhoeus::Request.new(uri, headers: headers)
92
103
  end
93
104
 
94
105
  def response_to_hash(response)
@@ -126,16 +137,14 @@ module ZoomingProxy
126
137
  end
127
138
 
128
139
  def requested_links(body, headers)
129
- links = requested_zooms(headers).select { |rel| body['_links'].key?(rel) }
130
- links.map do |rel|
131
- link = body['_links'][rel]
140
+ body['_links']
141
+ .to_h
142
+ .select { |k, _| headers['x-hal-zoom'].split(/\s+/).include?(k) }
143
+ .each_with_object({}) do |(k, v), urls|
144
+ array = v.respond_to?(:to_ary)
132
145
 
133
- [rel, link.respond_to?(:to_ary) ? link.map { |l| l['href'] } : link['href']]
134
- end
135
- end
136
-
137
- def requested_zooms(headers)
138
- headers['x-hal-zoom'].split(/\s+/)
146
+ [v].flatten.each { |url| urls[url['href']] = { relation: k, array: array } }
147
+ end
139
148
  end
140
149
  end
141
150
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ZoomingProxy
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
@@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.files = `git ls-files -z`.split("\x0")
19
19
  .reject { |f| f.match(%r{^(test|spec|features)/}) }
20
20
 
21
+ spec.add_dependency 'typhoeus', '~> 1.0.2'
22
+
21
23
  spec.add_development_dependency 'bundler', '~> 1.12'
22
24
  spec.add_development_dependency 'cucumber', '~> 2.4'
23
25
  spec.add_development_dependency 'cucumber-blendle-steps', '~> 0.8'
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zooming-proxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blendle developers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-13 00:00:00.000000000 Z
11
+ date: 2016-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: typhoeus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.2
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement