rest_resource 0.0.5 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +3 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +2 -0
- data/README.md +7 -0
- data/lib/rest_resource.rb +3 -1
- data/lib/rest_resource/resource.rb +1 -2
- data/lib/rest_resource/value.rb +16 -0
- data/lib/rest_resource/version.rb +1 -1
- data/spec/rest_resource/value_spec.rb +40 -0
- metadata +7 -4
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -32,5 +32,12 @@ To use it, you can just do:
|
|
32
32
|
|
33
33
|
Both operations assume your web service controller returns a json string which can be initialized into an object.
|
34
34
|
|
35
|
+
The gem supports nested resources. For example, if user.address is an instance of Address class and needs to be so. You can specify the json (in ruby hash format) as the following:
|
36
|
+
|
37
|
+
class Address << RestResource::Resource
|
38
|
+
...
|
39
|
+
end
|
40
|
+
|
41
|
+
{"name" => "Leslie Cheung", ..., "mailing_address" => {"object" => {"address" => {"address_line_1" => "a street"}} }
|
35
42
|
##Contributors
|
36
43
|
* Yi Wen
|
data/lib/rest_resource.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require "rest_resource/version"
|
2
2
|
require 'rubygems'
|
3
3
|
require 'rest-client'
|
4
|
-
require 'active_support'
|
4
|
+
require 'active_support/core_ext/string'
|
5
|
+
require 'active_support/json'
|
5
6
|
require 'rest_resource/rest_crud'
|
7
|
+
require 'rest_resource/value'
|
6
8
|
require 'rest_resource/resource'
|
@@ -1,7 +1,6 @@
|
|
1
1
|
module RestResource
|
2
2
|
class Resource
|
3
3
|
attr_reader :attributes
|
4
|
-
private :attributes
|
5
4
|
|
6
5
|
class << self
|
7
6
|
def find(resource_id)
|
@@ -29,7 +28,7 @@ module RestResource
|
|
29
28
|
(class << self; self; end).class_eval do
|
30
29
|
params_hash.each_pair do |method_name, value|
|
31
30
|
define_method(method_name) do
|
32
|
-
value
|
31
|
+
Value.new(value).value
|
33
32
|
end
|
34
33
|
end
|
35
34
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module RestResource
|
2
|
+
class Value
|
3
|
+
attr_reader :raw_value
|
4
|
+
private :raw_value
|
5
|
+
def initialize(raw_value)
|
6
|
+
@raw_value = raw_value
|
7
|
+
end
|
8
|
+
|
9
|
+
def value
|
10
|
+
return raw_value if !raw_value.is_a?(Hash) or raw_value["object"].nil?
|
11
|
+
class_name = raw_value["object"].keys.first
|
12
|
+
klass = class_name.classify.constantize
|
13
|
+
klass.new raw_value["object"][class_name]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), "..", "spec_helper")
|
2
|
+
module RestResource
|
3
|
+
describe Value do
|
4
|
+
describe "#value" do
|
5
|
+
context "when the raw value is not a hash" do
|
6
|
+
it "should return the value" do
|
7
|
+
Value.new(23).value.should == 23
|
8
|
+
end
|
9
|
+
end
|
10
|
+
context "when the raw value is a hash" do
|
11
|
+
context "and the hash doesn't have the key object" do
|
12
|
+
it "should return the value" do
|
13
|
+
Value.new({"test" => "para"}).value.should == ({"test" => "para"})
|
14
|
+
end
|
15
|
+
end
|
16
|
+
context "and the hash has the key object" do
|
17
|
+
let(:params) {
|
18
|
+
{"object" => { "rest_resource/test_class" => {"attr1" => "val1"}}}
|
19
|
+
}
|
20
|
+
subject {Value.new params}
|
21
|
+
before(:each) do
|
22
|
+
RestResource::TestClass = Class.new(Resource)
|
23
|
+
end
|
24
|
+
|
25
|
+
after(:each) do
|
26
|
+
RestResource.send(:remove_const, "TestClass")
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should return an object of the type being indicated in the hash" do
|
30
|
+
subject.value.should be_a RestResource::TestClass
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should set the attribute" do
|
34
|
+
subject.value.attr1.should == "val1"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rest_resource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.5
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Yi Wen
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-10-
|
18
|
+
date: 2011-10-08 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -67,10 +67,12 @@ files:
|
|
67
67
|
- lib/rest_resource.rb
|
68
68
|
- lib/rest_resource/resource.rb
|
69
69
|
- lib/rest_resource/rest_crud.rb
|
70
|
+
- lib/rest_resource/value.rb
|
70
71
|
- lib/rest_resource/version.rb
|
71
72
|
- rest_resource.gemspec
|
72
73
|
- spec/rest_resource/resource_spec.rb
|
73
74
|
- spec/rest_resource/rest_crud_spec.rb
|
75
|
+
- spec/rest_resource/value_spec.rb
|
74
76
|
- spec/spec_helper.rb
|
75
77
|
- tasks/rspec.rake
|
76
78
|
has_rdoc: true
|
@@ -110,4 +112,5 @@ summary: A wrapper over rest-client providing basic CRUD restful web service ope
|
|
110
112
|
test_files:
|
111
113
|
- spec/rest_resource/resource_spec.rb
|
112
114
|
- spec/rest_resource/rest_crud_spec.rb
|
115
|
+
- spec/rest_resource/value_spec.rb
|
113
116
|
- spec/spec_helper.rb
|