rest_resource 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,6 +1,7 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gem 'rest-client'
4
+ gem 'httparty'
4
5
  gem 'i18n'
5
6
  gem 'activesupport'
6
7
  gem 'rake'
@@ -1,29 +1,34 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- activesupport (3.1.0)
4
+ activesupport (3.1.3)
5
5
  multi_json (~> 1.0)
6
6
  diff-lcs (1.1.3)
7
+ httparty (0.8.1)
8
+ multi_json
9
+ multi_xml
7
10
  i18n (0.6.0)
8
- mime-types (1.16)
9
- multi_json (1.0.3)
10
- rake (0.9.2)
11
+ mime-types (1.17.2)
12
+ multi_json (1.0.4)
13
+ multi_xml (0.4.1)
14
+ rake (0.9.2.2)
11
15
  rest-client (1.6.7)
12
16
  mime-types (>= 1.16)
13
- rspec (2.6.0)
14
- rspec-core (~> 2.6.0)
15
- rspec-expectations (~> 2.6.0)
16
- rspec-mocks (~> 2.6.0)
17
- rspec-core (2.6.4)
18
- rspec-expectations (2.6.0)
17
+ rspec (2.7.0)
18
+ rspec-core (~> 2.7.0)
19
+ rspec-expectations (~> 2.7.0)
20
+ rspec-mocks (~> 2.7.0)
21
+ rspec-core (2.7.1)
22
+ rspec-expectations (2.7.0)
19
23
  diff-lcs (~> 1.1.2)
20
- rspec-mocks (2.6.0)
24
+ rspec-mocks (2.7.0)
21
25
 
22
26
  PLATFORMS
23
27
  ruby
24
28
 
25
29
  DEPENDENCIES
26
30
  activesupport
31
+ httparty
27
32
  i18n
28
33
  rake
29
34
  rest-client
data/README.md CHANGED
@@ -33,6 +33,8 @@ To use it, you can just do:
33
33
  user = User.new :id => 12, :name => "Faye Wang"
34
34
  user.save
35
35
 
36
+ users = User.all :active => true
37
+
36
38
  Both operations assume your web service controller returns a json string which can be initialized into an object.
37
39
 
38
40
  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:
@@ -1,6 +1,7 @@
1
1
  require "rest_resource/version"
2
2
  require 'rubygems'
3
3
  require 'rest-client'
4
+ require 'httparty'
4
5
  require 'active_support/core_ext/string'
5
6
  require 'active_support/json'
6
7
  require 'rest_resource/rest_crud'
@@ -3,21 +3,26 @@ module RestResource
3
3
  attr_reader :attributes
4
4
 
5
5
  class << self
6
- def find(resource_id)
7
- self.new(rest_crud.find(resource_id))
6
+ def find(resource_id, extra={})
7
+ self.new(rest_crud(extra).find(resource_id))
8
8
  end
9
9
 
10
10
  def create(params)
11
- self.new(rest_crud.create(params))
11
+ self.new(rest_crud(params).create(params))
12
12
  end
13
13
 
14
- def rest_crud
15
- RestCrud.new(url)
14
+ def all(params={})
15
+ val = ActiveSupport::JSON.decode(rest_crud(params).all(params))
16
+ val.map{|a|self.new(a)}
17
+ end
18
+
19
+ def rest_crud(params={})
20
+ RestCrud.new(url(params))
16
21
  end
17
22
 
18
23
  private
19
24
 
20
- def url
25
+ def url(params={})
21
26
  "#{site}/#{resource_name}"
22
27
  end
23
28
  end
@@ -1,8 +1,12 @@
1
1
  module RestResource
2
2
  class RestCrud
3
- attr_reader :url
3
+ attr_reader :url, :http
4
4
  private :url
5
5
  def initialize(url)
6
+ @http = Class.new do
7
+ include HTTParty
8
+ base_uri "#{url}"
9
+ end
6
10
  @url = url
7
11
  end
8
12
 
@@ -10,6 +14,10 @@ module RestResource
10
14
  RestClient.get "#{url}/#{resource_id}.json"
11
15
  end
12
16
 
17
+ def all(params)
18
+ RestClient.get "#{url}.json", params
19
+ end
20
+
13
21
  def create(params)
14
22
  RestClient.post "#{url}.json", params
15
23
  end
@@ -0,0 +1,5 @@
1
+ module RestResource
2
+ class UnprocessableEntity < StandardError
3
+
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module RestResource
2
- VERSION = "0.1.5"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -15,6 +15,7 @@ Gem::Specification.new do |s|
15
15
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
16
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
17
  s.require_paths = ["lib"]
18
+ s.add_runtime_dependency(%q<httparty>, [">= 0"])
18
19
  s.add_runtime_dependency(%q<rest-client>, [">= 0"])
19
20
  s.add_runtime_dependency(%q<activesupport>, [">= 0"])
20
21
  end
@@ -1,8 +1,12 @@
1
1
  require File.join(File.expand_path(File.dirname(__FILE__)), "..", "spec_helper")
2
2
  module RestResource
3
3
  describe Resource do
4
- let(:rest_crud) {double :rest_crud, :find => "{}", :create => "{}"}
4
+ let(:rest_crud) {
5
+ double :rest_crud, :find => "{}", :create => "{}",
6
+ :all => "{}"
7
+ }
5
8
  let(:find_params) {123}
