Skip to content

Commit b4026dc

Browse files
committed
Merge pull request #85 from codegram/cleanup
Cleanup
2 parents deebb3b + 753734e commit b4026dc

File tree

7 files changed

+50
-79
lines changed

7 files changed

+50
-79
lines changed

lib/hyperclient/collection.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Hyperclient
2-
# Public: A helper class to wrapp a collection of elements and provide
2+
# Public: A helper class to wrap a collection of elements and provide
33
# Hash-like access or via a method call.
44
#
55
# Examples
@@ -25,8 +25,13 @@ def each(&block)
2525
@collection.each(&block)
2626
end
2727

28-
def include?(obj)
29-
@collection.include?(obj)
28+
# Public: Checks if this collection includes a given key.
29+
#
30+
# key - A String or Symbol to check for existance.
31+
#
32+
# Returns True/False.
33+
def include?(key)
34+
@collection.include?(key)
3035
end
3136

3237
# Public: Returns a value from the collection for the given key.
@@ -51,7 +56,7 @@ def [](name)
5156
@collection[name.to_s]
5257
end
5358

54-
# Public: Returns the wrapped collection as a hash.
59+
# Public: Returns the wrapped collection as a Hash.
5560
#
5661
# Returns a Hash.
5762
def to_h

lib/hyperclient/curie.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ module Hyperclient
77
class Curie
88
# Public: Initializes a new Curie.
99
#
10-
# curie - The String with the URI of the curie.
11-
# entry_point - The EntryPoint object to inject the cofnigutation.
10+
# curie_hash - The String with the URI of the curie.
11+
# entry_point - The EntryPoint object to inject the configuration.
1212
def initialize(curie_hash, entry_point)
1313
@curie_hash = curie_hash
1414
@entry_point = entry_point
@@ -22,12 +22,12 @@ def templated?
2222
!!@curie_hash['templated']
2323
end
2424

25-
# Public: Returns the name property of the Curie
25+
# Public: Returns the name property of the Curie.
2626
def name
2727
@curie_hash['name']
2828
end
2929

30-
# Public: Returns the href property of the Curie
30+
# Public: Returns the href property of the Curie.
3131
def href
3232
@curie_hash['href']
3333
end
@@ -38,7 +38,7 @@ def inspect
3838

3939
# Public: Expands the Curie when is templated with the given variables.
4040
#
41-
# rel - The rel to expand.
41+
# rel - The String rel to expand.
4242
#
4343
# Returns a new expanded url.
4444
def expand(rel)

lib/hyperclient/entry_point.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ def initialize(url, &_block)
4343

4444
# Public: A Faraday connection to use as a HTTP client.
4545
#
46-
# options - A Hash containing additional options.
47-
#
48-
# default - Set to true to reuse default Faraday connection options.
46+
# options - A Hash containing additional options to pass to Farday. Use
47+
# {default: false} if you want to skip using default Faraday options set by
48+
# Hyperclient.
4949
#
5050
# Returns a Faraday::Connection.
5151
def connection(options = {}, &block)
@@ -123,12 +123,12 @@ def faraday_block=(value)
123123
#
124124
# Returns a block.
125125
def default_faraday_block
126-
lambda do |conn|
127-
conn.use Faraday::Response::RaiseError
128-
conn.use FaradayMiddleware::FollowRedirects
129-
conn.request :hal_json
130-
conn.response :hal_json, content_type: /\bjson$/
131-
conn.adapter :net_http
126+
lambda do |connection|
127+
connection.use Faraday::Response::RaiseError
128+
connection.use FaradayMiddleware::FollowRedirects
129+
connection.request :hal_json
130+
connection.response :hal_json, content_type: /\bjson$/
131+
connection.adapter :net_http
132132
end
133133
end
134134

lib/hyperclient/link.rb

Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Link
1010
#
1111
# key - The key or name of the link.
1212
# link - The String with the URI of the link.
13-
# entry_point - The EntryPoint object to inject the cofnigutation.
13+
# entry_point - The EntryPoint object to inject the configuration.
1414
# uri_variables - The optional Hash with the variables to expand the link
1515
# if it is templated.
1616
def initialize(key, link, entry_point, uri_variables = nil)
@@ -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

lib/hyperclient/link_collection.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ module Hyperclient
1313
class LinkCollection < Collection
1414
# Public: Initializes a LinkCollection.
1515
#
16-
# collection - The Hash with the links.
17-
# curies - Link curies.
16+
# collection - The Hash with the links.
17+
# curies - The Hash with link curies.
1818
# entry_point - The EntryPoint object to inject the configuration.
1919
def initialize(collection, curies, entry_point)
2020
fail "Invalid response for LinkCollection. The response was: #{collection.inspect}" if collection && !collection.respond_to?(:collect)
@@ -33,13 +33,15 @@ def initialize(collection, curies, entry_point)
3333

3434
# Internal: Creates links from the response hash.
3535
#
36+
# name - A String to identify the link's name.
3637
# link_or_links - A Hash or an Array of hashes with the links to build.
37-
# entry_point - The EntryPoint object to inject the configuration.
38-
# curies - Optional curies for templated links.
38+
# curies - An Array of Curies for templated links.
39+
# entry_point - The EntryPoint object to inject the configuration.
3940
#
40-
# Returns a Link or an array of Links when given an Array.
41+
# Returns a Link or an Array of Links when given an Array.
4142
def build_link(name, link_or_links, curies, entry_point)
4243
return unless link_or_links
44+
4345
if link_or_links.respond_to?(:to_ary)
4446
link_or_links.map do |link|
4547
build_link(name, link, curies, entry_point)

lib/hyperclient/resource_collection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ResourceCollection < Collection
1414
# Public: Initializes a ResourceCollection.
1515
#
1616
# collection - The Hash with the embedded resources.
17-
# entry_point - The EntryPoint object to inject the cofnigutation.
17+
# entry_point - The EntryPoint object to inject the configuration.
1818
#
1919
def initialize(collection, entry_point)
2020
@entry_point = entry_point

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)