Skip to content

Commit e847808

Browse files
committed
Allow setting of various other options on resources
1 parent 610de43 commit e847808

5 files changed

Lines changed: 37 additions & 14 deletions

File tree

lib/userlist/push/operations/delete.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def delete(payload = {}, config = self.config)
77
return false unless resource = from_payload(payload, config)
88
return false unless resource.delete?
99

10-
strategy.call(:delete, endpoint, resource.for_context(:delete))
10+
strategy.call(:delete, endpoint, resource.with(context: :delete))
1111
end
1212
end
1313

lib/userlist/push/operations/push.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def push(payload = {}, config = self.config)
77
return false unless resource = from_payload(payload, config)
88
return false unless resource.push?
99

10-
strategy.call(:post, endpoint, resource.for_context(:push))
10+
strategy.call(:post, endpoint, resource.with(context: :push))
1111
end
1212

1313
alias create push

lib/userlist/push/resource.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ def generated_methods
5858
end
5959
end
6060

61-
attr_reader :payload, :config, :context
61+
attr_reader :payload, :config, :options
6262

6363
def initialize(payload = {}, config = Userlist.config)
6464
raise Userlist::ArgumentError, 'Missing required payload' unless payload
6565

6666
@payload = payload
6767
@config = config
68-
@context = :push
68+
@options = { context: :push }
6969
end
7070

7171
def respond_to_missing?(method, include_private = false)
@@ -74,7 +74,7 @@ def respond_to_missing?(method, include_private = false)
7474
end
7575

7676
def to_hash
77-
Serializer.serialize(self, context: context)
77+
Serializer.serialize(self, **options)
7878
end
7979
alias to_h to_hash
8080

@@ -107,9 +107,9 @@ def push?
107107
true
108108
end
109109

110-
def for_context(context)
110+
def with(**options)
111111
dup.tap do |instance|
112-
instance.instance_variable_set(:@context, context)
112+
instance.instance_variable_set(:@options, options)
113113
end
114114
end
115115

spec/userlist/push/operations/delete_spec.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
end
2020

2121
it 'should set the context to :delete' do
22-
expect(strategy).to receive(:call).with(:delete, '/users', satisfy { |r| r.context == :delete })
22+
expect(strategy).to receive(:call).with(:delete, '/users', satisfy { |r| r.options[:context] == :delete })
2323
relation.delete(payload)
2424
end
2525
end
@@ -35,7 +35,7 @@
3535
end
3636

3737
it 'should set the context to :delete' do
38-
expect(strategy).to receive(:call).with(:delete, '/users', satisfy { |r| r.context == :delete })
38+
expect(strategy).to receive(:call).with(:delete, '/users', satisfy { |r| r.options[:context] == :delete })
3939
relation.delete(payload)
4040
end
4141
end
@@ -49,7 +49,7 @@
4949
end
5050

5151
it 'should set the context to :delete' do
52-
expect(strategy).to receive(:call).with(:delete, '/users', satisfy { |r| r.context == :delete })
52+
expect(strategy).to receive(:call).with(:delete, '/users', satisfy { |r| r.options[:context] == :delete })
5353
relation.delete(payload)
5454
end
5555
end
@@ -63,6 +63,17 @@
6363
end
6464
end
6565

66+
context 'when given keyword arguments as payload' do
67+
let(:payload) do
68+
{ identifier: identifier }
69+
end
70+
71+
it 'should send the request to the endpoint' do
72+
expect(strategy).to receive(:call).with(:delete, '/users', resource.with(context: :delete))
73+
relation.delete(identifier:)
74+
end
75+
end
76+
6677
context 'when the operation is not permitted' do
6778
before do
6879
allow_any_instance_of(resource_type).to receive(:delete?).and_return(false)

spec/userlist/push/operations/push_spec.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
let(:strategy) { instance_double('Userlist::Push::Strategies::Direct') }
88
let(:resource) { resource_type.from_payload(payload) }
99

10+
let(:identifier) { 'identifier' }
1011
let(:payload) do
1112
{
12-
identifier: 'identifier',
13+
identifier: identifier,
1314
properties: {
1415
first_name: 'John',
1516
last_name: 'Doe'
@@ -38,7 +39,7 @@
3839
end
3940

4041
it 'should set the context to :push' do
41-
expect(strategy).to receive(:call).with(:post, '/users', satisfy { |r| r.context == :push })
42+
expect(strategy).to receive(:call).with(:post, '/users', satisfy { |r| r.options[:context] == :push })
4243
relation.push(payload)
4344
end
4445
end
@@ -52,14 +53,14 @@
5253
end
5354

5455
it 'should set the context to :push' do
55-
expect(strategy).to receive(:call).with(:post, '/users', satisfy { |r| r.context == :push })
56+
expect(strategy).to receive(:call).with(:post, '/users', satisfy { |r| r.options[:context] == :push })
5657
relation.push(payload)
5758
end
5859
end
5960

6061
context 'when given an identifier' do
6162
let(:payload) do
62-
{ identifier: 'identifier' }
63+
{ identifier: identifier }
6364
end
6465

6566
it 'should send a simple payload to the endpoint' do
@@ -77,6 +78,17 @@
7778
end
7879
end
7980

81+
context 'when given keyword arguments as payload' do
82+
let(:payload) do
83+
{ identifier: identifier }
84+
end
85+
86+
it 'should send the request to the endpoint' do
87+
expect(strategy).to receive(:call).with(:post, '/users', resource.with(context: :push))
88+
relation.push(identifier:)
89+
end
90+
end
91+
8092
context 'when the operation is not permitted' do
8193
before do
8294
allow_any_instance_of(resource_type).to receive(:push?).and_return(false)

0 commit comments

Comments
 (0)