Skip to content

Commit 753734e

Browse files
committed
Remove duplication.
1 parent 21c6d00 commit 753734e

File tree

2 files changed

+18
-54
lines changed

2 files changed

+18
-54
lines changed

lib/hyperclient/link.rb

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -80,76 +80,37 @@ def _hreflang
8080
@link['hreflang']
8181
end
8282

83-
# Public: Returns the Resource which the Link is pointing to.
84-
def _get
85-
@resource = begin
86-
response = Futuroscope::Future.new do
87-
_connection.get(_url)
88-
end
89-
Resource.new(response.body, @entry_point, response)
90-
end
91-
end
92-
9383
def _resource
9484
@resource || _get
9585
end
9686

97-
def _connection
98-
@entry_point.connection
87+
# Public: Returns the Resource which the Link is pointing to.
88+
def _get
89+
http_method(:get)
9990
end
10091

10192
def _options
102-
@resource = begin
103-
response = Futuroscope::Future.new do
104-
_connection.run_request(:options, _url, nil, nil)
105-
end
106-
Resource.new(response.body, @entry_point, response)
107-
end
93+
http_method(:options)
10894
end
10995

11096
def _head
111-
@resource = begin
112-
response = Futuroscope::Future.new do
113-
_connection.head(_url)
114-
end
115-
Resource.new(response.body, @entry_point, response)
116-
end
97+
http_method(:head)
11798
end
11899

119100
def _delete
120-
@resource = begin
121-
response = Futuroscope::Future.new do
122-
_connection.delete(_url)
123-
end
124-
Resource.new(response.body, @entry_point, response)
125-
end
101+
http_method(:delete)
126102
end
127103

128104
def _post(params = {})
129-
@resource = begin
130-
response = Futuroscope::Future.new do
131-
_connection.post(_url, params)
132-
end
133-
Resource.new(response.body, @entry_point, response)
134-
end
105+
http_method(:post, params)
135106
end
136107

137108
def _put(params = {})
138-
@resource = begin
139-
response = Futuroscope::Future.new do
140-
_connection.put(_url, params)
141-
end
142-
Resource.new(response.body, @entry_point, response)
143-
end
109+
http_method(:put, params)
144110
end
145111

146112
def _patch(params = {})
147-
@resource = begin
148-
response = Futuroscope::Future.new do
149-
_connection.patch(_url, params)
150-
end
151-
Resource.new(response.body, @entry_point, response)
152-
end
113+
http_method(:patch, params)
153114
end
154115

155116
def inspect
@@ -198,5 +159,14 @@ def to_ary
198159
def _uri_template
199160
@uri_template ||= URITemplate.new(@link['href'])
200161
end
162+
163+
def http_method(method, body = nil)
164+
@resource = begin
165+
response = Futuroscope::Future.new do
166+
@entry_point.connection.run_request(method, _url, body, nil)
167+
end
168+
Resource.new(response.body, @entry_point, response)
169+
end
170+
end
201171
end
202172
end

test/hyperclient/link_test.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,6 @@ module Hyperclient
117117
end
118118
end
119119

120-
describe '_connection' do
121-
it 'returns the entry point connection' do
122-
Link.new('key', {}, entry_point)._connection.must_equal entry_point.connection
123-
end
124-
end
125-
126120
describe 'get' do
127121
it 'sends a GET request with the link url' do
128122
link = Link.new('key', { 'href' => '/productions/1' }, entry_point)

0 commit comments

Comments
 (0)