rlivsey-voorhees 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -119,7 +119,7 @@ This is used in instance methods:
119
119
  Like json_service, by default it assumes you're getting items of the same class, you can override this like so:
120
120
 
121
121
  def messages
122
- json_request(Message) do |r|
122
+ json_request(:class => Message) do |r|
123
123
  r.path = "/messages.json"
124
124
  r.parameters = {:user_id => self.id}
125
125
  end
@@ -127,6 +127,19 @@ Like json_service, by default it assumes you're getting items of the same class,
127
127
 
128
128
  User.new.messages => [Message, Message, ...]
129
129
 
130
+ By default a json_request call will convert the JSON to objects, you can make it return something else by setting the :returning property like so:
131
+
132
+ json_request(:returning => :raw) do |r|
133
+ ...
134
+ end
135
+
136
+ The returning property can be set to the following:
137
+
138
+ * :raw => the raw JSON response as a string
139
+ * :json => the JSON parsed to a ruby hash (through JSON.parse)
140
+ * :response => the Voorhees::Response object
141
+ * :objects => casts the JSON into objects (the default)
142
+
130
143
  #### Voorhees::Request
131
144
 
132
145
  Both json_service and json_request create Voorhees::Request objects to do their bidding.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.2.0
@@ -59,7 +59,7 @@ module Voorhees
59
59
 
60
60
  def perform
61
61
  setup_request
62
- parse_response(perform_actual_request)
62
+ build_response(perform_actual_request)
63
63
  end
64
64
 
65
65
  private
@@ -129,11 +129,8 @@ module Voorhees
129
129
  query_string_parts.size > 0 ? query_string_parts.join('&') : nil
130
130
  end
131
131
 
132
- def parse_response(response)
133
- Voorhees::Config[:response_class].new(JSON.parse(response.body), @caller_class, @hierarchy)
134
- rescue JSON::ParserError
135
- Voorhees.debug("Parsing JSON failed.\nFirst 500 chars of body:\n#{response.body[0...500]}")
136
- raise Voorhees::ParseError
132
+ def build_response(response)
133
+ Voorhees::Config[:response_class].new(response.body, @caller_class, @hierarchy)
137
134
  end
138
135
 
139
136
  def validate
@@ -25,7 +25,7 @@ module Voorhees
25
25
  (class << self; self; end).instance_eval do
26
26
  define_method name do |*args|
27
27
  params = args[0]
28
- json_request(klass) do |r|
28
+ json_request(:class => klass) do |r|
29
29
  r.parameters = params if params.is_a?(Hash)
30
30
  request_options.each do |option, value|
31
31
  r.send("#{option}=", value)
@@ -35,10 +35,21 @@ module Voorhees
35
35
  end
36
36
  end
37
37
 
38
- def json_request(klass=nil)
39
- request = Voorhees::Request.new(klass || self)
38
+ def json_request(options={})
39
+ request = Voorhees::Request.new(options[:class] || self)
40
40
  yield request
41
- request.perform.to_objects
41
+ response = request.perform
42
+
43
+ case options[:returning]
44
+ when :raw
45
+ response.body
46
+ when :json
47
+ response.json
48
+ when :response
49
+ response
50
+ else
51
+ response.to_objects
52
+ end
42
53
  end
43
54
  end
44
55
 
@@ -48,8 +59,8 @@ module Voorhees
48
59
  @json_attributes ||= @raw_json.keys.collect{|x| x.underscore.to_sym}
49
60
  end
50
61
 
51
- def json_request(klass=nil)
52
- self.class.json_request(klass) do |r|
62
+ def json_request(options={})
63
+ self.class.json_request(options) do |r|
53
64
  yield r
54
65
  end
55
66
  end
@@ -2,25 +2,32 @@ module Voorhees
2
2
 
3
3
  class Response
4
4
 
5
- attr_reader :json, :klass
5
+ attr_reader :body, :klass
6
6
 
7
- def initialize(json, klass=nil, hierarchy=nil)
8
- @json = json
7
+ def initialize(body, klass=nil, hierarchy=nil)
8
+ @body = body
9
9
  @hierarchy = hierarchy
10
10
  @klass = klass
11
11
  end
12
12
 
13
+ def json
14
+ @json ||= JSON.parse(@body)
15
+ rescue JSON::ParserError
16
+ Voorhees.debug("Parsing JSON failed.\nFirst 500 chars of body:\n#{response.body[0...500]}")
17
+ raise Voorhees::ParseError
18
+ end
19
+
13
20
  def to_objects
14
21
  return unless @klass
15
22
 
16
23
  raise Voorhees::NotResourceError.new unless @klass.respond_to?(:new_from_json)
17
24
 
18
- if @json.is_a?(Array)
19
- @json.collect do |item|
25
+ if json.is_a?(Array)
26
+ json.collect do |item|
20
27
  @klass.new_from_json(item, @hierarchy)
21
28
  end
22
29
  else
23
- @klass.new_from_json(@json, @hierarchy)
30
+ @klass.new_from_json(json, @hierarchy)
24
31
  end
