activeresource_json_patch 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +46 -0
- data/VERSION +1 -0
- data/activeresource_json_patch.gemspec +25 -0
- data/features/include_root_in_json.feature +44 -0
- data/features/step_definitions/include_root_in_json_steps.rb +27 -0
- data/features/support/env.rb +3 -0
- data/lib/activeresource_json_patch.rb +17 -0
- data/readme.md +91 -0
- metadata +124 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
yasm (0.0.0)
|
5
|
+
activeresource (~> 3.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (3.0.7)
|
11
|
+
activesupport (= 3.0.7)
|
12
|
+
builder (~> 2.1.2)
|
13
|
+
i18n (~> 0.5.0)
|
14
|
+
activeresource (3.0.7)
|
15
|
+
activemodel (= 3.0.7)
|
16
|
+
activesupport (= 3.0.7)
|
17
|
+
activesupport (3.0.7)
|
18
|
+
builder (2.1.2)
|
19
|
+
cucumber (0.10.2)
|
20
|
+
builder (>= 2.1.2)
|
21
|
+
diff-lcs (>= 1.1.2)
|
22
|
+
gherkin (>= 2.3.5)
|
23
|
+
json (>= 1.4.6)
|
24
|
+
term-ansicolor (>= 1.0.5)
|
25
|
+
diff-lcs (1.1.2)
|
26
|
+
gherkin (2.3.6)
|
27
|
+
json (>= 1.4.6)
|
28
|
+
i18n (0.5.0)
|
29
|
+
json (1.5.1)
|
30
|
+
rspec (2.4.0)
|
31
|
+
rspec-core (~> 2.4.0)
|
32
|
+
rspec-expectations (~> 2.4.0)
|
33
|
+
rspec-mocks (~> 2.4.0)
|
34
|
+
rspec-core (2.4.0)
|
35
|
+
rspec-expectations (2.4.0)
|
36
|
+
diff-lcs (~> 1.1.2)
|
37
|
+
rspec-mocks (2.4.0)
|
38
|
+
term-ansicolor (1.0.5)
|
39
|
+
|
40
|
+
PLATFORMS
|
41
|
+
ruby
|
42
|
+
|
43
|
+
DEPENDENCIES
|
44
|
+
cucumber (~> 0.10.0)
|
45
|
+
rspec (~> 2.4.0)
|
46
|
+
yasm!
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
@@ -0,0 +1,25 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = %q{activeresource_json_patch}
|
3
|
+
s.version = File.read "VERSION"
|
4
|
+
|
5
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
6
|
+
s.authors = ["Matt Parker"]
|
7
|
+
s.date = %q{2011-04-24}
|
8
|
+
s.summary = %q{JSON fix for ActiveResource (Rails 3).}
|
9
|
+
s.description = %q{Fixes json formatting problem with ActiveResource.}
|
10
|
+
s.email = %q{moonmaster9000@gmail.com}
|
11
|
+
s.extra_rdoc_files = [
|
12
|
+
"readme.md"
|
13
|
+
]
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
|
18
|
+
s.homepage = %q{http://github.com/moonmaster9000/activeresource_json_patch}
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
s.rubygems_version = %q{1.5.0}
|
21
|
+
|
22
|
+
s.add_dependency(%q<activeresource>, ["~> 3.0"])
|
23
|
+
s.add_development_dependency(%q<cucumber>, ["~> 0.10.0"])
|
24
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.4.0"])
|
25
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
Feature: Include the root (and only the root) in the JSON
|
2
|
+
|
3
|
+
Scenario: Including the root in the JSON, but not adding roots to any child elements
|
4
|
+
Given that "/books/2001-space-odyssey.json" returns:
|
5
|
+
"""
|
6
|
+
{
|
7
|
+
"title": "2001: A Space Odyssey",
|
8
|
+
"id": "2001-space-odyssey",
|
9
|
+
"genres": [
|
10
|
+
{
|
11
|
+
"name": "Fiction",
|
12
|
+
"id": "fiction"
|
13
|
+
},
|
14
|
+
{
|
15
|
+
"name": "Science Fiction",
|
16
|
+
"id": "science-fiction"
|
17
|
+
}
|
18
|
+
],
|
19
|
+
"author": nil
|
20
|
+
}
|
21
|
+
"""
|
22
|
+
When I retrieve the book via ActiveResource
|
23
|
+
And I change the author to "Arthur C. Clarke"
|
24
|
+
And I save the book
|
25
|
+
Then ActiveResource should send the following back to the server:
|
26
|
+
"""
|
27
|
+
{
|
28
|
+
"book": {
|
29
|
+
"author": "Arthur C. Clarke",
|
30
|
+
"title": "2001: A Space Odyssey",
|
31
|
+
"id": "2001-space-odyssey",
|
32
|
+
"genres": [
|
33
|
+
{
|
34
|
+
"name": "Fiction",
|
35
|
+
"id": "fiction"
|
36
|
+
},
|
37
|
+
{
|
38
|
+
"name": "Science Fiction",
|
39
|
+
"id": "science-fiction"
|
40
|
+
}
|
41
|
+
]
|
42
|
+
}
|
43
|
+
}
|
44
|
+
"""
|
@@ -0,0 +1,27 @@
|
|
1
|
+
Given /^that "([^"]*)" returns:$/ do |path, json_get_body|
|
2
|
+
@path = path
|
3
|
+
ActiveResource::HttpMock.respond_to do |mock|
|
4
|
+
mock.get @path, {}, json_get_body
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
When /^I retrieve the book via ActiveResource$/ do
|
9
|
+
class Book < ActiveResource::Base; self.site = 'http://www.example.com'; self.format = :json; end
|
10
|
+
@book = Book.find "2001-space-odyssey"
|
11
|
+
end
|
12
|
+
|
13
|
+
When /^I change the author to "([^"]*)"$/ do |author|
|
14
|
+
@book.author = author
|
15
|
+
end
|
16
|
+
|
17
|
+
When /^I save the book$/ do
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
Then /^ActiveResource should send the following back to the server:$/ do |book_body|
|
22
|
+
update_book = ActiveResource::Request.new(:put, @path, book_body.gsub(/^\s+/, "").gsub("\n", "").gsub(": \"", ":\""), {"Content-Type" => "application/json"})
|
23
|
+
updated_response = ActiveResource::Response.new("", 204, {"Location" => @path})
|
24
|
+
ActiveResource::HttpMock.respond_to({update_book => updated_response})
|
25
|
+
@book.save
|
26
|
+
ActiveResource::HttpMock.requests.include?(update_book).should be_true
|
27
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'active_resource'
|
2
|
+
|
3
|
+
module ActiveResource
|
4
|
+
class Base
|
5
|
+
def encode_with_json(options={})
|
6
|
+
case self.class.format
|
7
|
+
when ActiveResource::Formats[:json]
|
8
|
+
data = self.class.include_root_in_json ? {self.class.element_name => attributes} : attributes
|
9
|
+
self.class.format.encode data
|
10
|
+
else
|
11
|
+
encode_without_json(options)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
alias_method_chain :encode, :json
|
16
|
+
end
|
17
|
+
end
|
data/readme.md
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
# ActiveResource JSON Patch
|
2
|
+
|
3
|
+
Why? Because the ActiveResource (Rails 3) JSON format is broken, and this gem will fix it until ActiveResource gets patched.
|
4
|
+
|
5
|
+
## The problem
|
6
|
+
|
7
|
+
Imagine you fetch a book over ActiveResource.
|
8
|
+
|
9
|
+
@book = Book.find '2001-space-odyssey'
|
10
|
+
|
11
|
+
The JSON that came to you looked like this:
|
12
|
+
|
13
|
+
{
|
14
|
+
"title": "2001: A Space Odyssey",
|
15
|
+
"id": "2001-space-odyseey",
|
16
|
+
"genres": [
|
17
|
+
{
|
18
|
+
"name": "Fiction",
|
19
|
+
"id": "fiction"
|
20
|
+
},
|
21
|
+
{
|
22
|
+
"name": "Science Fiction",
|
23
|
+
"id": "science-fiction"
|
24
|
+
}
|
25
|
+
],
|
26
|
+
"author": nil
|
27
|
+
}
|
28
|
+
|
29
|
+
So far, so good.
|
30
|
+
|
31
|
+
Now, let's update the author attribute and save it.
|
32
|
+
|
33
|
+
@book.author = "Arthur C. Clarke"
|
34
|
+
@book.save
|
35
|
+
|
36
|
+
Great! But wait, let's inspect the JSON that it sent to the server on save:
|
37
|
+
|
38
|
+
{
|
39
|
+
"book": {
|
40
|
+
"title": "2001: A Space Odyssey",
|
41
|
+
"id": "2001-space-odyseey",
|
42
|
+
"genres": [
|
43
|
+
{
|
44
|
+
"genre": {
|
45
|
+
"name": "Fiction",
|
46
|
+
"id": "fiction"
|
47
|
+
}
|
48
|
+
},
|
49
|
+
{
|
50
|
+
"genre": {
|
51
|
+
"name": "Science Fiction",
|
52
|
+
"id": "science-fiction"
|
53
|
+
}
|
54
|
+
}
|
55
|
+
],
|
56
|
+
"author": "Arthur C. Clarke"
|
57
|
+
}
|
58
|
+
}
|
59
|
+
|
60
|
+
Not cool! By default, `ActiveResource::Base.include_root_in_json` is `true` - which is fine. Except it doesn't just include the `"book"` root, it added `"genre"` roots
|
61
|
+
to all of the `"genres"`! This will completely fuck up your document.
|
62
|
+
|
63
|
+
## The Solution
|
64
|
+
|
65
|
+
Step 1: Install this gem, `activeresource_json_patch`: either add it to your `Gemfile` and `bundle install`, or simply `gem install activeresource_json_patch`
|
66
|
+
|
67
|
+
Step 2: Require it in your codebase. If you included this in a Rails project via a `Gemfile`, then you're good to go. Otherwise, `require activeresource_json_patch` somewhere in your code.
|
68
|
+
|
69
|
+
Now, with the gem installed and loaded, were you to repeat the above steps, the JSON it will would send back would not inlude the superfluous `"genre"` roots:
|
70
|
+
|
71
|
+
{
|
72
|
+
"book": {
|
73
|
+
"title": "2001: A Space Odyssey",
|
74
|
+
"id": "2001-space-odyseey",
|
75
|
+
"genres": [
|
76
|
+
{
|
77
|
+
"name": "Fiction",
|
78
|
+
"id": "fiction"
|
79
|
+
},
|
80
|
+
{
|
81
|
+
"name": "Science Fiction",
|
82
|
+
"id": "science-fiction"
|
83
|
+
}
|
84
|
+
],
|
85
|
+
"author": "Arthur C. Clarke"
|
86
|
+
}
|
87
|
+
}
|
88
|
+
|
89
|
+
## LICENSE
|
90
|
+
|
91
|
+
None. This is PUBLIC DOMAIN. Do ANYTHING!
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activeresource_json_patch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 31
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 0.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Matt Parker
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-04-24 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: activeresource
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 3
|
32
|
+
- 0
|
33
|
+
version: "3.0"
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: cucumber
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 55
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
- 10
|
48
|
+
- 0
|
49
|
+
version: 0.10.0
|
50
|
+
type: :development
|
51
|
+
version_requirements: *id002
|
52
|
+
- !ruby/object:Gem::Dependency
|
53
|
+
name: rspec
|
54
|
+
prerelease: false
|
55
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ~>
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 31
|
61
|
+
segments:
|
62
|
+
- 2
|
63
|
+
- 4
|
64
|
+
- 0
|
65
|
+
version: 2.4.0
|
66
|
+
type: :development
|
67
|
+
version_requirements: *id003
|
68
|
+
description: Fixes json formatting problem with ActiveResource.
|
69
|
+
email: moonmaster9000@gmail.com
|
70
|
+
executables: []
|
71
|
+
|
72
|
+
extensions: []
|
73
|
+
|
74
|
+
extra_rdoc_files:
|
75
|
+
- readme.md
|
76
|
+
files:
|
77
|
+
- .gitignore
|
78
|
+
- Gemfile
|
79
|
+
- Gemfile.lock
|
80
|
+
- VERSION
|
81
|
+
- activeresource_json_patch.gemspec
|
82
|
+
- features/include_root_in_json.feature
|
83
|
+
- features/step_definitions/include_root_in_json_steps.rb
|
84
|
+
- features/support/env.rb
|
85
|
+
- lib/activeresource_json_patch.rb
|
86
|
+
- readme.md
|
87
|
+
has_rdoc: true
|
88
|
+
homepage: http://github.com/moonmaster9000/activeresource_json_patch
|
89
|
+
licenses: []
|
90
|
+
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
hash: 3
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: "0"
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
hash: 3
|
111
|
+
segments:
|
112
|
+
- 0
|
113
|
+
version: "0"
|
114
|
+
requirements: []
|
115
|
+
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 1.6.2
|
118
|
+
signing_key:
|
119
|
+
specification_version: 3
|
120
|
+
summary: JSON fix for ActiveResource (Rails 3).
|
121
|
+
test_files:
|
122
|
+
- features/include_root_in_json.feature
|
123
|
+
- features/step_definitions/include_root_in_json_steps.rb
|
124
|
+
- features/support/env.rb
|