placid 0.0.3 → 0.0.4
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/History.md +7 -0
- data/README.md +54 -2
- data/lib/placid/helper.rb +4 -65
- data/lib/placid/model.rb +4 -4
- data/placid.gemspec +1 -1
- data/spec/placid_helper_spec.rb +12 -32
- data/spec/placid_model_spec.rb +5 -5
- metadata +4 -4
data/History.md
CHANGED
data/README.md
CHANGED
@@ -5,6 +5,9 @@ Placid is an ActiveRecord-ish model using a REST API for storage. The REST API
|
|
5
5
|
can be any backend you choose or create yourself, provided it follows some basic
|
6
6
|
conventions.
|
7
7
|
|
8
|
+
[Documentation is on rdoc.info.](http://rdoc.info/github/a-e/placid/master/frames)
|
9
|
+
|
10
|
+
|
8
11
|
Installation
|
9
12
|
------------
|
10
13
|
|
@@ -48,7 +51,7 @@ define on your model. For example:
|
|
48
51
|
unique_id :email
|
49
52
|
|
50
53
|
def add_phone(phone_number)
|
51
|
-
put
|
54
|
+
request(:put, model, id, 'add_phone', phone_number)
|
52
55
|
end
|
53
56
|
end
|
54
57
|
|
@@ -56,7 +59,56 @@ define on your model. For example:
|
|
56
59
|
|
57
60
|
jenny.add_phone('867-5309')
|
58
61
|
# Same as:
|
59
|
-
jenny.put
|
62
|
+
jenny.request(:put, 'person', 'jenny@example.com', 'add_phone', '867-5309')
|
63
|
+
|
64
|
+
|
65
|
+
Model names
|
66
|
+
-----------
|
67
|
+
|
68
|
+
By default, Placid assumes that your REST pathnames use the `snake_case`
|
69
|
+
version of your model's name. That is, if you have these models:
|
70
|
+
|
71
|
+
class Person < Placid::Model
|
72
|
+
end
|
73
|
+
|
74
|
+
class HomeAddress < Placid::Model
|
75
|
+
end
|
76
|
+
|
77
|
+
then Placid will use these REST paths:
|
78
|
+
|
79
|
+
/person
|
80
|
+
/home_address
|
81
|
+
|
82
|
+
To override this behavior for a single model, simply define the `model` class
|
83
|
+
method. For instance, if the REST path for `HomeAddress` should be `addr`, do:
|
84
|
+
|
85
|
+
class HomeAddress < Placid::Model
|
86
|
+
def self.model
|
87
|
+
'addr'
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
If you want to override this behavior for all models in your app, create a
|
92
|
+
shared base class derived from `Placid::Model`, and override the `model` class
|
93
|
+
method there. For example, if your REST paths use the exact `CamelCase` model
|
94
|
+
name, you could do:
|
95
|
+
|
96
|
+
class Model < Placid::Model
|
97
|
+
def self.model
|
98
|
+
self.name
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
class Person < Model
|
103
|
+
end
|
104
|
+
|
105
|
+
class HomeAddress < Model
|
106
|
+
end
|
107
|
+
|
108
|
+
This configuration will use REST paths like:
|
109
|
+
|
110
|
+
/Person
|
111
|
+
/HomeAddress
|
60
112
|
|
61
113
|
|
62
114
|
License
|
data/lib/placid/helper.rb
CHANGED
@@ -43,7 +43,8 @@ module Placid
|
|
43
43
|
#
|
44
44
|
# @overload request(method, *path, params={})
|
45
45
|
# @param [String, Symbol] method
|
46
|
-
# Request method to use ('get', 'post', 'put', 'delete'
|
46
|
+
# Request method to use, as a string ('get', 'post', 'put', 'delete')
|
47
|
+
# or symbol (:get, :post, :put, :delete)
|
47
48
|
# @param [Array] path
|
48
49
|
# Path components for the request
|
49
50
|
# @param [Hash] params
|
@@ -70,68 +71,6 @@ module Placid
|
|
70
71
|
return JSON.parse(response) rescue {}
|
71
72
|
end
|
72
73
|
|
73
|
-
# Send a GET request and return the parsed JSON response.
|
74
|
-
#
|
75
|
-
# @example
|
76
|
-
# get('people', 'eric')
|
77
|
-
# get('people', {:name => 'eric'})
|
78
|
-
#
|
79
|
-
# @overload get(*path, params={})
|
80
|
-
# See {#request} for allowed parameters.
|
81
|
-
#
|
82
|
-
# @return [Hash]
|
83
|
-
# Parsed response, or an empty hash if parsing failed
|
84
|
-
#
|
85
|
-
def get(*path)
|
86
|
-
request('get', *path)
|
87
|
-
end
|
88
|
-
|
89
|
-
# Send a POST request and return the parsed JSON response.
|
90
|
-
#
|
91
|
-
# @example
|
92
|
-
# post('people', 'new', {:name => 'eric'})
|
93
|
-
#
|
94
|
-
# @overload post(*path, params={})
|
95
|
-
# See {#request} for allowed parameters.
|
96
|
-
#
|
97
|
-
# @return [Hash]
|
98
|
-
# Parsed response, or an empty hash if parsing failed
|
99
|
-
#
|
100
|
-
def post(*path)
|
101
|
-
request('post', *path)
|
102
|
-
end
|
103
|
-
|
104
|
-
# Send a PUT request and return the parsed JSON response.
|
105
|
-
#
|
106
|
-
# @example
|
107
|
-
# put('people', 'eric', {:title => 'Developer'})
|
108
|
-
#
|
109
|
-
# @overload put(*path, params={})
|
110
|
-
# See {#request} for allowed parameters.
|
111
|
-
#
|
112
|
-
# @return [Hash]
|
113
|
-
# Parsed response, or an empty hash if parsing failed
|
114
|
-
#
|
115
|
-
def put(*path)
|
116
|
-
request('put', *path)
|
117
|
-
end
|
118
|
-
|
119
|
-
# Send a DELETE request and return the parsed JSON response.
|
120
|
-
#
|
121
|
-
# @example
|
122
|
-
# delete('people', 'eric')
|
123
|
-
# delete('people', {:name => 'eric'})
|
124
|
-
#
|
125
|
-
# @overload delete(*path, params={})
|
126
|
-
# See {#request} for allowed parameters.
|
127
|
-
#
|
128
|
-
# @return [Hash]
|
129
|
-
# Parsed response, or an empty hash if parsing failed
|
130
|
-
#
|
131
|
-
def delete(*path)
|
132
|
-
request('delete', *path)
|
133
|
-
end
|
134
|
-
|
135
74
|
# Send a GET to a path that returns a single JSON object, and return the
|
136
75
|
# result as a Hashie::Mash.
|
137
76
|
#
|
@@ -141,7 +80,7 @@ module Placid
|
|
141
80
|
# @return [Hashie::Mash]
|
142
81
|
#
|
143
82
|
def get_mash(*path)
|
144
|
-
json = get
|
83
|
+
json = request(:get, *path)
|
145
84
|
begin
|
146
85
|
return Hashie::Mash.new(json)
|
147
86
|
rescue => e
|
@@ -159,7 +98,7 @@ module Placid
|
|
159
98
|
# @return [Array]
|
160
99
|
#
|
161
100
|
def get_mashes(*path)
|
162
|
-
json = get
|
101
|
+
json = request(:get, *path)
|
163
102
|
begin
|
164
103
|
return json.map {|rec| Hashie::Mash.new(rec)}
|
165
104
|
rescue => e
|
data/lib/placid/model.rb
CHANGED
@@ -112,7 +112,7 @@ module Placid
|
|
112
112
|
# @return [Model]
|
113
113
|
#
|
114
114
|
def self.find(id)
|
115
|
-
json = get
|
115
|
+
json = request(:get, model, id)
|
116
116
|
return self.new(json)
|
117
117
|
end
|
118
118
|
|
@@ -125,7 +125,7 @@ module Placid
|
|
125
125
|
#
|
126
126
|
def self.create(attrs={})
|
127
127
|
obj = self.new(attrs)
|
128
|
-
json = post
|
128
|
+
json = request(:post, model, attrs)
|
129
129
|
obj.merge!(json)
|
130
130
|
return obj
|
131
131
|
end
|
@@ -141,7 +141,7 @@ module Placid
|
|
141
141
|
#
|
142
142
|
def self.update(id, attrs={})
|
143
143
|
obj = self.new(attrs)
|
144
|
-
json = put
|
144
|
+
json = request(:put, model, id, attrs)
|
145
145
|
obj.merge!(json)
|
146
146
|
return obj
|
147
147
|
end
|
@@ -152,7 +152,7 @@ module Placid
|
|
152
152
|
# Identifier for the model instance to delete
|
153
153
|
#
|
154
154
|
def self.destroy(id)
|
155
|
-
delete
|
155
|
+
request(:delete, model, id)
|
156
156
|
end
|
157
157
|
|
158
158
|
end
|
data/placid.gemspec
CHANGED
data/spec/placid_helper_spec.rb
CHANGED
@@ -18,6 +18,18 @@ describe Placid::Helper do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
describe "#request" do
|
21
|
+
it "accepts a string for method" do
|
22
|
+
RestClient.stub(:get => '["success"]')
|
23
|
+
json = request('get')
|
24
|
+
json.should == ["success"]
|
25
|
+
end
|
26
|
+
|
27
|
+
it "accepts a symbol for method" do
|
28
|
+
RestClient.stub(:get => '["success"]')
|
29
|
+
json = request(:get)
|
30
|
+
json.should == ["success"]
|
31
|
+
end
|
32
|
+
|
21
33
|
it "returns a legitimate response as JSON" do
|
22
34
|
RestClient.stub(:get => '["success"]')
|
23
35
|
json = request('get')
|
@@ -69,38 +81,6 @@ describe Placid::Helper do
|
|
69
81
|
end
|
70
82
|
end
|
71
83
|
|
72
|
-
describe "#get" do
|
73
|
-
it "sends a GET request to the given path" do
|
74
|
-
RestClient.should_receive(:get).
|
75
|
-
with('http://localhost/foo/bar', {:params => {:baz => 'hi'}})
|
76
|
-
get('foo', 'bar', :baz => 'hi')
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
describe "#post" do
|
81
|
-
it "sends a POST request to the given path" do
|
82
|
-
RestClient.should_receive(:post).
|
83
|
-
with('http://localhost/foo/bar', {:baz => 'hi'})
|
84
|
-
post('foo', 'bar', :baz => 'hi')
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
describe "#put" do
|
89
|
-
it "sends a PUT request to the given path" do
|
90
|
-
RestClient.should_receive(:put).
|
91
|
-
with('http://localhost/foo/bar', {:baz => 'hi'})
|
92
|
-
put('foo', 'bar', :baz => 'hi')
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
describe "#delete" do
|
97
|
-
it "sends a DELETE request to the given path" do
|
98
|
-
RestClient.should_receive(:delete).
|
99
|
-
with('http://localhost/foo/bar', {:baz => 'hi'})
|
100
|
-
delete('foo', 'bar', :baz => 'hi')
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
84
|
describe "#get_mash" do
|
105
85
|
it "returns a Hashie::Mash for hash data" do
|
106
86
|
data = {
|
data/spec/placid_model_spec.rb
CHANGED
@@ -68,21 +68,21 @@ describe Placid::Model do
|
|
68
68
|
it "returns false if errors were reported" do
|
69
69
|
thing = Thing.new
|
70
70
|
Thing.stub(:find => nil)
|
71
|
-
Thing.stub(:post
|
71
|
+
Thing.stub(:request).with(:post, 'thing', {}) { {'errors' => 'Missing id'} }
|
72
72
|
thing.save.should be_false
|
73
73
|
end
|
74
74
|
|
75
75
|
it "returns true if errors is an empty list" do
|
76
76
|
thing = Thing.new(:id => '123')
|
77
77
|
Thing.stub(:find => nil)
|
78
|
-
Thing.stub(:post => {'errors' => []}
|
78
|
+
Thing.stub(:request).with(:post, 'thing', {'id' => '123'}) { {'errors' => []} }
|
79
79
|
thing.save.should be_true
|
80
80
|
end
|
81
81
|
|
82
82
|
it "returns true if no errors were reported" do
|
83
83
|
thing = Thing.new(:id => '123')
|
84
84
|
Thing.stub(:find => nil)
|
85
|
-
Thing.stub(:post => {}
|
85
|
+
Thing.stub(:request).with(:post, 'thing', {'id' => '123'}) { {} }
|
86
86
|
thing.save.should be_true
|
87
87
|
end
|
88
88
|
end
|
@@ -165,11 +165,11 @@ describe Placid::Model do
|
|
165
165
|
end
|
166
166
|
|
167
167
|
describe "helpers" do
|
168
|
-
it "can call #
|
168
|
+
it "can call #request on an instance" do
|
169
169
|
thing = Thing.new
|
170
170
|
RestClient.should_receive(:get).
|
171
171
|
with('http://localhost/thing/foo', {:params => {:x => 'y'}})
|
172
|
-
thing.get
|
172
|
+
thing.request(:get, 'thing', 'foo', :x => 'y')
|
173
173
|
end
|
174
174
|
end
|
175
175
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: placid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Eric Pierce
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-05-
|
18
|
+
date: 2012-05-31 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: hashie
|