hyperclient 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/Readme.md +2 -0
- data/examples/cyberscore.rb +3 -3
- data/examples/hal_shop.rb +4 -4
- data/lib/hyperclient/discoverer.rb +18 -10
- data/lib/hyperclient/representation.rb +1 -1
- data/lib/hyperclient/resource.rb +1 -1
- data/lib/hyperclient/version.rb +1 -1
- data/test/hyperclient/discoverer_test.rb +25 -0
- data/test/hyperclient/representation_test.rb +4 -4
- metadata +5 -5
data/.gitignore
CHANGED
data/Readme.md
CHANGED
@@ -121,6 +121,8 @@ posts.post({title: "I'm a blogger!", body: 'Wohoo!!'})
|
|
121
121
|
|
122
122
|
* Resource permissions: Using the `Allow` header Hyperclient should be able to
|
123
123
|
restrict the allowed method on a given `Resource`.
|
124
|
+
* Curie syntax support for links (see http://tools.ietf.org/html/draft-kelly-json-hal-03#section-8.2)
|
125
|
+
* Profile support for links
|
124
126
|
|
125
127
|
|
126
128
|
## Contributing
|
data/examples/cyberscore.rb
CHANGED
@@ -6,15 +6,15 @@ class Cyberscore
|
|
6
6
|
entry_point 'http://cs-api.heroku.com/api/'
|
7
7
|
|
8
8
|
def news
|
9
|
-
links.feeds.links.submissions.
|
9
|
+
links.feeds.links.submissions.embedded.news
|
10
10
|
end
|
11
11
|
|
12
12
|
def submissions
|
13
|
-
links.feeds.links.submissions.
|
13
|
+
links.feeds.links.submissions.embedded.submissions
|
14
14
|
end
|
15
15
|
|
16
16
|
def games
|
17
|
-
links.feeds.links.games.
|
17
|
+
links.feeds.links.games.embedded.games
|
18
18
|
end
|
19
19
|
|
20
20
|
def add_game(name)
|
data/examples/hal_shop.rb
CHANGED
@@ -35,11 +35,11 @@ print_resources(api.links)
|
|
35
35
|
|
36
36
|
puts
|
37
37
|
puts 'Resources at the entry point:'
|
38
|
-
print_resources(api.
|
38
|
+
print_resources(api.embedded)
|
39
39
|
|
40
40
|
puts
|
41
41
|
puts "Let's see what stats we have:"
|
42
|
-
print_attributes(api.
|
42
|
+
print_attributes(api.embedded.stats.attributes)
|
43
43
|
|
44
44
|
products = api.links["http://hal-shop.heroku.com/rels/products"].reload
|
45
45
|
|
@@ -49,7 +49,7 @@ puts products.attributes['inventory_size']
|
|
49
49
|
|
50
50
|
puts
|
51
51
|
puts 'What resources does products have?'
|
52
|
-
print_resources(products.
|
52
|
+
print_resources(products.embedded.products)
|
53
53
|
|
54
54
|
puts
|
55
55
|
puts 'And links?'
|
@@ -57,4 +57,4 @@ print_resources(products.links)
|
|
57
57
|
|
58
58
|
puts
|
59
59
|
puts 'Attributes of the first product?'
|
60
|
-
print_attributes(products.
|
60
|
+
print_attributes(products.embedded.products.first.attributes)
|
@@ -44,7 +44,7 @@ module Hyperclient
|
|
44
44
|
|
45
45
|
@resources ||= @representation.inject({}) do |memo, (name, representation)|
|
46
46
|
next memo if name == 'self'
|
47
|
-
|
47
|
+
memo.update(name => build_resource(representation, name))
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
@@ -55,19 +55,27 @@ module Hyperclient
|
|
55
55
|
def build_resource(representation, name = nil)
|
56
56
|
return representation.map(&method(:build_resource)) if representation.is_a?(Array)
|
57
57
|
|
58
|
-
url =
|
58
|
+
url = URLExtractor.new(representation).url
|
59
59
|
ResourceFactory.resource(url, {representation: representation, name: name})
|
60
60
|
end
|
61
61
|
|
62
|
-
# Internal:
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
62
|
+
# Internal: Extract the url from a HAL representation.
|
63
|
+
class URLExtractor
|
64
|
+
# Public: Initializes a URLExtractor.
|
65
|
+
#
|
66
|
+
# representation - A Hash with the representation of a Resource.
|
67
|
+
def initialize(representation)
|
68
|
+
@representation = representation
|
69
|
+
end
|
70
|
+
|
71
|
+
# Public: Returns a String with the resource URL
|
72
|
+
def url
|
73
|
+
return @representation.delete('href') if @representation.include?('href')
|
67
74
|
|
68
|
-
|
69
|
-
(url = representation['_links']['self']['href'])
|
70
|
-
|
75
|
+
if @representation && @representation['_links'] && @representation['_links']['self'] &&
|
76
|
+
(url = @representation['_links']['self']['href'])
|
77
|
+
return url
|
78
|
+
end
|
71
79
|
end
|
72
80
|
end
|
73
81
|
end
|
@@ -29,7 +29,7 @@ module Hyperclient
|
|
29
29
|
|
30
30
|
# Public: Returns a Discoverer for the _embedded section of the representation.
|
31
31
|
# It can be used later to use the resources from this section.
|
32
|
-
def
|
32
|
+
def embedded
|
33
33
|
@embedded ||= Discoverer.new(@representation['_embedded'])
|
34
34
|
end
|
35
35
|
|
data/lib/hyperclient/resource.rb
CHANGED
@@ -8,7 +8,7 @@ module Hyperclient
|
|
8
8
|
class Resource
|
9
9
|
extend Forwardable
|
10
10
|
# Public: Delegate attributes and resources to the representation.
|
11
|
-
def_delegators :representation, :attributes, :
|
11
|
+
def_delegators :representation, :attributes, :embedded, :links
|
12
12
|
|
13
13
|
# Public: Delegate all HTTP methods (get, post, put, delete, options and
|
14
14
|
# head) to Hyperclient::HTTP.
|
data/lib/hyperclient/version.rb
CHANGED
@@ -72,5 +72,30 @@ module Hyperclient
|
|
72
72
|
discoverer.filter.name.wont_be_empty
|
73
73
|
end
|
74
74
|
end
|
75
|
+
|
76
|
+
describe Discoverer::URLExtractor do
|
77
|
+
describe 'url' do
|
78
|
+
it 'extracts the url from embedded resources' do
|
79
|
+
hal = {'_links' => {'self' => {'href' => '/path/to/resource'}}}
|
80
|
+
extractor = Discoverer::URLExtractor.new(hal)
|
81
|
+
|
82
|
+
extractor.url.must_equal '/path/to/resource'
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'extracts the url from linked resources' do
|
86
|
+
hal = {'href' => '/path/to/resource'}
|
87
|
+
extractor = Discoverer::URLExtractor.new(hal)
|
88
|
+
|
89
|
+
extractor.url.must_equal '/path/to/resource'
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'deletes the url from linked resources to prevent empty representations' do
|
93
|
+
hal = {'href' => '/path/to/resource'}
|
94
|
+
Discoverer::URLExtractor.new(hal).url
|
95
|
+
|
96
|
+
hal.include?('href').must_equal false
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
75
100
|
end
|
76
101
|
end
|
@@ -41,11 +41,11 @@ module Hyperclient
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
-
describe '
|
44
|
+
describe 'embedded' do
|
45
45
|
it 'returns resources included in the _embedded section' do
|
46
|
-
representation.
|
47
|
-
representation.
|
48
|
-
representation.
|
46
|
+
representation.embedded.author.must_be_kind_of Resource
|
47
|
+
representation.embedded.episodes.first.must_be_kind_of Resource
|
48
|
+
representation.embedded.episodes.last.must_be_kind_of Resource
|
49
49
|
end
|
50
50
|
end
|
51
51
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hyperclient
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-08-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: httparty
|
@@ -126,7 +126,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
126
126
|
version: '0'
|
127
127
|
segments:
|
128
128
|
- 0
|
129
|
-
hash: -
|
129
|
+
hash: -1844692704935824375
|
130
130
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
131
|
none: false
|
132
132
|
requirements:
|
@@ -135,10 +135,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
135
|
version: '0'
|
136
136
|
segments:
|
137
137
|
- 0
|
138
|
-
hash: -
|
138
|
+
hash: -1844692704935824375
|
139
139
|
requirements: []
|
140
140
|
rubyforge_project:
|
141
|
-
rubygems_version: 1.8.
|
141
|
+
rubygems_version: 1.8.19
|
142
142
|
signing_key:
|
143
143
|
specification_version: 3
|
144
144
|
summary: ''
|