rest_resource 0.1.0 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -5,7 +5,7 @@ rest_resource -- rest-client gem wrapper to provide a simple CRUD interface
5
5
 
6
6
 
7
7
  ## DESCRIPTION
8
- The gem provides a basic CRUD operation to the resources. It is basically rest-client wrapper. Once you use it, you have find, create, update and delete (Currently I implemented only find and create)
8
+ The gem provides a basic CRUD operation to the resources. It is basically rest-client wrapper. Once you use it, you have find, create, update and delete (Currently I implemented find, create and update)
9
9
 
10
10
  The goal of this gem is to provide a minimum CRUD client interface for the RESTful web services. It doesn't make the API to be ActiveRecord like. Rather, it lets the users have the full control on the resources, especially on the error handling.
11
11
 
@@ -30,6 +30,9 @@ To use it, you can just do:
30
30
 
31
31
  user = User.create :name => "Leslie Cheung", :login => "singer"
32
32
 
33
+ user = User.new :id => 12, :name => "Faye Wang"
34
+ user.save
35
+
33
36
  Both operations assume your web service controller returns a json string which can be initialized into an object.
34
37
 
35
38
  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:
@@ -11,18 +11,19 @@ module RestResource
11
11
  self.new(rest_crud.create(params))
12
12
  end
13
13
 
14
+ def rest_crud
15
+ RestCrud.new(url)
16
+ end
17
+
14
18
  private
15
19
 
16
20
  def url
17
21
  "#{site}/#{resource_name}"
18
22
  end
19
-
20
- def rest_crud
21
- RestCrud.new(url)
22
- end
23
23
  end
24
24
 
25
25
  def initialize(params={})
26
+ return if params.blank?
26
27
  params_hash = (params.is_a?(String)) ? ActiveSupport::JSON.decode(params) : params
27
28
  @attributes = params_hash
28
29
  (class << self; self; end).class_eval do
@@ -32,6 +33,10 @@ module RestResource
32
33
  end
33
34
  end
34
35
  end
35
- end
36
+ end
37
+ def save
38
+ self.class.rest_crud.update self.attributes
39
+ end
40
+
36
41
  end
37
42
  end
@@ -13,5 +13,9 @@ module RestResource
13
13
  def create(params)
14
14
  RestClient.post "#{url}.json", params
15
15
  end
16
+
17
+ def update(params)
18
+ RestClient.put "#{url}/#{params.delete(:id)}.json", params
19
+ end
16
20
  end
17
21
  end
@@ -1,3 +1,3 @@
1
1
  module RestResource
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -6,30 +6,29 @@ module RestResource
6
6
  let(:create_params) {{"attr1" => "val1"}}
7
7
  let(:klass) {
8
8
  Class.new(Resource) do
9
- def self.site
10
- "http://www.example.com"
11
- end
12
-
13
- def self.resource_name
14
- "resources"
15
- end
9
+ def self.site
10
+ "http://www.example.com"
11
+ end
16
12
 
13
+ def self.resource_name
14
+ "resources"
15
+ end
17
16
  end
18
17
  }
19
18
 
19
+ let(:object) {klass.new "id" => "123", "attr1" => "val"}
20
+ before(:each) do
21
+ RestCrud.stub(:new).and_return rest_crud
22
+ end
23
+
20
24
  %w(find create).each do |method_name|
21
25
  describe ".#{method_name}" do
22
- before(:each) do
23
- RestCrud.stub(:new).and_return rest_crud
24
- end
25
-
26
-
27
26
  it "should initialize a rest crud object" do
28
27
  RestCrud.should_receive(:new).with("http://www.example.com/resources").and_return rest_crud
29
28
  klass.send(method_name, send("#{method_name}_params"))
30
29
  end
31
30
 
32
- it "should ask rest crud to do the finding" do
31
+ it "should ask rest crud to do the work" do
33
32
  rest_crud.should_receive(method_name).with(send("#{method_name}_params"))
34
33
  klass.send(method_name, send("#{method_name}_params"))
35
34
  end
@@ -43,6 +42,13 @@ module RestResource
43
42
  end
44
43
  end
45
44
 
45
+ describe ".rest_crud" do
46
+ it "builds a rest crub instance" do
47
+ RestCrud.should_receive(:new).with("http://www.example.com/resources").and_return rest_crud
48
+ klass.rest_crud
49
+ end
50
+ end
51
+
46
52
  describe ".new" do
47
53
  context "when the params is a string" do
48
54
  let(:params) { ActiveSupport::JSON.encode( {"attr1" => "val1", "attr2" => "val2"} ) }
@@ -76,5 +82,12 @@ module RestResource
76
82
  end
77
83
  end
78
84
  end
85
+
86
+ describe "#save" do
87
+ it "should ask rest crud object to do the update" do
88
+ rest_crud.should_receive(:update).with(object.attributes)
89
+ object.save
79
90
  end
91
+ end
80
92
  end
93
+ end
@@ -3,18 +3,25 @@ require File.join(File.expand_path(File.dirname(__FILE__)), "..", "spec_helper")
3
3
  module RestResource
4
4
  describe RestCrud do
5
5
  subject {RestCrud.new("http://example.com/resources")}
6
- describe ".find" do
6
+ describe "#find" do
7
7
  it "should get the resource specified" do
8
8
  RestClient.should_receive(:get).with("http://example.com/resources/123.json")
9
9
  subject.find 123
10
10
  end
11
11
  end
12
12
 
13
- describe ".create" do
13
+ describe "#create" do
14
14
  it "should post the resource specified" do
15
15
  RestClient.should_receive(:post).with("http://example.com/resources.json", {:param1 => "value1", :param2 => "values2"})
16
16
  subject.create({:param1 => "value1", :param2 => "values2"})
17
17
  end
18
18
  end
19
+
20
+ describe "#update" do
21
+ it "should put the resource specified" do
22
+ RestClient.should_receive(:put).with("http://example.com/resources/1.json", {:param1 => "value1", :param2 => "values2"})
23
+ subject.update({:id => 1,:param1 => "value1", :param2 => "values2"})
24
+ end
25
+ end
19
26
  end
20
27
  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: 27
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 5
10
+ version: 0.1.5
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-08 00:00:00 -05:00
18
+ date: 2011-10-12 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency