hoodoo 2.4.3 → 2.5.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
  SHA256:
3
- metadata.gz: ce758beb5008e4badbf7b78e912ed1a3fc09e5659f3a1dd585ad13f69571d568
4
- data.tar.gz: 76573396fd9b41de0098b8c511ce5601137d289f751cac9597c91d1bc13ea1ac
3
+ metadata.gz: 82cf349eadbadcea2eaeb98421fb6a9e8b35dd5b0f037b2089ff553d99b650d0
4
+ data.tar.gz: 20564a912fb0e68505cb59721b081a61a64227b9676343180b117bcc9246a982
5
5
  SHA512:
6
- metadata.gz: 19d0366a7da0aaa8c228d4aa1a96f2bd5701bdb9ea70a56c159bca77b959f2705c16b2b803fc3e894f6eeb24d24acb4d63bc0c2b84118673b121f46daf21de38
7
- data.tar.gz: 25853720d4851bdd5c992c40645385fd146cfbb0033d17925b6216c4ff97d6fc1d7b9e4244c323ef0696d21e139c2a30c5116e2315e20a179b952803bccadd26
6
+ metadata.gz: 555bb645bc07d3b4db3ff5341683ea98d5dfeea6a7dc1347723f892a36171da59f6faccb36e4a75e8a4b950c62c173b969e435e3eb8c37bb58319b5753be442c
7
+ data.tar.gz: 7f4c18b64388d17816227885ba6078d0ddc6fd009ede81e00b76e799c42e0e93b2033eec1e0a9bfb5417449cee5869fa0fc6b78da100873d6d2a00e3f50f92cd
@@ -63,12 +63,12 @@ module Hoodoo
63
63
  # implementation of such a Resource interface available! - as follows:
64
64
  #
65
65
  # results = members.list(
66
- # :offset => 50,
67
- # :limit => 25,
68
- # :sort => :created_at,
69
- # :direction => :asc,
70
- # :search => { :surname => 'Smith' },
71
- # :_embed => 'account'
66
+ # offset: 50,
67
+ # limit: 25,
68
+ # sort: 'created_at',
69
+ # direction: 'asc',
70
+ # search: { 'surname' => 'Smith' },
71
+ # _embed: 'account'
72
72
  # )
73
73
  #
74
74
  # This will return a Hoodoo::Client::AugmentedArray. This is an Array
@@ -189,7 +189,7 @@ module Hoodoo
189
189
  # +query_hash+:: A hash of _unencoded_ data that can be encoded to form
190
190
  # a query string. Search and filter data is represented
191
191
  # with nested hashes. Embed and reference data uses an
192
- # array. Example:
192
+ # array. All keys are Strings. Example:
193
193
  #
194
194
  # {
195
195
  # offset: 75,
@@ -328,13 +328,13 @@ module Hoodoo
328
328
  # Returns the +augmented_array+ passed in, with the +next_page_proc+
329
329
  # value set.
330
330
  #
331
- def inject_enumeration_state ( augmented_array, query_hash )
331
+ def inject_enumeration_state( augmented_array, query_hash )
332
332
 
333
333
  endpoint = self
334
334
  query_hash = query_hash.nil? ? {} : query_hash.dup
335
335
  batch_size = [ 1, augmented_array.size ].max
336
336
  augmented_array.next_page_proc = Proc.new do
337
- query_hash[ :offset ] = ( query_hash[ :offset ] || 0 ) + batch_size
337
+ query_hash[ 'offset' ] = ( query_hash[ 'offset' ] || 0 ) + batch_size
338
338
  endpoint.list( query_hash )
339
339
  end
340
340
 
@@ -162,13 +162,7 @@ module Hoodoo; module Services
162
162
  :embeds => context.request.embeds,
163
163
  :references => context.request.references,
164
164
  :headers => context.request.headers,
165
- :list => {
166
- :offset => context.request.list.offset,
167
- :limit => context.request.list.limit,
168
- :sort_data => context.request.list.sort_data,
169
- :search_data => context.request.list.search_data,
170
- :filter_data => context.request.list.filter_data
171
- }
165
+ :list => context.request.list.to_h
172
166
  }
173
167
  }
174
168
 
@@ -1292,6 +1292,11 @@ module Hoodoo; module Services
1292
1292
  #
1293
1293
  # For other kinds of data, check the secure actions to see if the body
1294
1294
  # should be included.
1295
+ #
1296
+ # TODO: This uses deprecated acccessors into the "context.request.list"
1297
+ # object, but it keeps the code simple. It'd be nice to just have
1298
+ # e.g. "data[ :list ] = context.request.list.to_h()" but the
1299
+ # change in log output format might break dependent clients.
1295
1300
 
1296
1301
  if context.response.body.is_a?( ::Array )
1297
1302
  attributes = %i( list_offset list_limit list_sort_data list_search_data list_filter_data embeds references )
@@ -63,6 +63,46 @@ module Hoodoo; module Services
63
63
  self.search_data = {}
64
64
  self.filter_data = {}
65
65
  end
66
+
67
+ # Represent the list data as a Hash, for uses such as persistence
68
+ # or loading into another session instance. The returned Hash is a
69
+ # full deep copy of any internal data; changing it will not alter
70
+ # the ListParameters object state.
71
+ #
72
+ # Top-level keys in the Hash are Strings corresponding to the names
73
+ # of accessor parameters:
74
+ #
75
+ # * +"offset"+
76
+ # * +"limit"+
77
+ # * +"sort_data"+
78
+ # * +"search_data"+
79
+ # * +"filter_data"+
80
+ #
81
+ # Sort, search and filter data, if not empty, also have String keys.
82
+ #
83
+ # See also #from_h!.
84
+ #
85
+ def to_h
86
+ {
87
+ 'offset' => self.offset,
88
+ 'limit' => self.limit,
89
+ 'sort_data' => Hoodoo::Utilities.deep_dup( self.sort_data ),
90
+ 'search_data' => Hoodoo::Utilities.deep_dup( self.search_data ),
91
+ 'filter_data' => Hoodoo::Utilities.deep_dup( self.filter_data )
92
+ }
93
+ end
94
+
95
+ # Load list parameters from a given Hash, of the form set by #to_h.
96
+ # Overwrites any corresponding internal attributes and takes full
97
+ # deep copies of sort, search and filter values.
98
+ #
99
+ def from_h!( hash )
100
+ self.offset = hash[ 'offset' ] if hash.has_key?( 'offset' )
101
+ self.limit = hash[ 'limit' ] if hash.has_key?( 'limit' )
102
+ self.sort_data = Hoodoo::Utilities.deep_dup( hash[ 'sort_data' ] ) if hash[ 'sort_data' ].is_a?( Hash )
103
+ self.search_data = Hoodoo::Utilities.deep_dup( hash[ 'search_data' ] ) if hash[ 'search_data' ].is_a?( Hash )
104
+ self.filter_data = Hoodoo::Utilities.deep_dup( hash[ 'filter_data' ] ) if hash[ 'filter_data' ].is_a?( Hash )
105
+ end
66
106
  end
67
107
 
68
108
  # Requested locale for internationalised operations; +"en-nz"+ by
@@ -12,11 +12,11 @@ module Hoodoo
12
12
  # The Hoodoo gem version. If this changes, be sure to re-run
13
13
  # <tt>bundle install</tt> or <tt>bundle update</tt>.
14
14
  #
15
- VERSION = '2.4.3'
15
+ VERSION = '2.5.0'
16
16
 
17
17
  # The Hoodoo gem date. If this changes, be sure to re-run
18
18
  # <tt>bundle install</tt> or <tt>bundle update</tt>.
19
19
  #
20
- DATE = '2018-04-30'
20
+ DATE = '2018-05-17'
21
21
 
22
22
  end
@@ -88,11 +88,11 @@ describe Hoodoo::Services::Middleware::ExceptionReporting::BaseReporter do
88
88
  :references => [ 'r1', 'r2' ],
89
89
  :headers => { 'HTTP_X_EXAMPLE' => '42' },
90
90
  :list => {
91
- :offset => 0,
92
- :limit => 50,
93
- :sort_data => { 'created_at' => 'desc' },
94
- :search_data => { 'example' => '42' },
95
- :filter_data => { 'unexample' => '24' }
91
+ 'offset' => 0,
92
+ 'limit' => 50,
93
+ 'sort_data' => { 'created_at' => 'desc' },
94
+ 'search_data' => { 'example' => '42' },
95
+ 'filter_data' => { 'unexample' => '24' }
96
96
  }
97
97
  },
98
98
  :session => Hoodoo::Services::Middleware.test_session().to_h()
@@ -1,12 +1,30 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Hoodoo::Services::Request do
4
- context 'uri_path_components' do
5
- before do
6
- @r = Hoodoo::Services::Request.new
7
- end
4
+ before do
5
+ @r = Hoodoo::Services::Request.new
6
+ end
7
+
8
+ it 'has correct default values' do
8
9
 
9
- it 'should record path components properly' do
10
+ # For list parameters in @r.list, see later.
11
+ #
12
+ expect( @r.locale ).to eq( 'en-nz' )
13
+ expect( @r.dated_at ).to eq( nil )
14
+ expect( @r.body ).to eq( nil )
15
+ expect( @r.uri_path_components ).to eq( [] )
16
+ expect( @r.ident ).to eq( nil )
17
+ expect( @r.uri_path_extension ).to eq( '' )
18
+ expect( @r.embeds ).to eq( [] )
19
+ expect( @r.references ).to eq( [] )
20
+ expect( @r.headers ).to eq( {} )
21
+
22
+ expect( @r.headers.frozen? ).to eq( true )
23
+
24
+ end
25
+
26
+ context 'uri_path_components' do
27
+ it 'records path components properly' do
10
28
  ary = [ 'one', 'two', 'three' ]