9
+ let(:all_params) {{:condition => "a condition"}}
6
10
  let(:create_params) {{"attr1" => "val1"}}
7
11
  let(:klass) {
8
12
  Class.new(Resource) do
@@ -21,24 +25,40 @@ module RestResource
21
25
  RestCrud.stub(:new).and_return rest_crud
22
26
  end
23
27
 
28
+ shared_examples "general behavior" do
29
+ it "should initialize a rest crud object" do
30
+ RestCrud.should_receive(:new).with("http://www.example.com/resources").and_return rest_crud
31
+ klass.send(method_name, send("#{method_name}_params"))
32
+ end
33
+
34
+ it "should ask rest crud to do the work" do
35
+ rest_crud.should_receive(method_name).with(send("#{method_name}_params"))
36
+ klass.send(method_name, send("#{method_name}_params"))
37
+ end
38
+ end
39
+ shared_examples "singular resource fetching" do
40
+ it "should intialize a resource object" do
41
+ rest_crud.stub(:find).and_return(ActiveSupport::JSON.encode("attr1" => "val1", "attr2" => "val2"))
42
+ object = klass.send(method_name, send("#{method_name}_params"))
43
+ object.should be_a klass
44
+ end
45
+ end
24
46
  %w(find create).each do |method_name|
25
47
  describe ".#{method_name}" do
26
- it "should initialize a rest crud object" do
27
- RestCrud.should_receive(:new).with("http://www.example.com/resources").and_return rest_crud
28
- klass.send(method_name, send("#{method_name}_params"))
29
- end
30
-
31
- it "should ask rest crud to do the work" do
32
- rest_crud.should_receive(method_name).with(send("#{method_name}_params"))
33
- klass.send(method_name, send("#{method_name}_params"))
34
- end
35
-
36
- it "should intialize a resource object" do
37
- rest_crud.stub(:find).and_return(ActiveSupport::JSON.encode("attr1" => "val1", "attr2" => "val2"))
38
- object = klass.send(method_name, send("#{method_name}_params"))
39
- object.should be_a klass
40
- end
41
-
48
+ let(:method_name) {method_name}
49
+ it_should_behave_like "general behavior"
50
+ it_should_behave_like "singular resource fetching"
51
+ end
52
+ end
53
+ describe ".all" do
54
+ let(:method_name) {"all"}
55
+ it_should_behave_like "general behavior"
56
+ it "should intialize resource objects" do
57
+ rest_crud.stub(:all).and_return([{ "attr1" => "val1" }, { "attr1" => "val2" }].to_json)
58
+ objects = klass.all all_params
59
+ objects.size.should == 2
60
+ objects[0].should be_a klass
61
+ objects[1].should be_a klass
42
62
  end
43
63
  end
44
64
 
@@ -89,5 +109,5 @@ module RestResource
89
109
  object.save
90
110
  end
91
111
  end
112
+ end
92
113
  end
93
- end
@@ -3,6 +3,12 @@ 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
+ let(:http) {double :http_class, :get => true, :post => true}
7
+ let(:response) {double :response, :parsed_response => "response"}
8
+ before(:each) do
9
+ Class.stub(:new).and_return http
10
+ end
11
+
6
12
  describe "#find" do
7
13
  it "should get the resource specified" do
8
14
  RestClient.should_receive(:get).with("http://example.com/resources/123.json")
@@ -10,6 +16,13 @@ module RestResource
10
16
  end
11
17
  end
12
18
 
19
+ describe "#all" do
20
+ it "should get the resources specified" do
21
+ RestClient.should_receive(:get).with("http://example.com/resources.json", {:condition => "a condition"}).and_return response
22
+ subject.all( :condition => "a condition" ).should == response
23
+ end
24
+ end
25
+
13
26
  describe "#create" do
14
27
  it "should post the resource specified" do
15
28
  RestClient.should_receive(:post).with("http://example.com/resources.json", {:param1 => "value1", :param2 => "values2"})
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: 17
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 5
10
- version: 0.1.5
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Yi Wen
@@ -15,11 +15,11 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-12 00:00:00 -05:00
18
+ date: 2011-12-13 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: rest-client
22
+ name: httparty
23
23
  prerelease: false
24
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
25
  none: false
@@ -33,7 +33,7 @@ dependencies:
33
33
  type: :runtime
34
34
  version_requirements: *id001
35
35
  - !ruby/object:Gem::Dependency
36
- name: activesupport
36
+ name: rest-client
37
37
  prerelease: false
38
38
  requirement: &id002 !ruby/object:Gem::Requirement
39
39
  none: false
@@ -46,6 +46,20 @@ dependencies:
46
46
  version: "0"
47
47
  type: :runtime
48
48
  version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: activesupport
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :runtime
62
+ version_requirements: *id003
49
63
  description: A wrapper over rest-client providing basic CRUD restful web service operation
50
64
  email:
51
65
  - hayafirst@gmail.com
@@ -67,6 +81,7 @@ files:
67
81
  - lib/rest_resource.rb
68
82
  - lib/rest_resource/resource.rb
69
83
  - lib/rest_resource/rest_crud.rb
84
+ - lib/rest_resource/unprocessable_entity.rb
70
85
  - lib/rest_resource/value.rb
71
86
  - lib/rest_resource/version.rb
72
87
  - rest_resource.gemspec