hyperclient 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Readme.md +1 -1
- data/examples/hal_shop.rb +1 -0
- data/lib/hyperclient.rb +1 -1
- data/lib/hyperclient/discoverer.rb +14 -1
- data/lib/hyperclient/http.rb +15 -0
- data/lib/hyperclient/representation.rb +0 -9
- data/lib/hyperclient/resource.rb +0 -1
- data/lib/hyperclient/resource_factory.rb +20 -0
- data/lib/hyperclient/version.rb +1 -1
- data/test/hyperclient/http_test.rb +15 -0
- data/test/hyperclient/representation_test.rb +3 -15
- data/test/hyperclient/resource_factory_test.rb +4 -0
- data/test/hyperclient/resource_test.rb +0 -12
- metadata +4 -4
data/Readme.md
CHANGED
data/examples/hal_shop.rb
CHANGED
data/lib/hyperclient.rb
CHANGED
@@ -55,7 +55,20 @@ 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
|
-
|
58
|
+
url = extract_url(representation)
|
59
|
+
ResourceFactory.resource(url, {representation: representation, name: name})
|
60
|
+
end
|
61
|
+
|
62
|
+
# Internal: Returns a String with the resource URL
|
63
|
+
#
|
64
|
+
# representation - The JSON representation of the resource.
|
65
|
+
def extract_url(representation)
|
66
|
+
return representation['href'] if representation.include?('href')
|
67
|
+
|
68
|
+
if representation && representation['_links'] && representation['_links']['self'] &&
|
69
|
+
(url = representation['_links']['self']['href'])
|
70
|
+
return url
|
71
|
+
end
|
59
72
|
end
|
60
73
|
end
|
61
74
|
end
|
data/lib/hyperclient/http.rb
CHANGED
@@ -26,6 +26,7 @@ module Hyperclient
|
|
26
26
|
@resource = resource
|
27
27
|
authenticate(options[:auth]) if options && options.include?(:auth)
|
28
28
|
headers(options[:headers]) if options && options.include?(:headers)
|
29
|
+
enable_debug(options[:debug]) if options && options.include?(:debug)
|
29
30
|
end
|
30
31
|
|
31
32
|
# Public: Sends a GET request the the resource url.
|
@@ -99,5 +100,19 @@ module Hyperclient
|
|
99
100
|
def headers(headers)
|
100
101
|
self.class.send(:headers, headers)
|
101
102
|
end
|
103
|
+
|
104
|
+
# Internal: Enables HTTP debugging.
|
105
|
+
#
|
106
|
+
# stream - An object to stream the HTTP out to or just a truthy value. If
|
107
|
+
# it's truthy it will output to $stderr.
|
108
|
+
def enable_debug(stream)
|
109
|
+
return unless stream
|
110
|
+
|
111
|
+
if stream.respond_to?(:<<)
|
112
|
+
self.class.debug_output(stream)
|
113
|
+
else
|
114
|
+
self.class.debug_output
|
115
|
+
end
|
116
|
+
end
|
102
117
|
end
|
103
118
|
end
|
@@ -37,15 +37,6 @@ module Hyperclient
|
|
37
37
|
def attributes
|
38
38
|
@attributes ||= @representation.dup.delete_if {|key, value| key =~ /^_/}
|
39
39
|
end
|
40
|
-
|
41
|
-
# Public: Returns a String with the resource URL or nil of it does not have
|
42
|
-
# one.
|
43
|
-
def url
|
44
|
-
if @representation && @representation['_links'] && @representation['_links']['self'] &&
|
45
|
-
(url = @representation['_links']['self']['href'])
|
46
|
-
return url
|
47
|
-
end
|
48
|
-
end
|
49
40
|
end
|
50
41
|
end
|
51
42
|
|
data/lib/hyperclient/resource.rb
CHANGED
@@ -14,7 +14,12 @@ module Hyperclient
|
|
14
14
|
#
|
15
15
|
# url - A String to identify the Resource
|
16
16
|
# args - An Array to pass other arguments to the Resource initialization.
|
17
|
+
#
|
18
|
+
# Raises MissingURLException if no url given.
|
19
|
+
# Returns a Resource.
|
17
20
|
def self.resource(url, *args)
|
21
|
+
raise MissingURLException.new(args) unless url
|
22
|
+
|
18
23
|
identity_map.fetch(url) do |url|
|
19
24
|
resource = Resource.new(url, *args)
|
20
25
|
identity_map.update(url => resource)
|
@@ -28,6 +33,21 @@ module Hyperclient
|
|
28
33
|
@identity_map ||= {}
|
29
34
|
end
|
30
35
|
end
|
36
|
+
|
37
|
+
# Public: Exception that is raised when building a Resource without a URL.
|
38
|
+
class MissingURLException < StandardError
|
39
|
+
# Public: Initializes a MissingURLException
|
40
|
+
#
|
41
|
+
# args - An Array of the args the were to be used to build the Resource.
|
42
|
+
def initialize(args)
|
43
|
+
@args = args
|
44
|
+
end
|
45
|
+
|
46
|
+
# Public: Returns a String with the exception message.
|
47
|
+
def message
|
48
|
+
"Cannot build Resource without a URL, given args were: #{@args.inspect}"
|
49
|
+
end
|
50
|
+
end
|
31
51
|
end
|
32
52
|
|
33
53
|
require 'hyperclient/resource'
|
data/lib/hyperclient/version.rb
CHANGED
@@ -35,6 +35,21 @@ module Hyperclient
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
+
describe 'debug' do
|
39
|
+
it 'enables debugging' do
|
40
|
+
http = HTTP.new(resource, {debug: true})
|
41
|
+
|
42
|
+
http.class.instance_variable_get(:@default_options)[:debug_output].must_equal $stderr
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'uses a custom stream' do
|
46
|
+
stream = StringIO.new
|
47
|
+
http = HTTP.new(resource, {debug: stream})
|
48
|
+
|
49
|
+
http.class.instance_variable_get(:@default_options)[:debug_output].must_equal stream
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
38
53
|
describe 'get' do
|
39
54
|
it 'sends a GET request and returns the response body' do
|
40
55
|
stub_request(:get, 'api.example.org/productions/1').
|
@@ -9,15 +9,15 @@ module Hyperclient
|
|
9
9
|
|
10
10
|
describe 'intialize' do
|
11
11
|
it 'handles non-hash representations' do
|
12
|
-
representation = Representation.new '{"
|
12
|
+
representation = Representation.new '{"title": "Hello world"}'
|
13
13
|
|
14
|
-
representation.
|
14
|
+
representation.attributes['title'].must_equal 'Hello world'
|
15
15
|
end
|
16
16
|
|
17
17
|
it 'does not raise when non-JSON response is given' do
|
18
18
|
representation = Representation.new 'This is not JSON'
|
19
19
|
|
20
|
-
representation.
|
20
|
+
representation.attributes.must_equal({})
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -48,17 +48,5 @@ module Hyperclient
|
|
48
48
|
representation.resources.episodes.last.must_be_kind_of Resource
|
49
49
|
end
|
50
50
|
end
|
51
|
-
|
52
|
-
describe 'url' do
|
53
|
-
it 'returns the url of the resource grabbed from the representation' do
|
54
|
-
representation.url.must_equal '/productions/1'
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'returns nil when the representation does not include the resource url' do
|
58
|
-
representation = Representation.new({_links: {media: {href: '/media/1'}}})
|
59
|
-
|
60
|
-
representation.url.must_equal nil
|
61
|
-
end
|
62
|
-
end
|
63
51
|
end
|
64
52
|
end
|
@@ -38,18 +38,6 @@ module Hyperclient
|
|
38
38
|
assert_not_requested(:get, 'http://api.example.org/')
|
39
39
|
end
|
40
40
|
|
41
|
-
it 'updates the resource URL if the representation has one' do
|
42
|
-
resource = Resource.new('/', {representation: JSON.parse(representation)})
|
43
|
-
|
44
|
-
resource.url.must_include '/productions/1'
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'does no update the resource URL if the representation does not have one' do
|
48
|
-
resource = Resource.new('/', {})
|
49
|
-
|
50
|
-
resource.url.wont_include '/productions/1'
|
51
|
-
end
|
52
|
-
|
53
41
|
it 'sets the resource name' do
|
54
42
|
resource = Resource.new('/', {name: 'posts'})
|
55
43
|
|
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.6
|
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-06-
|
12
|
+
date: 2012-06-30 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: -2196933980159133374
|
130
130
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
131
|
none: false
|
132
132
|
requirements:
|
@@ -135,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
135
|
version: '0'
|
136
136
|
segments:
|
137
137
|
- 0
|
138
|
-
hash: -
|
138
|
+
hash: -2196933980159133374
|
139
139
|
requirements: []
|
140
140
|
rubyforge_project:
|
141
141
|
rubygems_version: 1.8.23
|