Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 536fe77

Browse files
committed
feat: Accept position params in non-data request methods
These are merged with the keyword argument params, with the latter being preferred. This allows writing shorter methods, such as: gh.rel(:user).get({user: 'jgraichen'}).value! The commit further expands the testing on the request methods, for params and data handling.
1 parent fce2046 commit 536fe77

5 files changed

Lines changed: 128 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2222

2323
`#post`, `#put`, and `#patch` still take an optional first positional data/body argument. Change `post(body, {}, {headers: ...})` to `post(body, headers: ...)`.
2424

25+
`#get`, `#head`, and `#delete` accept a hash as the first positional argument, which is merged with `params:`. Therefore, passing parameters as data works too: `get({id: 1})`.
26+
2527
### Fixes
2628

2729
### Breaks

examples/github_last.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
end
1818

1919
gh = Restify.new('https://api.github.com', headers:).get.value!
20-
user = gh.rel(:user).get(user: 'jgraichen').value!
20+
user = gh.rel(:user).get({user: 'jgraichen'}).value!
2121
repos = user.rel(:repos).get.value!
2222

2323
commits = repos.map do |repo|

examples/github_thread.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
end
1515

1616
gh = Restify.new('https://api.github.com', headers:).get.value
17-
repo = gh.rel(:repository).get(owner: 'jgraichen', repo: 'restify').value
17+
repo = gh.rel(:repository).get({owner: 'jgraichen', repo: 'restify'}).value
1818
cmt = repo.rel(:commits).get.value.first
1919

2020
puts "Last commit: #{cmt['sha']}"

lib/restify/relation.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,31 @@ def request(method:, params: {}, **opts)
2323
context.request(method, expand(params), **opts)
2424
end
2525

26-
def get(**opts)
27-
request(**opts, method: :get)
26+
def get(data = {}, params: {}, **opts)
27+
request(**opts, method: :get, params: data.merge(params))
2828
end
2929

30-
def head(**opts)
31-
request(**opts, method: :head)
30+
def head(data = {}, params: {}, **opts)
31+
request(**opts, method: :head, params: data.merge(params))
3232
end
3333

34-
def delete(**opts)
35-
request(**opts, method: :head)
34+
def delete(data = {}, params: {}, **opts)
35+
request(**opts, method: :delete, params: data.merge(params))
3636
end
3737

3838
def post(data = nil, **opts)
39-
request(**opts, method: :post, data: data)
39+
opts[:data] = data unless opts.key?(:data)
40+
request(**opts, method: :post)
4041
end
4142

4243
def put(data = nil, **opts)
43-
request(**opts, method: :put, data: data)
44+
opts[:data] = data unless opts.key?(:data)
45+
request(**opts, method: :put)
4446
end
4547

4648
def patch(data = nil, **opts)
47-
request(**opts, method: :patch, data: data)
49+
opts[:data] = data unless opts.key?(:data)
50+
request(**opts, method: :patch)
4851
end
4952

5053
def ==(other)

spec/restify/relation_spec.rb

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
subject(:relation) { described_class.new context, pattern }
88

99
let(:context) { Restify::Context.new('http://test.host/') }
10-
let(:pattern) { '/resource/{id}' }
10+
let(:pattern) { '/resource/{id}{?q*}' }
1111

1212
describe '#==' do
1313
it 'equals pattern' do
@@ -120,4 +120,115 @@ def to_param
120120
it { expect { expanded }.to raise_exception TypeError, /Can't convert Hash into String./ }
121121
end
122122
end
123+
124+
shared_examples 'non-data-request' do |method|
125+
it "issues a #{method.upcase} request" do
126+
expect(context).to receive(:request) do |meth, uri, opts|
127+
expect(meth).to eq method
128+
expect(uri.to_s).to eq 'http://test.host/resource/'
129+
expect(opts).to be_nil
130+
end
131+
relation.send(method)
132+
end
133+
134+
it 'accepts params as keyword argument' do
135+
expect(context).to receive(:request) do |meth, uri, opts|
136+
expect(meth).to eq method
137+
expect(uri.to_s).to eq 'http://test.host/resource/1'
138+
expect(opts).to be_nil
139+
end
140+
relation.send(method, params: {id: 1})
141+
end
142+
143+
it 'accepts params as positional argument' do
144+
expect(context).to receive(:request) do |meth, uri, opts|
145+
expect(meth).to eq method
146+
expect(uri.to_s).to eq 'http://test.host/resource/1'
147+
expect(opts).to be_nil
148+
end
149+
relation.send(method, {id: 1})
150+
end
151+
152+
it 'merges keyword params into positional params' do
153+
expect(context).to receive(:request) do |meth, uri, opts|
154+
expect(meth).to eq method
155+
expect(uri.to_s).to eq 'http://test.host/resource/1?a=2&b=3'
156+
expect(opts).to be_nil
157+
end
158+
relation.send(method, {id: 1, q: {a: 1, c: 1}}, params: {q: {a: 2, b: 3}})
159+
end
160+
end
161+
162+
describe '#get' do
163+
include_examples 'non-data-request', :get
164+
end
165+
166+
describe '#head' do
167+
include_examples 'non-data-request', :head
168+
end
169+
170+
describe '#delete' do
171+
include_examples 'non-data-request', :delete
172+
end
173+
174+
shared_examples 'data-request' do |method|
175+
let(:data) { Object.new }
176+
177+
it "issues a #{method.upcase} request" do
178+
expect(context).to receive(:request) do |meth, uri, opts|
179+
expect(meth).to eq method
180+
expect(uri.to_s).to eq 'http://test.host/resource/'
181+
expect(opts).to eq({data: nil})
182+
end
183+
relation.send(method)
184+
end
185+
186+
it 'accepts params as keyword argument' do
187+
expect(context).to receive(:request) do |meth, uri, opts|
188+
expect(meth).to eq method
189+
expect(uri.to_s).to eq 'http://test.host/resource/1'
190+
expect(opts).to eq({data: nil})
191+
end
192+
relation.send(method, params: {id: 1})
193+
end
194+
195+
it 'accepts data as keyword argument' do
196+
expect(context).to receive(:request) do |meth, uri, opts|
197+
expect(meth).to eq method
198+
expect(uri.to_s).to eq 'http://test.host/resource/1'
199+
expect(opts).to eq({data:})
200+
end
201+
relation.send(method, data:, params: {id: 1})
202+
end
203+
204+
it 'accepts data as position argument' do
205+
expect(context).to receive(:request) do |meth, uri, opts|
206+
expect(meth).to eq method
207+
expect(uri.to_s).to eq 'http://test.host/resource/1'
208+
expect(opts).to eq({data:})
209+
end
210+
relation.send(method, data, params: {id: 1})
211+
end
212+
213+
it 'prefers data from keyword argument' do
214+
expect(context).to receive(:request) do |meth, uri, opts|
215+
expect(meth).to eq method
216+
expect(uri.to_s).to eq 'http://test.host/resource/1'
217+
expect(opts).to eq({data:})
218+
end
219+
relation.send(method, 'DATA', data:, params: {id: 1})
220+
end
221+
end
222+
223+
describe '#post' do
224+
include_examples 'data-request', :post
225+
end
226+
227+
describe '#put' do
228+
include_examples 'data-request', :put
229+
end
230+
231+
describe '#patch' do
232+
include_examples 'data-request', :patch
233+
end
123234
end

0 commit comments

Comments
 (0)