25
32
  end
26
33
 
data/spec/request_spec.rb CHANGED
@@ -157,7 +157,7 @@ describe Voorhees::Request do
157
157
  body = '{"something":"result"}'
158
158
  @http_response = Net::HTTPResponse::CODE_TO_OBJ["200"].new("1.1", 200, body)
159
159
  @http_response.stub!(:body).and_return(body)
160
- @json_response = JSON.parse(body)
160
+ @json_response = body
161
161
 
162
162
  @mock_http = MockNetHttp.new
163
163
  @connection = @mock_http.connection
@@ -275,7 +275,7 @@ describe Voorhees::Request do
275
275
  @request.perform.should be_a(Voorhees::Response)
276
276
  end
277
277
 
278
- it "should pass the parsed JSON, caller class and hierarcy to the response" do
278
+ it "should pass the json body, caller class and hierarcy to the response" do
279
279
  @connection.stub!(:request).and_return(@http_response)
280
280
 
281
281
  Voorhees::Response.should_receive(:new).with(@json_response, @caller_class, @hierarchy)
@@ -317,7 +317,7 @@ describe Voorhees::Request do
317
317
  it "should return the response from the service" do
318
318
  @connection.should_receive(:request).with(@mock_get).exactly(1).times.ordered.and_raise(Timeout::Error.new(nil))
319
319
  @connection.should_receive(:request).with(@mock_get).exactly(1).times.ordered.and_return(@http_response)
320
- @request.perform.json.should == @json_response
320
+ @request.perform.body.should == @json_response
321
321
  end
322
322
 
323
323
  end
@@ -407,7 +407,7 @@ describe Voorhees::Request do
407
407
  it "should return the response from the service" do
408
408
  @connection.should_receive(:request).with(@mock_get).exactly(1).times.ordered.and_raise(Errno::ECONNREFUSED)
409
409
  @connection.should_receive(:request).with(@mock_get).exactly(1).times.ordered.and_return(@http_response)
410
- @request.perform.json.should == @json_response
410
+ @request.perform.body.should == @json_response
411
411
  end
412
412
  end
413
413
 
@@ -36,13 +36,19 @@ describe User do
36
36
  describe "json_request" do
37
37
 
38
38
  before :each do
39
+ @json_string= "{}"
40
+ @json_hash = {}
41
+
39
42
  @request = mock(:request, :null_object => true)
40
43
  @response = mock(:response, :null_object => true)
41
44
  @objects = [mock(:object)]
42
45
 
43
46
  Voorhees::Request.stub!(:new).and_return(@request)
44
47
  @request.stub!(:perform).and_return(@response)
48
+
45
49
  @response.stub!(:to_objects).and_return(@objects)
50
+ @response.stub!(:json).and_return(@json_hash)
51
+ @response.stub!(:body).and_return(@json_string)
46
52
  end
47
53
 
48
54
  def perform_request
@@ -57,7 +63,7 @@ describe User do
57
63
 
58
64
  it "should call Request.new with the specified class if a class is passed" do
59
65
  Voorhees::Request.should_receive(:new).with(Message).and_return(@request)
60
- User.json_request(Message) do |request|
66
+ User.json_request(:class => Message) do |request|
61
67
  # ...
62
68
  end
63
69
  end
@@ -80,9 +86,26 @@ describe User do
80
86
  perform_request
81
87
  end
82
88
 
83
- it "should return the result of Response#to_objects" do
89
+ it "should return the result of Response#to_objects if :returning is not set" do
84
90
  perform_request.should == @objects
85
91
  end
92
+
93
+ it "should return the JSON string if :returning is set to :raw" do
94
+ User.json_request(:returning => :raw){}.should == @json_string
95
+ end
96
+
97
+ it "should return the JSON hash if :returning is set to :json" do
98
+ User.json_request(:returning => :json){}.should == @json_hash
99
+ end
100
+
101
+ it "should return the objects string if :returning is set to :objects" do
102
+ User.json_request(:returning => :objects){}.should == @objects
103
+ end
104
+
105
+ it "should return the response if :returning is set to :response" do
106
+ User.json_request(:returning => :response){}.should == @response
107
+ end
108
+
86
109
  end
87
110
 
88
111
  describe "json_service" do
@@ -89,5 +89,5 @@ def build_response(fixture)
89
89
  @hierarchy = {
90
90
  :address => Address
91
91
  }
92
- @response = Voorhees::Response.new(JSON.parse(body), @klass, @hierarchy)
92
+ @response = Voorhees::Response.new(body, @klass, @hierarchy)
93
93
  end
data/voorhees.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{voorhees}
5
- s.version = "0.1.4"
5
+ s.version = "0.2.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Richard Livsey"]
9
- s.date = %q{2009-06-24}
9
+ s.date = %q{2009-07-01}
10
10
  s.email = %q{richard@livsey.org}
11
11
  s.extra_rdoc_files = [
12
12
  "LICENSE",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rlivsey-voorhees
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Livsey
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-24 00:00:00 -07:00
12
+ date: 2009-07-01 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15