11
29
  @r.uri_path_components = ary
12
30
 
@@ -14,7 +32,7 @@ describe Hoodoo::Services::Request do
14
32
  expect(@r.ident).to eq(ary.first)
15
33
  end
16
34
 
17
- it 'should deal with nil path components properly' do
35
+ it 'deals with nil path components properly' do
18
36
  ary = nil
19
37
  @r.uri_path_components = ary
20
38
 
@@ -22,56 +40,165 @@ describe Hoodoo::Services::Request do
22
40
  expect(@r.ident).to be_nil
23
41
  end
24
42
 
25
- it 'should deal with non-array path components properly' do
43
+ it 'deals with non-array path components properly' do
26
44
  ary = 'not an array'
27
45
  @r.uri_path_components = ary
28
46
 
29
47
  expect(@r.uri_path_components).to be_nil
30
48
  expect(@r.ident).to be_nil
31
49
  end
50
+ end
32
51
 
33
- it 'rationalises date/time' do
34
- [ :dated_at, :dated_from ].each do | attr |
35
- now = DateTime.now
36
- @r.send( "#{ attr }=", now )
37
- expect( @r.send( attr ) ).to eq( now )
52
+ it 'rationalises date/time' do
53
+ [ :dated_at, :dated_from ].each do | attr |
54
+ now = DateTime.now
55
+ @r.send( "#{ attr }=", now )
56
+ expect( @r.send( attr ) ).to eq( now )
38
57
 
39
- now = DateTime.now + 10
40
- @r.send( "#{ attr }=", now.to_time )
41
- expect( @r.send( attr ) ).to eq( now )
58
+ now = DateTime.now + 10
59
+ @r.send( "#{ attr }=", now.to_time )
60
+ expect( @r.send( attr ) ).to eq( now )
42
61
 
43
- now = DateTime.now + 20
44
- @r.send( "#{ attr }=", Hoodoo::Utilities::nanosecond_iso8601( now ) )
45
- expect( @r.send( attr ) ).to eq( now )
46
- end
62
+ now = DateTime.now + 20
63
+ @r.send( "#{ attr }=", Hoodoo::Utilities::nanosecond_iso8601( now ) )
64
+ expect( @r.send( attr ) ).to eq( now )
47
65
  end
66
+ end
48
67
 
68
+ context '#list' do
49
69
  it 'has correct default values' do
50
- expect( @r.locale ).to eq( 'en-nz' )
51
- expect( @r.dated_at ).to eq( nil )
52
- expect( @r.body ).to eq( nil )
53
- expect( @r.uri_path_components ).to eq( [] )
54
- expect( @r.ident ).to eq( nil )
55
- expect( @r.uri_path_extension ).to eq( '' )
56
- expect( @r.list.offset ).to eq( 0 )
57
- expect( @r.list.limit ).to eq( 50 )
58
- expect( @r.list_sort_data ).to eq( { 'created_at' => 'desc' } )
59
- expect( @r.list.search_data ).to eq( {} )
60
- expect( @r.list.filter_data ).to eq( {} )
61
- expect( @r.embeds ).to eq( [] )
62
- expect( @r.references ).to eq( [] )
63
- expect( @r.headers ).to eq( {} )
64
-
65
- expect( @r.headers.frozen? ).to eq( true )
70
+ expect( @r.list.offset ).to eq( 0 )
71
+ expect( @r.list.limit ).to eq( 50 )
72
+ expect( @r.list.sort_data ).to eq( { 'created_at' => 'desc' } )
73
+ expect( @r.list.search_data ).to eq( {} )
74
+ expect( @r.list.filter_data ).to eq( {} )
66
75
  end
67
76
 
