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.
@@ -1,3 +1,3 @@
1
1
  module Her
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.6"
3
3
  end
@@ -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 :base_uri => "https://api.example.com"
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 :base_uri => "https://api.example.com"
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 :base_uri => "https://api.example.com" do |builder|
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 :base_uri => "https://api.example.com" do |builder|
48
+ @api.setup :url => "https://api.example.com" do |builder|
45
49
  builder.use SimpleParser
46
50
  builder.use Faraday::Request::UrlEncoded
47
- builder.use Faraday::Adapter::NetHttp
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 "parses a request with the default parser" do # {{{
55
- FakeWeb.register_uri(:get, "https://api.example.com/users/1", :body => MultiJson.dump({ :id => 1, :name => "George Michael Bluth", :errors => ["This is a single error"], :metadata => { :page => 1, :per_page => 10 } }))
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 :base_uri => "https://api.example.com" do |builder|
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.use Faraday::Adapter::NetHttp
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 :base_uri => "https://api.example.com" do |builder|
110
+ @api.setup :url => "https://api.example.com" do |builder|
87
111
  builder.use CustomParser
88
112
  builder.use Faraday::Request::UrlEncoded
89
- builder.use Faraday::Adapter::NetHttp
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" }
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe Her::Middleware::AcceptJSON do
5
+ it "adds an Accept header" do
6
+ described_class.new.add_header({}).tap do |headers|
7
+ headers["Accept"].should == "application/json"
8
+ end
9
+ end
10
+ end
@@ -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 :base_uri => "https://api.example.com" do |builder|
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.use Faraday::Adapter::NetHttp
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
@@ -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
- @api = Her::API.new
8
- @api.setup :base_uri => "https://api.example.com"
7
+ api = Her::API.new
8
+ api.setup :url => "https://api.example.com"
9
9
 
10
- spawn_model :User
11
- User.uses_api @api
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 :base_uri => "https://api.example.com"
21
-
22
- spawn_model :User
18
+ Her::API.setup :url => "https://api.example.com"
23
19
 
24
- User.class_eval do
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
- @api1 = Her::API.new
32
- @api1.setup :base_uri => "https://api1.example.com" do |builder|
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
- User.uses_api @api1
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
- @api2 = Her::API.new
46
- @api2.setup :base_uri => "https://api2.example.com" do |builder|
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
- Comment.uses_api @api2
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 :base_uri => "https://api1.example.com" do |builder|
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
- @api = Her::API.new
74
- @api.setup :base_uri => "https://api2.example.com" do |builder|
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
- Comment.uses_api @api
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
- @api = Her::API.new
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.use Faraday::Adapter::NetHttp
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
- @api = Her::API.new
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.use Faraday::Adapter::NetHttp
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
- FakeWeb.register_uri(:get, "https://api.example.com/users/popular", :body => [{ :id => 1 }, { :id => 2 }].to_json)
195
- FakeWeb.register_uri(:post, "https://api.example.com/users/from_default", :body => { :id => 4 }.to_json)
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 :base_uri => "https://api.example.com" do |builder|
8
- builder.use Her::Middleware::FirstLevelParseJSON
9
- builder.use Faraday::Request::UrlEncoded
10
- builder.use Faraday::Adapter::NetHttp
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
 
@@ -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 :base_uri => "https://api.example.com" do |builder|
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.use Faraday::Adapter::NetHttp
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 "creating resources" do
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.setup :base_uri => "https://api.example.com" do |builder|
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.use Faraday::Adapter::NetHttp
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
- FakeWeb.register_uri(:post, "https://api.example.com/users", :body => { :id => 1, :fullname => "Tobias Fünke" }.to_json)
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
- @api = Her::API.new
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.use Faraday::Adapter::NetHttp
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
- @api = Her::API.new
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.use Faraday::Adapter::NetHttp
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