rest_resource 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/.rspec +2 -0
- data/.rvmrc +1 -0
- data/.travis.yml +7 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +28 -0
- data/README.md +30 -0
- data/Rakefile +2 -0
- data/lib/rest_resource.rb +6 -0
- data/lib/rest_resource/resource.rb +34 -0
- data/lib/rest_resource/rest_crud.rb +17 -0
- data/lib/rest_resource/version.rb +3 -0
- data/rest_resource.gemspec +22 -0
- data/spec/rest_resource/resource_spec.rb +63 -0
- data/spec/rest_resource/rest_crud_spec.rb +20 -0
- data/spec/spec_helper.rb +17 -0
- data/tasks/rspec.rake +17 -0
- metadata +113 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm ree@rest_resource
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
active_support (3.0.0)
|
5
|
+
activesupport (= 3.0.0)
|
6
|
+
activesupport (3.0.0)
|
7
|
+
diff-lcs (1.1.3)
|
8
|
+
mime-types (1.16)
|
9
|
+
rake (0.9.2)
|
10
|
+
rest-client (1.6.7)
|
11
|
+
mime-types (>= 1.16)
|
12
|
+
rspec (2.6.0)
|
13
|
+
rspec-core (~> 2.6.0)
|
14
|
+
rspec-expectations (~> 2.6.0)
|
15
|
+
rspec-mocks (~> 2.6.0)
|
16
|
+
rspec-core (2.6.4)
|
17
|
+
rspec-expectations (2.6.0)
|
18
|
+
diff-lcs (~> 1.1.2)
|
19
|
+
rspec-mocks (2.6.0)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
active_support
|
26
|
+
rake
|
27
|
+
rest-client
|
28
|
+
rspec
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
rest_resource -- rest-client gem wrapper to provide a simple CRUD interface
|
2
|
+
====================================
|
3
|
+
|
4
|
+
[![Build Status](https://secure.travis-ci.org/ywen/rest_resource.png)](http://travis-ci.org/ywen/rest_resource)
|
5
|
+
|
6
|
+
|
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)
|
9
|
+
|
10
|
+
## USAGE
|
11
|
+
Given you need to fetch user from a web service. You can write:
|
12
|
+
|
13
|
+
class User < RestResource::Resource
|
14
|
+
class << self
|
15
|
+
def url
|
16
|
+
"http://www.example.com/users"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
To use it, you can just do:
|
22
|
+
|
23
|
+
User.find 1
|
24
|
+
|
25
|
+
User.create :name => "Leslie Cheung", :login => "singer"
|
26
|
+
|
27
|
+
Both operation assume your web service controller returns a json string which can be initialized into an object.
|
28
|
+
|
29
|
+
##Contributors
|
30
|
+
* Yi Wen
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module RestResource
|
2
|
+
class Resource
|
3
|
+
attr_reader :attributes
|
4
|
+
private :attributes
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def find(resource_id)
|
8
|
+
self.new(rest_crud.find(resource_id))
|
9
|
+
end
|
10
|
+
|
11
|
+
def create(params)
|
12
|
+
self.new(rest_crud.create(params))
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def rest_crud
|
18
|
+
RestCrud.new(url)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize(params={})
|
23
|
+
params_hash = ActiveSupport::JSON.decode params
|
24
|
+
@attributes = params_hash
|
25
|
+
(class << self; self; end).class_eval do
|
26
|
+
params_hash.each_pair do |method_name, value|
|
27
|
+
define_method(method_name) do
|
28
|
+
value
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module RestResource
|
2
|
+
class RestCrud
|
3
|
+
attr_reader :url
|
4
|
+
private :url
|
5
|
+
def initialize(url)
|
6
|
+
@url = url
|
7
|
+
end
|
8
|
+
|
9
|
+
def find(resource_id)
|
10
|
+
RestClient.get "#{url}/#{resource_id}.json"
|
11
|
+
end
|
12
|
+
|
13
|
+
def create(params)
|
14
|
+
RestClient.post "#{url}.json", params
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "rest_resource/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "rest_resource"
|
7
|
+
s.version = RestResource::VERSION
|
8
|
+
s.authors = ["Yi Wen"]
|
9
|
+
s.email = ["hayafirst@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{A wrapper over rest-client providing basic CRUD restful web service operation}
|
12
|
+
s.description = %q{A wrapper over rest-client providing basic CRUD restful web service operation}
|
13
|
+
|
14
|
+
s.rubyforge_project = "rest_resource"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
s.add_runtime_dependency(%q<rest-client>, [">= 0"])
|
21
|
+
s.add_runtime_dependency(%q<active_support>, [">= 0"])
|
22
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), "..", "spec_helper")
|
2
|
+
module RestResource
|
3
|
+
describe Resource do
|
4
|
+
let(:rest_crud) {double :rest_crud, :find => "{}", :create => "{}"}
|
5
|
+
let(:find_params) {123}
|
6
|
+
let(:create_params) {{"attr1" => "val1"}}
|
7
|
+
let(:klass) {
|
8
|
+
Class.new(Resource) do
|
9
|
+
def self.url
|
10
|
+
"http://www.example.com/resources"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
}
|
14
|
+
|
15
|
+
%w(find create).each do |method_name|
|
16
|
+
describe ".#{method_name}" do
|
17
|
+
before(:each) do
|
18
|
+
RestCrud.stub(:new).and_return rest_crud
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
it "should initialize a rest crud object" do
|
23
|
+
RestCrud.should_receive(:new).with("http://www.example.com/resources").and_return rest_crud
|
24
|
+
klass.send(method_name, send("#{method_name}_params"))
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should ask rest crud to do the finding" do
|
28
|
+
rest_crud.should_receive(method_name).with(send("#{method_name}_params"))
|
29
|
+
klass.send(method_name, send("#{method_name}_params"))
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should intialize a resource object" do
|
33
|
+
rest_crud.stub(:find).and_return(ActiveSupport::JSON.encode("attr1" => "val1", "attr2" => "val2"))
|
34
|
+
object = klass.send(method_name, send("#{method_name}_params"))
|
35
|
+
object.should be_a klass
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe ".new" do
|
42
|
+
let(:params) { ActiveSupport::JSON.encode( {"attr1" => "val1", "attr2" => "val2"} ) }
|
43
|
+
let(:resource) {klass.new(params)}
|
44
|
+
{"attr1" => "val1", "attr2" => "val2"}.each_pair do |attr, val|
|
45
|
+
it "should generate '#{attr}' method and return '#{val}'" do
|
46
|
+
resource.send(attr).should == val
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context "when the objects are initialized multiple times" do
|
51
|
+
attr_reader :resource1
|
52
|
+
before(:each) do
|
53
|
+
@resource1 = klass.new( ActiveSupport::JSON.encode("attr1" => "val1"))
|
54
|
+
klass.new( ActiveSupport::JSON.encode("attr1" => "val2"))
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should work" do
|
58
|
+
resource1.attr1.should == "val1"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), "..", "spec_helper")
|
2
|
+
|
3
|
+
module RestResource
|
4
|
+
describe RestCrud do
|
5
|
+
subject {RestCrud.new("http://example.com/resources")}
|
6
|
+
describe ".find" do
|
7
|
+
it "should get the resource specified" do
|
8
|
+
RestClient.should_receive(:get).with("http://example.com/resources/123.json")
|
9
|
+
subject.find 123
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe ".create" do
|
14
|
+
it "should post the resource specified" do
|
15
|
+
RestClient.should_receive(:post).with("http://example.com/resources.json", {:param1 => "value1", :param2 => "values2"})
|
16
|
+
subject.create({:param1 => "value1", :param2 => "values2"})
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rspec'
|
3
|
+
RSpec.configure do |config|
|
4
|
+
# == Mock Framework
|
5
|
+
#
|
6
|
+
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
|
7
|
+
#
|
8
|
+
# config.mock_with :mocha
|
9
|
+
# config.mock_with :flexmock
|
10
|
+
# config.mock_with :rr
|
11
|
+
config.mock_with :rspec
|
12
|
+
|
13
|
+
# If you're not using ActiveRecord, or you'd prefer not to run each of your
|
14
|
+
# examples within a transaction, comment the following line or assign false
|
15
|
+
# instead of true.
|
16
|
+
end
|
17
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), "../lib", "rest_resource")
|
data/tasks/rspec.rake
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
begin
|
2
|
+
require 'rspec/core'
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
|
5
|
+
spec_prereq = :noop
|
6
|
+
task :noop do
|
7
|
+
end
|
8
|
+
|
9
|
+
desc "Run all specs in spec directory (excluding plugin specs)"
|
10
|
+
RSpec::Core::RakeTask.new(:spec => spec_prereq) do |t|
|
11
|
+
t.rspec_opts = ["--format", "documentation", "--colour"]
|
12
|
+
t.pattern = "spec/**/*_spec.rb"
|
13
|
+
end
|
14
|
+
|
15
|
+
rescue LoadError => e
|
16
|
+
p e
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rest_resource
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Yi Wen
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-10-06 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rest-client
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: active_support
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
description: A wrapper over rest-client providing basic CRUD restful web service operation
|
50
|
+
email:
|
51
|
+
- hayafirst@gmail.com
|
52
|
+
executables: []
|
53
|
+
|
54
|
+
extensions: []
|
55
|
+
|
56
|
+
extra_rdoc_files: []
|
57
|
+
|
58
|
+
files:
|
59
|
+
- .gitignore
|
60
|
+
- .rspec
|
61
|
+
- .rvmrc
|
62
|
+
- .travis.yml
|
63
|
+
- Gemfile
|
64
|
+
- Gemfile.lock
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/rest_resource.rb
|
68
|
+
- lib/rest_resource/resource.rb
|
69
|
+
- lib/rest_resource/rest_crud.rb
|
70
|
+
- lib/rest_resource/version.rb
|
71
|
+
- rest_resource.gemspec
|
72
|
+
- spec/rest_resource/resource_spec.rb
|
73
|
+
- spec/rest_resource/rest_crud_spec.rb
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
- tasks/rspec.rake
|
76
|
+
has_rdoc: true
|
77
|
+
homepage: ""
|
78
|
+
licenses: []
|
79
|
+
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
hash: 3
|
91
|
+
segments:
|
92
|
+
- 0
|
93
|
+
version: "0"
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
hash: 3
|
100
|
+
segments:
|
101
|
+
- 0
|
102
|
+
version: "0"
|
103
|
+
requirements: []
|
104
|
+
|
105
|
+
rubyforge_project: rest_resource
|
106
|
+
rubygems_version: 1.4.2
|
107
|
+
signing_key:
|
108
|
+
specification_version: 3
|
109
|
+
summary: A wrapper over rest-client providing basic CRUD restful web service operation
|
110
|
+
test_files:
|
111
|
+
- spec/rest_resource/resource_spec.rb
|
112
|
+
- spec/rest_resource/rest_crud_spec.rb
|
113
|
+
- spec/spec_helper.rb
|