68
- it 'supports deprecated accessors' do
69
- expect( @r.list_offset ).to eq( 0 )
70
- expect( @r.list_limit ).to eq( 50 )
71
- expect( @r.list_sort_data ).to eq( { 'created_at' => 'desc' } )
72
- expect( @r.list_search_data ).to eq( {} )
73
- expect( @r.list_filter_data ).to eq( {} )
77
+ context 'and hashes via' do
78
+ before :each do
79
+ @offset = 1000
80
+ @limit = 500
81
+ @sort = { 'created_at' => 'asc' }
82
+ @search = { 'foo' => 'bar', 'bar' => { 'baz' => 3 } }
83
+ @filter = { 'bar' => 'foo', 'baz' => { 'bar' => 2 } }
84
+
85
+ @replacement_hash = {
86
+ 'offset' => @offset,
87
+ 'limit' => @limit,
88
+ 'sort_data' => @sort,
89
+ 'search_data' => @search,
90
+ 'filter_data' => @filter
91
+ }
92
+ end
93
+
94
+ context '#to_h' do
95
+ before :each do
96
+ @r.list.offset = @offset
97
+ @r.list.limit = @limit
98
+ @r.list.sort_data = @sort
99
+ @r.list.search_data = @search
100
+ @r.list.filter_data = @filter
101
+ end
102
+
103
+ it 'returns the expected Hash' do
104
+ expect( @r.list.to_h ).to eq( @replacement_hash )
105
+ end
106
+
107
+ it 'deep-duplicates the parameters' do
108
+ result = @r.list.to_h()
109
+
110
+ old_sort_value = @sort[ @sort.keys.first ]
111
+ result[ 'sort_data' ][ @sort.keys.first ] == 'changed'
112
+ expect( @sort[ @sort.keys.first ] ).to eql( old_sort_value )
113
+
114
+ old_search_value = @search[ @search.keys.first ]
115
+ result[ 'search_data' ][ @search.keys.first ] == 'changed'
116
+ expect( @search[ @search.keys.first ] ).to eql( old_search_value )
117
+
118
+ old_filter_value = @filter[ @filter.keys.first ]
119
+ result[ 'filter_data' ][ @filter.keys.first ] == 'changed'
120
+ expect( @filter[ @filter.keys.first ] ).to eql( old_filter_value )
121
+ end
122
+ end
123
+
124
+ context '#from_h!' do
125
+ it 'overwrites all parameters' do
126
+ request = Hoodoo::Services::Request.new
127
+
128
+ expect( request.list.offset ).to eq( 0 )
129
+ expect( request.list.limit ).to eq( 50 )
130
+ expect( request.list.sort_data ).to eq( { 'created_at' => 'desc' } )
131
+ expect( request.list.search_data ).to eq( {} )
132
+ expect( request.list.filter_data ).to eq( {} )
133
+
134
+ request.list.from_h!( @replacement_hash )
135
+
136
+ expect( request.list.offset ).to eq( @offset )
137
+ expect( request.list.limit ).to eq( @limit )
138
+ expect( request.list.sort_data ).to eq( @sort )
139
+ expect( request.list.search_data ).to eq( @search )
140
+ expect( request.list.filter_data ).to eq( @filter )
141
+ end
142
+
143
+ it 'overwrites only provided parameters' do
144
+ replacements = {
145
+ 'offset' => @offset,
146
+ 'limit' => @limit,
147
+ 'sort_data' => @sort,
148
+ 'search_data' => @search,
149
+ 'filter_data' => @filter
150
+ }
151
+
152
+ # Iterate over all the possible keys above and build a Hash that
153
+ # contains just one at a time. Make a new request, set the list
154
+ # options from the one-at-a-time Hash and verify that only that
155
+ # one parameter changed.
156
+
157
+ replacements.each do | replacement_key, replacement_value |
158
+ request = Hoodoo::Services::Request.new
159
+ clean = {
160
+ 'offset' => request.list.offset,
161
+ 'limit' => request.list.limit,
162
+ 'sort_data' => request.list.sort_data,
163
+ 'search_data' => request.list.search_data,
164
+ 'filter_data' => request.list.filter_data
165
+ }
166
+
167
+ hash = { replacement_key => replacement_value }
168
+ request.list.from_h!( hash )
169
+
170
+ clean.each do | clean_key, clean_value |
171
+ if clean_key == replacement_key
172
+ expect( request.list.send( clean_key ) ).to eql( replacement_value )
173
+ else
174
+ expect( request.list.send( clean_key ) ).to eql( clean_value )
175
+ end
176
+ end
177
+ end
178
+ end
179
+ end
180
+
181
+ context '#to_h and #from_h! together' do
182
+ before :each do
183
+ @r.list.offset = @offset
184
+ @r.list.limit = @limit
185
+ @r.list.sort_data = @sort
186
+ @r.list.search_data = @search
187
+ @r.list.filter_data = @filter
188
+ end
74
189
 
190
+ it 'translates from one list to another' do
191
+ hash = @r.list.to_h
192
+
193
+ request = Hoodoo::Services::Request.new
194
+ request.list.from_h!( hash )
195
+
196
+ expect( request.list.to_h ).to eql( @replacement_hash )
197
+ end
198
+ end
199
+ end
200
+
201
+ it 'supports deprecated accessors' do
75
202
  lo = 10
76
203
  ll = 20
77
204
  ke = 'foo'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hoodoo
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.3
4
+ version: 2.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Loyalty New Zealand
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-30 00:00:00.000000000 Z
11
+ date: 2018-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dalli