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 CHANGED
@@ -19,7 +19,7 @@ class MyAPIClient
19
19
 
20
20
  entry_point 'http://myapp.com/api'
21
21
  auth :digest, 'user', 'password'
22
- http_options {headers: {'accept-encoding' => 'deflate, gzip'}}
22
+ http_options headers: {'accept-encoding' => 'deflate, gzip'}, debug: true
23
23
  end
24
24
  ````
25
25
 
data/examples/hal_shop.rb CHANGED
@@ -4,6 +4,7 @@ class HalShop
4
4
  include Hyperclient
5
5
 
6
6
  entry_point 'http://hal-shop.heroku.com'
7
+ http_options debug: true
7
8
  end
8
9
 
9
10
  def print_resources(resources)
data/lib/hyperclient.rb CHANGED
@@ -59,7 +59,7 @@ module Hyperclient
59
59
  #
60
60
  # Example:
61
61
  #
62
- # http_options {headers: {'accept-encoding' => 'deflate, gzip'}}
62
+ # http_options headers: {'accept-encoding' => 'deflate, gzip'}
63
63
  #
64
64
  # Returns a Hash.
65
65
  def http_options(options = {})
@@ -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
- ResourceFactory.resource(representation.delete('href'), {representation: representation, name: name})
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
@@ -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
 
@@ -69,7 +69,6 @@ module Hyperclient
69
69
  def initialize_representation(raw_representation)
70
70
  if raw_representation && !raw_representation.empty?
71
71
  @representation = Representation.new(raw_representation)
72
- @url = @representation.url if @representation.url
73
72
  end
74
73
  end
75
74
 
@@ -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'
@@ -1,3 +1,3 @@
1
1
  module Hyperclient
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -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 '{"_links": {"self": {"href": "/productions/1"}}}'
12
+ representation = Representation.new '{"title": "Hello world"}'
13
13
 
14
- representation.url.must_equal '/productions/1'
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.url.must_equal nil
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
@@ -23,6 +23,10 @@ module Hyperclient
23
23
 
24
24
  new_resource.object_id.must_equal resource.object_id
25
25
  end
26
+
27
+ it 'raises if the given url is nil' do
28
+ proc { ResourceFactory.resource(nil) }.must_raise MissingURLException
29
+ end
26
30
  end
27
31
  end
28
32
  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.5
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-23 00:00:00.000000000 Z
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: -2046368484385540531
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: -2046368484385540531
138
+ hash: -2196933980159133374
139
139
  requirements: []
140
140
  rubyforge_project:
141
141
  rubygems_version: 1.8.23