her 0.2.5 → 0.2.6
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/.rspec +2 -0
- data/README.md +83 -37
- data/Rakefile +0 -1
- data/UPGRADE.md +13 -13
- data/examples/twitter-oauth/app.rb +6 -5
- data/examples/twitter-search/app.rb +10 -8
- data/her.gemspec +13 -13
- data/lib/her.rb +7 -4
- data/lib/her/api.rb +20 -12
- data/lib/her/collection.rb +12 -0
- data/lib/her/middleware.rb +4 -3
- data/lib/her/middleware/accept_json.rb +15 -0
- data/lib/her/model.rb +8 -8
- data/lib/her/model/hooks.rb +24 -0
- data/lib/her/model/http.rb +19 -19
- data/lib/her/model/orm.rb +59 -46
- data/lib/her/model/relationships.rb +40 -27
- data/lib/her/version.rb +1 -1
- data/spec/api_spec.rb +41 -15
- data/spec/middleware/accept_json_spec.rb +10 -0
- data/spec/model/hooks_spec.rb +7 -7
- data/spec/model/http_spec.rb +44 -59
- data/spec/model/introspection_spec.rb +6 -5
- data/spec/model/orm_spec.rb +128 -23
- data/spec/model/paths_spec.rb +8 -10
- data/spec/model/relationships_spec.rb +54 -10
- data/spec/spec_helper.rb +0 -1
- metadata +59 -69
data/lib/her/version.rb
CHANGED
data/spec/api_spec.rb
CHANGED
@@ -5,7 +5,7 @@ describe Her::API do
|
|
5
5
|
context "initialization" do
|
6
6
|
describe ".setup" do
|
7
7
|
it "creates a default connection" do # {{{
|
8
|
-
Her::API.setup :
|
8
|
+
Her::API.setup :url => "https://api.example.com"
|
9
9
|
Her::API.default_api.base_uri.should == "https://api.example.com"
|
10
10
|
end # }}}
|
11
11
|
end
|
@@ -13,7 +13,7 @@ describe Her::API do
|
|
13
13
|
describe "#setup" do
|
14
14
|
it "sets a base URI" do # {{{
|
15
15
|
@api = Her::API.new
|
16
|
-
@api.setup :
|
16
|
+
@api.setup :url => "https://api.example.com"
|
17
17
|
@api.base_uri.should == "https://api.example.com"
|
18
18
|
end # }}}
|
19
19
|
|
@@ -22,18 +22,22 @@ describe Her::API do
|
|
22
22
|
class Bar; end;
|
23
23
|
|
24
24
|
@api = Her::API.new
|
25
|
-
@api.setup :
|
25
|
+
@api.setup :url => "https://api.example.com" do |builder|
|
26
26
|
builder.use Foo
|
27
27
|
builder.use Bar
|
28
28
|
end
|
29
29
|
@api.connection.builder.handlers.should == [Foo, Bar]
|
30
30
|
end # }}}
|
31
|
+
|
32
|
+
it "takes custom options" do # {{{
|
33
|
+
@api = Her::API.new
|
34
|
+
@api.setup :foo => { :bar => "baz" }, :url => "https://api.example.com"
|
35
|
+
@api.options.should == { :foo => { :bar => "baz" }, :url => "https://api.example.com" }
|
36
|
+
end # }}}
|
31
37
|
end
|
32
38
|
|
33
39
|
describe "#request" do
|
34
40
|
it "makes HTTP requests" do # {{{
|
35
|
-
FakeWeb.register_uri(:get, "https://api.example.com/foo", :body => "Foo, it is.")
|
36
|
-
|
37
41
|
class SimpleParser < Faraday::Response::Middleware
|
38
42
|
def on_complete(env)
|
39
43
|
env[:body] = { :data => env[:body] }
|
@@ -41,24 +45,46 @@ describe Her::API do
|
|
41
45
|
end
|
42
46
|
|
43
47
|
@api = Her::API.new
|
44
|
-
@api.setup :
|
48
|
+
@api.setup :url => "https://api.example.com" do |builder|
|
45
49
|
builder.use SimpleParser
|
46
50
|
builder.use Faraday::Request::UrlEncoded
|
47
|
-
builder.
|
51
|
+
builder.adapter :test do |stub|
|
52
|
+
stub.get("/foo") { |env| [200, {}, "Foo it is"] }
|
53
|
+
end
|
48
54
|
end
|
49
55
|
|
50
56
|
parsed_data = @api.request(:_method => :get, :_path => "/foo")
|
51
57
|
parsed_data[:data] == "Foo, it is."
|
52
58
|
end # }}}
|
53
59
|
|
54
|
-
it "
|
55
|
-
|
60
|
+
it "makes HTTP requests while specifying custom HTTP headers" do # {{{
|
61
|
+
class SimpleParser < Faraday::Response::Middleware
|
62
|
+
def on_complete(env)
|
63
|
+
env[:body] = { :data => env[:body] }
|
64
|
+
end
|
65
|
+
end
|
56
66
|
|
57
67
|
@api = Her::API.new
|
58
|
-
@api.setup :
|
68
|
+
@api.setup :url => "https://api.example.com" do |builder|
|
69
|
+
builder.use SimpleParser
|
70
|
+
builder.use Faraday::Request::UrlEncoded
|
71
|
+
builder.adapter :test do |stub|
|
72
|
+
stub.get("/foo") { |env| [200, {}, "Foo it is #{env[:request_headers]["X-Page"]}"] }
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
parsed_data = @api.request(:_method => :get, :_path => "/foo", :_headers => { "X-Page" => 2 })
|
77
|
+
parsed_data[:data] == "Foo, it is page 2."
|
78
|
+
end # }}}
|
79
|
+
|
80
|
+
it "parses a request with the default parser" do # {{{
|
81
|
+
@api = Her::API.new
|
82
|
+
@api.setup :url => "https://api.example.com" do |builder|
|
59
83
|
builder.use Her::Middleware::FirstLevelParseJSON
|
60
84
|
builder.use Faraday::Request::UrlEncoded
|
61
|
-
builder.
|
85
|
+
builder.adapter :test do |stub|
|
86
|
+
stub.get("/users/1") { |env| [200, {}, MultiJson.dump({ :id => 1, :name => "George Michael Bluth", :errors => ["This is a single error"], :metadata => { :page => 1, :per_page => 10 } })] }
|
87
|
+
end
|
62
88
|
end
|
63
89
|
parsed_data = @api.request(:_method => :get, :_path => "users/1")
|
64
90
|
parsed_data[:data].should == { :id => 1, :name => "George Michael Bluth" }
|
@@ -67,8 +93,6 @@ describe Her::API do
|
|
67
93
|
end # }}}
|
68
94
|
|
69
95
|
it "parses a request with a custom parser" do # {{{
|
70
|
-
FakeWeb.register_uri(:get, "https://api.example.com/users/1", :body => MultiJson.dump(:id => 1, :name => "George Michael Bluth"))
|
71
|
-
|
72
96
|
class CustomParser < Faraday::Response::Middleware
|
73
97
|
def on_complete(env)
|
74
98
|
json = MultiJson.load(env[:body], :symbolize_keys => true)
|
@@ -83,10 +107,12 @@ describe Her::API do
|
|
83
107
|
end
|
84
108
|
|
85
109
|
@api = Her::API.new
|
86
|
-
@api.setup :
|
110
|
+
@api.setup :url => "https://api.example.com" do |builder|
|
87
111
|
builder.use CustomParser
|
88
112
|
builder.use Faraday::Request::UrlEncoded
|
89
|
-
builder.
|
113
|
+
builder.adapter :test do |stub|
|
114
|
+
stub.get("/users/1") { |env| [200, {}, MultiJson.dump(:id => 1, :name => "George Michael Bluth")] }
|
115
|
+
end
|
90
116
|
end
|
91
117
|
parsed_data = @api.request(:_method => :get, :_path => "users/1")
|
92
118
|
parsed_data[:data].should == { :id => 1, :name => "George Michael Bluth" }
|
data/spec/model/hooks_spec.rb
CHANGED
@@ -110,17 +110,17 @@ describe Her::Model::Hooks do
|
|
110
110
|
|
111
111
|
context "perform hooks on a model" do
|
112
112
|
before do # {{{
|
113
|
-
Her::API.setup :
|
113
|
+
Her::API.setup :url => "https://api.example.com" do |builder|
|
114
114
|
builder.use Her::Middleware::FirstLevelParseJSON
|
115
115
|
builder.use Faraday::Request::UrlEncoded
|
116
|
-
builder.
|
116
|
+
builder.adapter :test do |stub|
|
117
|
+
stub.post("/users") { |env| [200, {}, { :id => 1, :name => "Tobias Fünke" }.to_json] }
|
118
|
+
stub.get("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Fünke" }.to_json] }
|
119
|
+
stub.put("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Fünke" }.to_json] }
|
120
|
+
stub.delete("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Fünke" }.to_json] }
|
121
|
+
end
|
117
122
|
end
|
118
123
|
|
119
|
-
FakeWeb.register_uri(:post, "https://api.example.com/users", :body => { :id => 1, :name => "Tobias Fünke" }.to_json)
|
120
|
-
FakeWeb.register_uri(:get, "https://api.example.com/users/1", :body => { :id => 1, :name => "Tobias Fünke" }.to_json)
|
121
|
-
FakeWeb.register_uri(:put, "https://api.example.com/users/1", :body => { :id => 1, :name => "Tobias Fünke" }.to_json)
|
122
|
-
FakeWeb.register_uri(:delete, "https://api.example.com/users/1", :body => { :id => 1, :name => "Tobias Fünke" }.to_json)
|
123
|
-
|
124
124
|
spawn_model :User do
|
125
125
|
attr_accessor :internal_save_id, :internal_create_id, :internal_update_id, :internal_destroy_id
|
126
126
|
attr_accessor :internal_after_save_id, :internal_after_create_id, :internal_after_update_id, :internal_after_destroy_id
|
data/spec/model/http_spec.rb
CHANGED
@@ -4,83 +4,67 @@ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
|
|
4
4
|
describe Her::Model::HTTP do
|
5
5
|
context "binding a model with an API" do
|
6
6
|
it "binds a model to an instance of Her::API" do # {{{
|
7
|
-
|
8
|
-
|
7
|
+
api = Her::API.new
|
8
|
+
api.setup :url => "https://api.example.com"
|
9
9
|
|
10
|
-
spawn_model :User
|
11
|
-
|
12
|
-
|
13
|
-
User.class_eval do
|
10
|
+
spawn_model :User do
|
11
|
+
uses_api api
|
14
12
|
@her_api.should_not == nil
|
15
13
|
@her_api.base_uri.should == "https://api.example.com"
|
16
14
|
end
|
17
15
|
end # }}}
|
18
16
|
|
19
17
|
it "binds a model directly to Her::API" do # {{{
|
20
|
-
Her::API.setup :
|
21
|
-
|
22
|
-
spawn_model :User
|
18
|
+
Her::API.setup :url => "https://api.example.com"
|
23
19
|
|
24
|
-
User
|
20
|
+
spawn_model :User do
|
25
21
|
@her_api.should_not == nil
|
26
22
|
@her_api.base_uri.should == "https://api.example.com"
|
27
23
|
end
|
28
24
|
end # }}}
|
29
25
|
|
30
26
|
it "binds two models to two different instances of Her::API" do # {{{
|
31
|
-
|
32
|
-
|
27
|
+
api1 = Her::API.new
|
28
|
+
api1.setup :url => "https://api1.example.com" do |builder|
|
33
29
|
builder.use Her::Middleware::FirstLevelParseJSON
|
34
30
|
builder.use Faraday::Request::UrlEncoded
|
35
|
-
builder.use Faraday::Adapter::NetHttp
|
36
31
|
end
|
37
32
|
|
38
|
-
spawn_model :User
|
39
|
-
|
40
|
-
|
41
|
-
User.class_eval do
|
33
|
+
spawn_model :User do
|
34
|
+
uses_api api1
|
42
35
|
@her_api.base_uri.should == "https://api1.example.com"
|
43
36
|
end
|
44
37
|
|
45
|
-
|
46
|
-
|
38
|
+
api2 = Her::API.new
|
39
|
+
api2.setup :url => "https://api2.example.com" do |builder|
|
47
40
|
builder.use Her::Middleware::FirstLevelParseJSON
|
48
41
|
builder.use Faraday::Request::UrlEncoded
|
49
|
-
builder.use Faraday::Adapter::NetHttp
|
50
42
|
end
|
51
43
|
|
52
|
-
spawn_model :Comment
|
53
|
-
|
54
|
-
|
55
|
-
Comment.class_eval do
|
44
|
+
spawn_model :Comment do
|
45
|
+
uses_api api2
|
56
46
|
@her_api.base_uri.should == "https://api2.example.com"
|
57
47
|
end
|
58
48
|
end # }}}
|
59
49
|
|
60
50
|
it "binds one model to Her::API and another one to an instance of Her::API" do # {{{
|
61
|
-
Her::API.setup :
|
51
|
+
Her::API.setup :url => "https://api1.example.com" do |builder|
|
62
52
|
builder.use Her::Middleware::FirstLevelParseJSON
|
63
53
|
builder.use Faraday::Request::UrlEncoded
|
64
|
-
builder.use Faraday::Adapter::NetHttp
|
65
54
|
end
|
66
55
|
|
67
|
-
spawn_model :User
|
68
|
-
|
69
|
-
User.class_eval do
|
56
|
+
spawn_model :User do
|
70
57
|
@her_api.base_uri.should == "https://api1.example.com"
|
71
58
|
end
|
72
59
|
|
73
|
-
|
74
|
-
|
60
|
+
api = Her::API.new
|
61
|
+
api.setup :url => "https://api2.example.com" do |builder|
|
75
62
|
builder.use Her::Middleware::FirstLevelParseJSON
|
76
63
|
builder.use Faraday::Request::UrlEncoded
|
77
|
-
builder.use Faraday::Adapter::NetHttp
|
78
64
|
end
|
79
65
|
|
80
|
-
spawn_model :Comment
|
81
|
-
|
82
|
-
|
83
|
-
Comment.class_eval do
|
66
|
+
spawn_model :Comment do
|
67
|
+
uses_api api
|
84
68
|
@her_api.base_uri.should == "https://api2.example.com"
|
85
69
|
end
|
86
70
|
end # }}}
|
@@ -88,24 +72,27 @@ describe Her::Model::HTTP do
|
|
88
72
|
|
89
73
|
context "making HTTP requests" do
|
90
74
|
before do # {{{
|
91
|
-
|
92
|
-
@api.setup :base_uri => "https://api.example.com" do |builder|
|
75
|
+
Her::API.setup :url => "https://api.example.com" do |builder|
|
93
76
|
builder.use Her::Middleware::FirstLevelParseJSON
|
94
77
|
builder.use Faraday::Request::UrlEncoded
|
95
|
-
builder.
|
78
|
+
builder.adapter :test do |stub|
|
79
|
+
stub.get("/users") do |env|
|
80
|
+
if env[:params]["page"] == "2"
|
81
|
+
[200, {}, [{ :id => 2 }].to_json]
|
82
|
+
else
|
83
|
+
[200, {}, [{ :id => 1 }].to_json]
|
84
|
+
end
|
85
|
+
end
|
86
|
+
stub.get("/users/popular") { |env| [200, {}, [{ :id => 1 }, { :id => 2 }].to_json] }
|
87
|
+
stub.get("/users/1") { |env| [200, {}, { :id => 1 }.to_json] }
|
88
|
+
stub.post("/users") { |env| [200, {}, [{ :id => 3 }].to_json] }
|
89
|
+
stub.put("/users/4") { |env| [200, {}, [{ :id => 4 }].to_json] }
|
90
|
+
stub.patch("/users/6") { |env| [200, {}, [{ :id => 6 }].to_json] }
|
91
|
+
stub.delete("/users/5") { |env| [200, {}, [{ :id => 5 }].to_json] }
|
92
|
+
end
|
96
93
|
end
|
97
94
|
|
98
|
-
FakeWeb.register_uri(:get, "https://api.example.com/users", :body => [{ :id => 1 }].to_json)
|
99
|
-
FakeWeb.register_uri(:get, "https://api.example.com/users?page=2", :body => [{ :id => 2 }].to_json)
|
100
|
-
FakeWeb.register_uri(:get, "https://api.example.com/users/popular", :body => [{ :id => 1 }, { :id => 2 }].to_json)
|
101
|
-
FakeWeb.register_uri(:get, "https://api.example.com/users/1", :body => { :id => 1 }.to_json)
|
102
|
-
FakeWeb.register_uri(:post, "https://api.example.com/users", :body => [{ :id => 3 }].to_json)
|
103
|
-
FakeWeb.register_uri(:put, "https://api.example.com/users/4", :body => [{ :id => 4 }].to_json)
|
104
|
-
FakeWeb.register_uri(:patch, "https://api.example.com/users/6", :body => [{ :id => 6 }].to_json)
|
105
|
-
FakeWeb.register_uri(:delete, "https://api.example.com/users/5", :body => [{ :id => 5 }].to_json)
|
106
|
-
|
107
95
|
spawn_model :User
|
108
|
-
User.uses_api @api
|
109
96
|
end # }}}
|
110
97
|
|
111
98
|
it "handle GET wrapper method" do # {{{
|
@@ -184,21 +171,19 @@ describe Her::Model::HTTP do
|
|
184
171
|
|
185
172
|
context "setting custom requests" do
|
186
173
|
before do # {{{
|
187
|
-
|
188
|
-
@api.setup :base_uri => "https://api.example.com" do |builder|
|
174
|
+
Her::API.setup :url => "https://api.example.com" do |builder|
|
189
175
|
builder.use Her::Middleware::FirstLevelParseJSON
|
190
176
|
builder.use Faraday::Request::UrlEncoded
|
191
|
-
builder.
|
177
|
+
builder.adapter :test do |stub|
|
178
|
+
stub.get("/users/popular") { |env| [200, {}, [{ :id => 1 }, { :id => 2 }].to_json] }
|
179
|
+
stub.post("/users/from_default") { |env| [200, {}, { :id => 4 }.to_json] }
|
180
|
+
end
|
192
181
|
end
|
193
182
|
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
class User
|
198
|
-
include Her::Model
|
183
|
+
spawn_model :User do
|
184
|
+
custom_get :popular, :foobar
|
185
|
+
custom_post :from_default
|
199
186
|
end
|
200
|
-
User.custom_get :popular, :foobar
|
201
|
-
User.custom_post :from_default
|
202
187
|
end # }}}
|
203
188
|
|
204
189
|
it "handles custom methods" do # {{{
|
@@ -4,13 +4,14 @@ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
|
|
4
4
|
describe Her::Model::Introspection do
|
5
5
|
context "introspecting a resource" do
|
6
6
|
before do # {{{
|
7
|
-
Her::API.setup :
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
Her::API.setup :url => "https://api.example.com" do |builder|
|
8
|
+
builder.use Her::Middleware::FirstLevelParseJSON
|
9
|
+
builder.use Faraday::Request::UrlEncoded
|
10
|
+
builder.adapter :test do |stub|
|
11
|
+
stub.get("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Funke" }.to_json] }
|
11
12
|
end
|
13
|
+
end
|
12
14
|
|
13
|
-
FakeWeb.register_uri(:get, "https://api.example.com/users/1", :body => { :id => 1, :name => "Tobias Funke" }.to_json)
|
14
15
|
spawn_model :User
|
15
16
|
end # }}}
|
16
17
|
|
data/spec/model/orm_spec.rb
CHANGED
@@ -5,16 +5,16 @@ describe Her::Model::ORM do
|
|
5
5
|
context "mapping data to Ruby objects" do
|
6
6
|
before do # {{{
|
7
7
|
api = Her::API.new
|
8
|
-
api.setup :
|
8
|
+
api.setup :url => "https://api.example.com" do |builder|
|
9
9
|
builder.use Her::Middleware::FirstLevelParseJSON
|
10
10
|
builder.use Faraday::Request::UrlEncoded
|
11
|
-
builder.
|
11
|
+
builder.adapter :test do |stub|
|
12
|
+
stub.get("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Fünke" }.to_json] }
|
13
|
+
stub.get("/users") { |env| [200, {}, [{ :id => 1, :name => "Tobias Fünke" }, { :id => 2, :name => "Lindsay Fünke" }].to_json] }
|
14
|
+
stub.get("/admin_users") { |env| [200, {}, [{ :id => 1, :name => "Tobias Fünke" }, { :id => 2, :name => "Lindsay Fünke" }].to_json] }
|
15
|
+
end
|
12
16
|
end
|
13
17
|
|
14
|
-
FakeWeb.register_uri(:get, "https://api.example.com/users/1", :body => { :id => 1, :name => "Tobias Fünke" }.to_json)
|
15
|
-
FakeWeb.register_uri(:get, "https://api.example.com/users", :body => [{ :id => 1, :name => "Tobias Fünke" }, { :id => 2, :name => "Lindsay Fünke" }].to_json)
|
16
|
-
FakeWeb.register_uri(:get, "https://api.example.com/admin_users", :body => [{ :id => 1, :name => "Tobias Fünke" }, { :id => 2, :name => "Lindsay Fünke" }].to_json)
|
17
|
-
|
18
18
|
spawn_model :User do
|
19
19
|
uses_api api
|
20
20
|
end
|
@@ -41,23 +41,130 @@ describe Her::Model::ORM do
|
|
41
41
|
end # }}}
|
42
42
|
|
43
43
|
it "handles new resource" do # {{{
|
44
|
-
@new_user = User.new(:fullname => "Tobias Fünke")
|
44
|
+
@new_user = User.new(:fullname => "Tobias Fünke", :medicine_license => nil)
|
45
45
|
@new_user.new?.should be_true
|
46
|
+
@new_user.fullname.should == "Tobias Fünke"
|
47
|
+
@new_user.medicine_license.should be_nil
|
46
48
|
|
47
49
|
@existing_user = User.find(1)
|
48
50
|
@existing_user.new?.should be_false
|
49
51
|
end # }}}
|
52
|
+
|
53
|
+
it "handles method missing for getter" do# {{{
|
54
|
+
@new_user = User.new(:fullname => 'Mayonegg')
|
55
|
+
lambda { @new_user.unknown_method_for_a_user }.should raise_error(NoMethodError)
|
56
|
+
expect { @new_user.fullname }.to_not raise_error(NoMethodError)
|
57
|
+
end# }}}
|
58
|
+
|
59
|
+
it "handles method missing for setter" do# {{{
|
60
|
+
@new_user = User.new
|
61
|
+
expect { @new_user.fullname = "Tobias Fünke" }.to_not raise_error(NoMethodError)
|
62
|
+
end# }}}
|
50
63
|
end
|
51
64
|
|
52
|
-
context "
|
65
|
+
context "mapping data, metadata and error data to Ruby objects" do
|
66
|
+
before do # {{{
|
67
|
+
api = Her::API.new
|
68
|
+
api.setup :url => "https://api.example.com" do |builder|
|
69
|
+
builder.use Her::Middleware::SecondLevelParseJSON
|
70
|
+
builder.use Faraday::Request::UrlEncoded
|
71
|
+
builder.adapter :test do |stub|
|
72
|
+
stub.get("/users") { |env| [200, {}, { :data => [{ :id => 1, :name => "Tobias Fünke" }, { :id => 2, :name => "Lindsay Fünke" }], :metadata => { :total_pages => 10, :next_page => 2 }, :errors => ["Oh", "My", "God"] }.to_json] }
|
73
|
+
stub.post("/users") { |env| [200, {}, { :data => { :name => "George Michael Bluth" }, :metadata => { :foo => "bar" }, :errors => ["Yes", "Sir"] }.to_json] }
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
spawn_model :User do
|
78
|
+
uses_api api
|
79
|
+
end
|
80
|
+
end # }}}
|
81
|
+
|
82
|
+
it "handles metadata on a collection" do # {{{
|
83
|
+
@users = User.all
|
84
|
+
@users.metadata[:total_pages].should == 10
|
85
|
+
end # }}}
|
86
|
+
|
87
|
+
it "handles error data on a collection" do # {{{
|
88
|
+
@users = User.all
|
89
|
+
@users.errors.length.should == 3
|
90
|
+
end # }}}
|
91
|
+
|
92
|
+
it "handles metadata on a resource" do # {{{
|
93
|
+
@user = User.create(:name => "George Michael Bluth")
|
94
|
+
@user.metadata[:foo].should == "bar"
|
95
|
+
end # }}}
|
96
|
+
|
97
|
+
it "handles error data on a resource" do # {{{
|
98
|
+
@user = User.create(:name => "George Michael Bluth")
|
99
|
+
@user.errors.should == ["Yes", "Sir"]
|
100
|
+
@user.should be_invalid
|
101
|
+
end # }}}
|
102
|
+
end
|
103
|
+
|
104
|
+
context "defining custom getters and setters" do
|
53
105
|
before do # {{{
|
54
|
-
Her::API.
|
106
|
+
api = Her::API.new
|
107
|
+
api.setup :url => "https://api.example.com" do |builder|
|
55
108
|
builder.use Her::Middleware::FirstLevelParseJSON
|
56
109
|
builder.use Faraday::Request::UrlEncoded
|
57
|
-
builder.
|
110
|
+
builder.adapter :test do |stub|
|
111
|
+
stub.get("/users/1") { |env| [200, {}, { :id => 1, :friends => ["Maeby", "GOB", "Anne"] }.to_json] }
|
112
|
+
stub.get("/users/2") { |env| [200, {}, { :id => 1, :organization => true }.to_json] }
|
113
|
+
end
|
58
114
|
end
|
59
115
|
|
60
|
-
|
116
|
+
spawn_model :User do
|
117
|
+
uses_api api
|
118
|
+
belongs_to :organization
|
119
|
+
|
120
|
+
def friends=(val)
|
121
|
+
val = val.gsub("\r", "").split("\n").map { |friend| friend.gsub(/^\s*\*\s*/, "") } if val and val.is_a?(String)
|
122
|
+
@data[:friends] = val
|
123
|
+
end
|
124
|
+
|
125
|
+
def friends
|
126
|
+
@data[:friends].map { |friend| "* #{friend}" }.join("\n")
|
127
|
+
end
|
128
|
+
|
129
|
+
# Why would anybody want to do this? I don’t know.
|
130
|
+
def organization=(organization)
|
131
|
+
@data[:organization] = { :foo => :bar }
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end # }}}
|
135
|
+
|
136
|
+
it "handles custom setters" do # {{{
|
137
|
+
@user = User.find(1)
|
138
|
+
@user.friends.should == "* Maeby\n* GOB\n* Anne"
|
139
|
+
@user.instance_eval do
|
140
|
+
@data[:friends] = ["Maeby", "GOB", "Anne"]
|
141
|
+
end
|
142
|
+
end # }}}
|
143
|
+
|
144
|
+
it "handles custom setters with relationships" do # {{{
|
145
|
+
@user = User.find(2)
|
146
|
+
@user.organization.should == { :foo => :bar }
|
147
|
+
end # }}}
|
148
|
+
|
149
|
+
it "handles custom getters" do # {{{
|
150
|
+
@user = User.new
|
151
|
+
@user.friends = "* George\n* Oscar\n* Lucille"
|
152
|
+
@user.friends.should == "* George\n* Oscar\n* Lucille"
|
153
|
+
@user.instance_eval do
|
154
|
+
@data[:friends] = ["George", "Oscar", "Lucille"]
|
155
|
+
end
|
156
|
+
end # }}}
|
157
|
+
end
|
158
|
+
|
159
|
+
context "creating resources" do
|
160
|
+
before do # {{{
|
161
|
+
Her::API.setup :url => "https://api.example.com" do |builder|
|
162
|
+
builder.use Her::Middleware::FirstLevelParseJSON
|
163
|
+
builder.use Faraday::Request::UrlEncoded
|
164
|
+
builder.adapter :test do |stub|
|
165
|
+
stub.post("/users") { |env| [200, {}, { :id => 1, :fullname => "Tobias Fünke" }.to_json] }
|
166
|
+
end
|
167
|
+
end
|
61
168
|
|
62
169
|
spawn_model :User
|
63
170
|
end # }}}
|
@@ -77,16 +184,15 @@ describe Her::Model::ORM do
|
|
77
184
|
|
78
185
|
context "updating resources" do
|
79
186
|
before do # {{{
|
80
|
-
|
81
|
-
@api.setup :base_uri => "https://api.example.com" do |builder|
|
187
|
+
Her::API.setup :url => "https://api.example.com" do |builder|
|
82
188
|
builder.use Her::Middleware::FirstLevelParseJSON
|
83
189
|
builder.use Faraday::Request::UrlEncoded
|
84
|
-
builder.
|
190
|
+
builder.adapter :test do |stub|
|
191
|
+
stub.get("/users/1") { |env| [200, {}, { :id => 1, :fullname => "Tobias Fünke" }.to_json] }
|
192
|
+
stub.put("/users/1") { |env| [200, {}, { :id => 1, :fullname => "Lindsay Fünke" }.to_json] }
|
193
|
+
end
|
85
194
|
end
|
86
195
|
|
87
|
-
FakeWeb.register_uri(:get, "https://api.example.com/users/1", :body => { :id => 1, :fullname => "Tobias Fünke" }.to_json)
|
88
|
-
FakeWeb.register_uri(:put, "https://api.example.com/users/1", :body => { :id => 1, :fullname => "Lindsay Fünke" }.to_json)
|
89
|
-
|
90
196
|
spawn_model :User
|
91
197
|
end # }}}
|
92
198
|
|
@@ -112,16 +218,15 @@ describe Her::Model::ORM do
|
|
112
218
|
|
113
219
|
context "deleting resources" do
|
114
220
|
before do # {{{
|
115
|
-
|
116
|
-
@api.setup :base_uri => "https://api.example.com" do |builder|
|
221
|
+
Her::API.setup :url => "https://api.example.com" do |builder|
|
117
222
|
builder.use Her::Middleware::FirstLevelParseJSON
|
118
223
|
builder.use Faraday::Request::UrlEncoded
|
119
|
-
builder.
|
224
|
+
builder.adapter :test do |stub|
|
225
|
+
stub.get("/users/1") { |env| [200, {}, { :id => 1, :fullname => "Tobias Fünke", :active => true }.to_json] }
|
226
|
+
stub.delete("/users/1") { |env| [200, {}, { :id => 1, :fullname => "Lindsay Fünke", :active => false }.to_json] }
|
227
|
+
end
|
120
228
|
end
|
121
229
|
|
122
|
-
FakeWeb.register_uri(:get, "https://api.example.com/users/1", :body => { :id => 1, :fullname => "Tobias Fünke", :active => true }.to_json)
|
123
|
-
FakeWeb.register_uri(:delete, "https://api.example.com/users/1", :body => { :id => 1, :fullname => "Lindsay Fünke", :active => false }.to_json)
|
124
|
-
|
125
230
|
spawn_model :User
|
126
231
|
end # }}}
|
127
232
|
|