restparty 0.0.3 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +43 -1
- data/VERSION +1 -1
- data/lib/restparty.rb +40 -33
- data/restparty.gemspec +1 -1
- metadata +3 -3
data/README.rdoc
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
= restparty
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
RestParty is a simple wrapper around HTTParty allowing you to handle simple operation with a restful webservice.
|
|
4
|
+
|
|
5
|
+
class User < RestParty
|
|
6
|
+
resource_for :users
|
|
7
|
+
format :json
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
This simple piece of code will generate basics CRUD operations and allow you to :
|
|
11
|
+
|
|
12
|
+
User.find(1)
|
|
13
|
+
User.find(:all)
|
|
14
|
+
User.create({:name => "Mike", :email => "mike@mike.com"})
|
|
15
|
+
User.delete(1)
|
|
16
|
+
|
|
17
|
+
All of this methods can take a 2nd optional argument :
|
|
18
|
+
|
|
19
|
+
User.find(:all, {:q => "Mike"})
|
|
20
|
+
|
|
21
|
+
The resource_for can also take few arguments :
|
|
22
|
+
|
|
23
|
+
* only, except
|
|
24
|
+
|
|
25
|
+
resource_for :user_sessions, :only => [:create, :delete]
|
|
26
|
+
|
|
27
|
+
* member, collection
|
|
28
|
+
|
|
29
|
+
resource_for :auditions, :except => [:delete, :update], :member => {:rate => :post, :comment => :post}, :collection => {:search => :get}
|
|
30
|
+
|
|
31
|
+
This will allow you to add basic operations as :
|
|
32
|
+
|
|
33
|
+
Audition.rate(1, options)
|
|
34
|
+
Audition.comment(2, {:comment => "Now we're talking !"})
|
|
35
|
+
Audition.search(:q => "Mike")
|
|
36
|
+
|
|
37
|
+
Of course You can use your RestParty class like a basic HTTparty class everything works as expected :
|
|
38
|
+
|
|
39
|
+
class User < RestParty
|
|
40
|
+
resource_for :users
|
|
41
|
+
format :json
|
|
42
|
+
base_uri 'localhost:3000'
|
|
43
|
+
auth_digest "login", "pass"
|
|
44
|
+
end
|
|
45
|
+
|
|
4
46
|
|
|
5
47
|
== Contributing to restparty
|
|
6
48
|
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.0
|
|
1
|
+
0.1.0
|
data/lib/restparty.rb
CHANGED
|
@@ -1,91 +1,98 @@
|
|
|
1
|
+
require "httparty"
|
|
2
|
+
|
|
1
3
|
class RestParty
|
|
2
4
|
include HTTParty
|
|
3
5
|
|
|
4
6
|
attr_reader :resource
|
|
7
|
+
attr_reader :resource_methods
|
|
8
|
+
|
|
9
|
+
class << self
|
|
5
10
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
11
|
+
def resource_for(resource, options = {:methods => [:create, :index, :show, :update, :delete], :member => {}, :collection => {}})
|
|
12
|
+
@resource = resource.to_s
|
|
13
|
+
@resource_methods = options[:methods]
|
|
14
|
+
|
|
15
|
+
if !options[:only].blank?
|
|
16
|
+
@resource_methods = options[:only]
|
|
17
|
+
elsif !options[:except].blank?
|
|
18
|
+
@resource_methods = [:create, :index, :show, :update, :delete]
|
|
19
|
+
@resource_methods.delete_if{|m| options[:except].include?(m)}
|
|
20
|
+
end
|
|
9
21
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
resource_methods.
|
|
22
|
+
build_members(options[:member]) if options[:member]
|
|
23
|
+
build_collections(options[:collection]) if options[:collection]
|
|
24
|
+
build_create if @resource_methods.include?(:create)
|
|
25
|
+
build_get if @resource_methods.include?(:index) || @resource_methods.include?(:show)
|
|
26
|
+
build_update if @resource_methods.include?(:update)
|
|
27
|
+
build_delete if @resource_methods.include?(:delete)
|
|
15
28
|
end
|
|
16
29
|
|
|
17
|
-
|
|
18
|
-
build_collections(options[:collection]) if options[:collection]
|
|
19
|
-
build_create if resource_methods.include?(:create)
|
|
20
|
-
build_get if resource_methods.include?(:index) || resource_methods.include?(:show)
|
|
21
|
-
build_update if resource_methods.include?(:update)
|
|
22
|
-
build_delete if resource_methods.include?(:delete)
|
|
23
|
-
end
|
|
30
|
+
private
|
|
24
31
|
|
|
25
|
-
private
|
|
26
|
-
|
|
27
32
|
def build_delete
|
|
28
33
|
class_eval %{
|
|
29
34
|
def self.delete(id, options = {})
|
|
30
|
-
delete('/#{resource}/'+id.to_s, :query => options)
|
|
35
|
+
delete('/#{@resource}/'+id.to_s, :query => options)
|
|
31
36
|
end
|
|
32
37
|
}
|
|
33
38
|
end
|
|
34
|
-
|
|
39
|
+
|
|
35
40
|
def build_update
|
|
36
41
|
class_eval %{
|
|
37
42
|
def self.update(id, options = {})
|
|
38
|
-
put('/#{resource}/'+id.to_s, :query => options, :headers => {'Content-Length' => '0'})
|
|
43
|
+
put('/#{@resource}/'+id.to_s, :query => options, :headers => {'Content-Length' => '0'})
|
|
39
44
|
end
|
|
40
45
|
}
|
|
41
46
|
end
|
|
42
|
-
|
|
47
|
+
|
|
43
48
|
def build_get
|
|
44
49
|
class_eval %{
|
|
45
50
|
def self.find(id, options = {})
|
|
46
51
|
response = ""
|
|
47
|
-
if id.to_s == "all" and #{resource_methods.include?(:index)}
|
|
48
|
-
response = get('/#{resource}', :query => options)
|
|
49
|
-
elsif id.to_s =~ /^[-+]?[0-9]+$/ and #{resource_methods.include?(:show)}
|
|
50
|
-
response = get('/#{resource}/'+id.to_s, :query => options)
|
|
52
|
+
if id.to_s == "all" and #{@resource_methods.include?(:index)}
|
|
53
|
+
response = get('/#{@resource}', :query => options)
|
|
54
|
+
elsif id.to_s =~ /^[-+]?[0-9]+$/ and #{@resource_methods.include?(:show)}
|
|
55
|
+
response = get('/#{@resource}/'+id.to_s, :query => options)
|
|
51
56
|
else
|
|
52
57
|
raise "error"
|
|
53
58
|
end
|
|
54
59
|
end
|
|
55
60
|
}
|
|
56
61
|
end
|
|
57
|
-
|
|
62
|
+
|
|
58
63
|
def build_create
|
|
59
64
|
class_eval %{
|
|
60
65
|
def self.create(options = {})
|
|
61
|
-
post('/#{resource}', :query => options)
|
|
66
|
+
post('/#{@resource}', :query => options)
|
|
62
67
|
end
|
|
63
68
|
}
|
|
64
69
|
end
|
|
65
|
-
|
|
70
|
+
|
|
66
71
|
def build_collections(collections)
|
|
67
72
|
collections.each_pair do |collection, method|
|
|
68
73
|
raise 'error_http_method' unless valid_http_method?(method)
|
|
69
74
|
class_eval %{
|
|
70
75
|
def self.#{collection}(options = {})
|
|
71
|
-
|
|
76
|
+
#{method}('/#{@resource}/#{collection}', :query => options)
|
|
72
77
|
end
|
|
73
78
|
}
|
|
74
79
|
end
|
|
75
80
|
end
|
|
76
|
-
|
|
81
|
+
|
|
77
82
|
def build_members(members)
|
|
78
83
|
members.each_pair do |member, method|
|
|
79
84
|
raise 'error_method' unless valid_http_method?(method)
|
|
80
85
|
class_eval %{
|
|
81
86
|
def self.#{member}(id, options = {})
|
|
82
|
-
|
|
87
|
+
#{method}('/#{@resource}/'+id.to_s+'/#{member}', :query => options)
|
|
83
88
|
end
|
|
84
89
|
}
|
|
85
90
|
end
|
|
86
91
|
end
|
|
87
|
-
|
|
88
|
-
def
|
|
92
|
+
|
|
93
|
+
def valid_http_method?(method)
|
|
89
94
|
['post', 'get', 'put', 'delete', 'head'].include? method.to_s
|
|
90
95
|
end
|
|
96
|
+
|
|
97
|
+
end
|
|
91
98
|
end
|
data/restparty.gemspec
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
Gem::Specification.new do |s|
|
|
7
7
|
s.name = %q{restparty}
|
|
8
|
-
s.version = "0.0
|
|
8
|
+
s.version = "0.1.0"
|
|
9
9
|
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
11
11
|
s.authors = ["Michael Bensoussan", "Pierre Boutbel"]
|
metadata
CHANGED
|
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
|
4
4
|
prerelease: false
|
|
5
5
|
segments:
|
|
6
6
|
- 0
|
|
7
|
+
- 1
|
|
7
8
|
- 0
|
|
8
|
-
|
|
9
|
-
version: 0.0.3
|
|
9
|
+
version: 0.1.0
|
|
10
10
|
platform: ruby
|
|
11
11
|
authors:
|
|
12
12
|
- Michael Bensoussan
|
|
@@ -111,7 +111,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
111
111
|
requirements:
|
|
112
112
|
- - ">="
|
|
113
113
|
- !ruby/object:Gem::Version
|
|
114
|
-
hash:
|
|
114
|
+
hash: 692511520615816357
|
|
115
115
|
segments:
|
|
116
116
|
- 0
|
|
117
117
|
version: "0"
|