giraffesoft-classy_resources 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 1
2
+ :patch: 0
3
3
  :major: 0
4
- :minor: 1
4
+ :minor: 2
@@ -1,6 +1,7 @@
1
1
  dir = File.expand_path(File.dirname(__FILE__))
2
2
  $LOAD_PATH << dir unless $LOAD_PATH.include?(dir)
3
3
  require 'classy_resources/mime_type'
4
+ require 'classy_resources/post_body_params'
4
5
 
5
6
  module ClassyResources
6
7
  def class_for(resource)
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'json'
3
+
4
+ module ClassyResources
5
+
6
+ # A Rack middleware for parsing POST/PUT body data when Content-Type is
7
+ # not one of the standard supported types, like <tt>application/json</tt>.
8
+ #
9
+ # TODO: Find a better name.
10
+ #
11
+ class PostBodyParams
12
+
13
+ # Constants
14
+ #
15
+ CONTENT_TYPE = 'CONTENT_TYPE'.freeze
16
+ POST_BODY = 'rack.input'.freeze
17
+ FORM_INPUT = 'rack.request.form_input'.freeze
18
+ FORM_HASH = 'rack.request.form_hash'.freeze
19
+
20
+ # Supported Content-Types
21
+ #
22
+ APPLICATION_JSON = 'application/json'.freeze
23
+
24
+ def initialize(app)
25
+ @app = app
26
+ end
27
+
28
+ def call(env)
29
+ case env[CONTENT_TYPE]
30
+ when APPLICATION_JSON
31
+ env.update(FORM_HASH => JSON.parse(env[POST_BODY].read), FORM_INPUT => env[POST_BODY])
32
+ end
33
+ @app.call(env)
34
+ end
35
+
36
+ end
37
+ end
38
+
@@ -0,0 +1,41 @@
1
+ require 'rubygems'
2
+ require 'json'
3
+
4
+ module ClassyResources
5
+
6
+ # A Rack middleware for parsing POST/PUT body data when Content-Type is
7
+ # not one of the standard supported types, like <tt>application/json</tt>.
8
+ #
9
+ # TODO: Find a better name.
10
+ #
11
+ class PostBodyParams
12
+
13
+ # Constants
14
+ #
15
+ CONTENT_TYPE = 'CONTENT_TYPE'.freeze
16
+ POST_BODY = 'rack.input'.freeze
17
+ FORM_INPUT = 'rack.request.form_input'.freeze
18
+ FORM_HASH = 'rack.request.form_hash'.freeze
19
+
20
+ # Supported Content-Types
21
+ #
22
+ APPLICATION_JSON = 'application/json'.freeze
23
+ APPLICATION_XML = 'application/xml'.freeze
24
+
25
+ def initialize(app)
26
+ @app = app
27
+ end
28
+
29
+ def call(env)
30
+ case env[CONTENT_TYPE]
31
+ when APPLICATION_JSON
32
+ env.update(FORM_HASH => JSON.parse(env[POST_BODY].read), FORM_INPUT => env[POST_BODY])
33
+ when APPLICATION_XML
34
+ env.update(FORM_HASH => Hash.from_xml(env[POST_BODY].read), FORM_INPUT => env[POST_BODY])
35
+ end
36
+ @app.call(env)
37
+ end
38
+
39
+ end
40
+ end
41
+
@@ -102,4 +102,32 @@ class ActiveRecordTest < Test::Unit::TestCase
102
102
  expect { assert_equal "/comments/#{@post.comments.reload.first.id}.xml", @response.location }
103
103
  expect { assert_equal 1, @post.comments.reload.count }
104
104
  end
105
+
106
+ context "on POST to /posts/id/comments with a JSON post body" do
107
+ setup do
108
+ @post = create_post
109
+ post "/posts/#{@post.id}/comments.xml", {:comment => hash_for_comment(:author => 'james')}.to_json,
110
+ :content_type => 'application/json'
111
+ end
112
+
113
+ expect { assert_equal 302, @response.status }
114
+ expect { assert_equal "application/xml", @response.content_type }
115
+ expect { assert_equal "/comments/#{@post.comments.reload.first.id}.xml", @response.location }
116
+ expect { assert_equal 1, @post.comments.reload.count }
117
+ expect { assert_equal 'james', @post.comments.first.author }
118
+ end
119
+
120
+ context "on POST to /posts/id/comments with a XML post body" do
121
+ setup do
122
+ @post = create_post
123
+ post "/posts/#{@post.id}/comments.xml", Comment.new(:author => 'james').to_xml,
124
+ :content_type => 'application/xml'
125
+ end
126
+
127
+ expect { assert_equal 302, @response.status }
128
+ expect { assert_equal "application/xml", @response.content_type }
129
+ expect { assert_equal "/comments/#{@post.comments.reload.first.id}.xml", @response.location }
130
+ expect { assert_equal 1, @post.comments.reload.count }
131
+ expect { assert_equal 'james', @post.comments.first.author }
132
+ end
105
133
  end
@@ -37,3 +37,5 @@ define_resource :posts, :collection => [:get, :post],
37
37
  define_resource :comments, :collection => [:get, :post],
38
38
  :parent => :posts
39
39
 
40
+ use ClassyResources::PostBodyParams
41
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: giraffesoft-classy_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Golick
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-28 00:00:00 -08:00
12
+ date: 2009-01-29 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -27,6 +27,8 @@ files:
27
27
  - lib/classy_resources
28
28
  - lib/classy_resources/active_record.rb
29
29
  - lib/classy_resources/mime_type.rb
30
+ - lib/classy_resources/post_body_param_parsing.rb
31
+ - lib/classy_resources/post_body_params.rb
30
32
  - lib/classy_resources/sequel.rb
31
33
  - lib/classy_resources.rb
32
34
  - test/active_record_test.rb