her 0.4 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -43,7 +43,7 @@ module Her
43
43
  when :has_many
44
44
  Her::Model::ORM.initialize_collection(klass, :data => data[name])
45
45
  when :has_one, :belongs_to
46
- klass.new(data[name])
46
+ klass.new(klass.parse(data[name]))
47
47
  else
48
48
  nil
49
49
  end
data/lib/her/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Her
2
- VERSION = "0.4"
2
+ VERSION = "0.4.1"
3
3
  end
data/spec/api_spec.rb CHANGED
@@ -2,6 +2,8 @@
2
2
  require File.join(File.dirname(__FILE__), "spec_helper.rb")
3
3
 
4
4
  describe Her::API do
5
+ subject { Her::API.new }
6
+
5
7
  context "initialization" do
6
8
  describe ".setup" do
7
9
  it "creates a default connection" do
@@ -11,119 +13,118 @@ describe Her::API do
11
13
  end
12
14
 
13
15
  describe "#setup" do
14
- it "sets a base URI" do
15
- @api = Her::API.new
16
- @api.setup :url => "https://api.example.com"
17
- @api.base_uri.should == "https://api.example.com"
16
+ context "when using :url option" do
17
+ before { subject.setup :url => "https://api.example.com" }
18
+ its(:base_uri) { should == "https://api.example.com" }
18
19
  end
19
20
 
20
- it "supports the base_uri legacy option" do
21
- @api = Her::API.new
22
- @api.setup :base_uri => "https://api.example.com"
23
- @api.base_uri.should == "https://api.example.com"
21
+ context "when using the legacy :base_uri option" do
22
+ before { subject.setup :base_uri => "https://api.example.com" }
23
+ its(:base_uri) { should == "https://api.example.com" }
24
24
  end
25
25
 
26
- it "sets custom middleware with #use" do
27
- class Foo; end;
28
- class Bar; end;
26
+ context "when setting custom middleware" do
27
+ before do
28
+ class Foo; end;
29
+ class Bar; end;
29
30
 
30
- @api = Her::API.new
31
- @api.setup :url => "https://api.example.com" do |builder|
32
- builder.use Foo
33
- builder.use Bar
31
+ subject.setup :url => "https://api.example.com" do |connection|
32
+ connection.use Foo
33
+ connection.use Bar
34
+ end
34
35
  end
35
- @api.connection.builder.handlers.should == [Foo, Bar]
36
+
37
+ specify { subject.connection.builder.handlers.should == [Foo, Bar] }
36
38
  end
37
39
 
38
- it "takes custom options" do
39
- @api = Her::API.new
40
- @api.setup :foo => { :bar => "baz" }, :url => "https://api.example.com"
41
- @api.options.should == { :foo => { :bar => "baz" }, :url => "https://api.example.com" }
40
+ context "when setting custom options" do
41
+ before { subject.setup :foo => { :bar => "baz" }, :url => "https://api.example.com" }
42
+ its(:options) { should == { :foo => { :bar => "baz" }, :url => "https://api.example.com" } }
42
43
  end
43
44
  end
44
45
 
45
46
  describe "#request" do
46
- it "makes HTTP requests" do
47
+ before do
47
48
  class SimpleParser < Faraday::Response::Middleware
48
49
  def on_complete(env)
49
50
  env[:body] = { :data => env[:body] }
50
51
  end
51
52
  end
53
+ end
52
54
 
53
- @api = Her::API.new
54
- @api.setup :url => "https://api.example.com" do |builder|
55
- builder.use SimpleParser
56
- builder.use Faraday::Request::UrlEncoded
57
- builder.adapter :test do |stub|
58
- stub.get("/foo") { |env| [200, {}, "Foo it is"] }
55
+ context "making HTTP requests" do
56
+ let(:parsed_data) { subject.request(:_method => :get, :_path => "/foo") }
57
+ before do
58
+ subject.setup :url => "https://api.example.com" do |builder|
59
+ builder.use SimpleParser
60
+ builder.adapter(:test) { |stub| stub.get("/foo") { |env| [200, {}, "Foo, it is."] } }
59
61
  end
60
62
  end
61
63
 
62
- parsed_data = @api.request(:_method => :get, :_path => "/foo")
63
- parsed_data[:data] == "Foo, it is."
64
+ specify { parsed_data[:data].should == "Foo, it is." }
64
65
  end
65
66
 
66
- it "makes HTTP requests while specifying custom HTTP headers" do
67
- class SimpleParser < Faraday::Response::Middleware
68
- def on_complete(env)
69
- env[:body] = { :data => env[:body] }
70
- end
71
- end
67
+ context "making HTTP requests while specifying custom HTTP headers" do
68
+ let(:parsed_data) { subject.request(:_method => :get, :_path => "/foo", :_headers => { "X-Page" => 2 }) }
72
69
 
73
- @api = Her::API.new
74
- @api.setup :url => "https://api.example.com" do |builder|
75
- builder.use SimpleParser
76
- builder.use Faraday::Request::UrlEncoded
77
- builder.adapter :test do |stub|
78
- stub.get("/foo") { |env| [200, {}, "Foo it is #{env[:request_headers]["X-Page"]}"] }
70
+ before do
71
+ subject.setup :url => "https://api.example.com" do |builder|
72
+ builder.use SimpleParser
73
+ builder.adapter(:test) { |stub| stub.get("/foo") { |env| [200, {}, "Foo, it is page #{env[:request_headers]["X-Page"]}."] } }
79
74
  end
80
75
  end
81
76
 
82
- parsed_data = @api.request(:_method => :get, :_path => "/foo", :_headers => { "X-Page" => 2 })
83
- parsed_data[:data] == "Foo, it is page 2."
77
+ specify { parsed_data[:data].should == "Foo, it is page 2." }
84
78
  end
85
79
 
86
- it "parses a request with the default parser" do
87
- @api = Her::API.new
88
- @api.setup :url => "https://api.example.com" do |builder|
89
- builder.use Her::Middleware::FirstLevelParseJSON
90
- builder.use Faraday::Request::UrlEncoded
91
- builder.adapter :test do |stub|
92
- 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 } })] }
80
+ context "parsing a request with the default parser" do
81
+ let(:parsed_data) { subject.request(:_method => :get, :_path => "users/1") }
82
+ before do
83
+ subject.setup :url => "https://api.example.com" do |builder|
84
+ builder.use Her::Middleware::FirstLevelParseJSON
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
93
88
  end
94
89
  end
95
- parsed_data = @api.request(:_method => :get, :_path => "users/1")
96
- parsed_data[:data].should == { :id => 1, :name => "George Michael Bluth" }
97
- parsed_data[:errors].should == ["This is a single error"]
98
- parsed_data[:metadata].should == { :page => 1, :per_page => 10 }
90
+
91
+ specify do
92
+ parsed_data[:data].should == { :id => 1, :name => "George Michael Bluth" }
93
+ parsed_data[:errors].should == ["This is a single error"]
94
+ parsed_data[:metadata].should == { :page => 1, :per_page => 10 }
95
+ end
99
96
  end
100
97
 
101
- it "parses a request with a custom parser" do
102
- class CustomParser < Faraday::Response::Middleware
103
- def on_complete(env)
104
- json = MultiJson.load(env[:body], :symbolize_keys => true)
105
- errors = json.delete(:errors) || []
106
- metadata = json.delete(:metadata) || {}
107
- env[:body] = {
108
- :data => json,
109
- :errors => errors,
110
- :metadata => metadata,
111
- }
98
+ context "parsing a request with a custom parser" do
99
+ let(:parsed_data) { subject.request(:_method => :get, :_path => "users/1") }
100
+ before do
101
+ class CustomParser < Faraday::Response::Middleware
102
+ def on_complete(env)
103
+ json = MultiJson.load(env[:body], :symbolize_keys => true)
104
+ errors = json.delete(:errors) || []
105
+ metadata = json.delete(:metadata) || {}
106
+ env[:body] = {
107
+ :data => json,
108
+ :errors => errors,
109
+ :metadata => metadata,
110
+ }
111
+ end
112
112
  end
113
- end
114
113
 
115
- @api = Her::API.new
116
- @api.setup :url => "https://api.example.com" do |builder|
117
- builder.use CustomParser
118
- builder.use Faraday::Request::UrlEncoded
119
- builder.adapter :test do |stub|
120
- stub.get("/users/1") { |env| [200, {}, MultiJson.dump(:id => 1, :name => "George Michael Bluth")] }
114
+ subject.setup :url => "https://api.example.com" do |builder|
115
+ builder.use CustomParser
116
+ builder.use Faraday::Request::UrlEncoded
117
+ builder.adapter :test do |stub|
118
+ stub.get("/users/1") { |env| [200, {}, MultiJson.dump(:id => 1, :name => "George Michael Bluth")] }
119
+ end
121
120
  end
122
121
  end
123
- parsed_data = @api.request(:_method => :get, :_path => "users/1")
124
- parsed_data[:data].should == { :id => 1, :name => "George Michael Bluth" }
125
- parsed_data[:errors].should == []
126
- parsed_data[:metadata].should == {}
122
+
123
+ specify do
124
+ parsed_data[:data].should == { :id => 1, :name => "George Michael Bluth" }
125
+ parsed_data[:errors].should == []
126
+ parsed_data[:metadata].should == {}
127
+ end
127
128
  end
128
129
  end
129
130
  end
@@ -2,26 +2,25 @@ require 'spec_helper'
2
2
 
3
3
  describe Her::Collection do
4
4
 
5
- let(:items) { [1,2,3,4] }
6
- let(:metadata) { {:name => 'Testname'} }
7
- let(:errors) { {:name => ['not_present']} }
5
+ let(:items) { [1, 2, 3, 4] }
6
+ let(:metadata) { { :name => 'Testname' } }
7
+ let(:errors) { { :name => ['not_present'] } }
8
8
 
9
9
  describe "#new" do
10
10
  context "without parameters" do
11
-
12
11
  subject { Her::Collection.new }
13
12
 
14
- it { should eq([])}
15
- its(:metadata) { should eq({})}
16
- its(:errors) { should eq({})}
13
+ it { should eq([]) }
14
+ its(:metadata) { should eq({}) }
15
+ its(:errors) { should eq({}) }
17
16
  end
18
17
 
19
18
  context "with parameters" do
20
- subject { Her::Collection.new(items, metadata, errors)}
19
+ subject { Her::Collection.new(items, metadata, errors) }
21
20
 
22
- it { should eq([1,2,3,4])}
23
- its(:metadata) { should eq({:name => 'Testname'})}
24
- its(:errors) { should eq({:name => ['not_present']})}
21
+ it { should eq([1,2,3,4]) }
22
+ its(:metadata) { should eq({ :name => 'Testname' }) }
23
+ its(:errors) { should eq({ :name => ['not_present'] }) }
25
24
  end
26
25
  end
27
26
  end
@@ -29,4 +29,14 @@ describe Her::Middleware::FirstLevelParseJSON do
29
29
  it 'ensures the errors are a hash if there are no errors' do
30
30
  subject.parse(body_with_errors)[:errors].should eq({:name => [ 'not_valid', 'should_be_present']})
31
31
  end
32
+
33
+ context 'with status code 204' do
34
+ it 'returns an empty body' do
35
+ env = { :status => 204 }
36
+ subject.on_complete(env)
37
+ env[:body].tap do |json|
38
+ json[:data].should == { }
39
+ end
40
+ end
41
+ end
32
42
  end
@@ -3,105 +3,62 @@ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
3
3
 
4
4
  describe Her::Model::HTTP do
5
5
  context "binding a model with an API" do
6
- it "binds a model to an instance of Her::API" do
7
- api = Her::API.new
8
- api.setup :url => "https://api.example.com"
6
+ let(:api1) { Her::API.new :url => "https://api1.example.com" }
7
+ let(:api2) { Her::API.new :url => "https://api2.example.com" }
9
8
 
10
- spawn_model "Foo::User"
11
- Foo::User.uses_api api
12
- Foo::User.her_api.should_not == nil
13
- Foo::User.her_api.base_uri.should == "https://api.example.com"
14
- end
15
-
16
- it "binds a model directly to Her::API" do
9
+ before do
10
+ spawn_model("Foo::User")
11
+ spawn_model("Foo::Comment")
17
12
  Her::API.setup :url => "https://api.example.com"
18
-
19
- spawn_model "Foo::User"
20
-
21
- Foo::User.her_api.should_not == nil
22
- Foo::User.her_api.base_uri.should == "https://api.example.com"
23
13
  end
24
14
 
25
- it "binds two models to two different instances of Her::API" do
26
- api1 = Her::API.new
27
- api1.setup :url => "https://api1.example.com" do |builder|
28
- builder.use Her::Middleware::FirstLevelParseJSON
29
- builder.use Faraday::Request::UrlEncoded
30
- end
31
-
32
- spawn_model "Foo::User"
33
- Foo::User.uses_api api1
34
- Foo::User.her_api.base_uri.should == "https://api1.example.com"
35
-
36
- api2 = Her::API.new
37
- api2.setup :url => "https://api2.example.com" do |builder|
38
- builder.use Her::Middleware::FirstLevelParseJSON
39
- builder.use Faraday::Request::UrlEncoded
40
- end
41
-
42
- spawn_model "Foo::Comment"
43
- Foo::Comment.uses_api api2
44
- Foo::Comment.her_api.base_uri.should == "https://api2.example.com"
15
+ context "when binding a model to an instance of Her::API" do
16
+ before { Foo::User.uses_api api1 }
17
+ subject { Foo::User.her_api }
18
+ its(:base_uri) { should == "https://api1.example.com" }
45
19
  end
46
20
 
47
- it "binds one model to Her::API and another one to an instance of Her::API" do
48
- Her::API.setup :url => "https://api1.example.com" do |builder|
49
- builder.use Her::Middleware::FirstLevelParseJSON
50
- builder.use Faraday::Request::UrlEncoded
51
- end
52
-
53
- spawn_model "Foo::User"
54
-
55
- Foo::User.her_api.base_uri.should == "https://api1.example.com"
56
-
57
- api = Her::API.new
58
- api.setup :url => "https://api2.example.com" do |builder|
59
- builder.use Her::Middleware::FirstLevelParseJSON
60
- builder.use Faraday::Request::UrlEncoded
61
- end
62
-
63
- spawn_model "Foo::Comment"
64
- Foo::Comment.uses_api api
65
- Foo::Comment.her_api.base_uri.should == "https://api2.example.com"
21
+ context "when binding a model directly to Her::API" do
22
+ before { spawn_model "Foo::User" }
23
+ subject { Foo::User.her_api }
24
+ its(:base_uri) { should == "https://api.example.com" }
66
25
  end
67
26
 
68
- it "binds a a model to it's superclass' her_api" do
69
- api = Her::API.new
70
- api.setup :url => "http://api.example.com" do |builder|
71
- builder.use Her::Middleware::FirstLevelParseJSON
72
- builder.use Faraday::Request::UrlEncoded
27
+ context "when binding two models to two different instances of Her::API" do
28
+ before do
29
+ Foo::User.uses_api api1
30
+ Foo::Comment.uses_api api2
73
31
  end
74
32
 
75
- spawn_model "Foo::Superclass" do
76
- uses_api api
77
- end
78
-
79
- Foo::Subclass = Class.new(Foo::Superclass)
80
- Foo::Subclass.her_api.should == Foo::Superclass.her_api
33
+ specify { Foo::User.her_api.base_uri.should == "https://api1.example.com" }
34
+ specify { Foo::Comment.her_api.base_uri.should == "https://api2.example.com" }
81
35
  end
82
36
 
83
- it "allows subclasses to change her_api without changing the parent class' her_api" do
84
- api1 = Her::API.new
85
- api1.setup :url => "http://api.example.com" do |builder|
86
- builder.use Her::Middleware::FirstLevelParseJSON
87
- builder.use Faraday::Request::UrlEncoded
88
- end
37
+ context "binding one model to Her::API and another one to an instance of Her::API" do
38
+ before { Foo::Comment.uses_api api2 }
39
+ specify { Foo::User.her_api.base_uri.should == "https://api.example.com" }
40
+ specify { Foo::Comment.her_api.base_uri.should == "https://api2.example.com" }
41
+ end
89
42
 
90
- spawn_model "Foo::Superclass" do
91
- uses_api api1
43
+ context "when binding a model to its superclass' her_api" do
44
+ before do
45
+ spawn_model "Foo::Superclass"
46
+ Foo::Superclass.uses_api api1
47
+ Foo::Subclass = Class.new(Foo::Superclass)
92
48
  end
93
49
 
94
- api2 = Her::API.new
95
- api2.setup :url => "http://api.example.com" do |builder|
96
- builder.use Her::Middleware::FirstLevelParseJSON
97
- builder.use Faraday::Request::UrlEncoded
98
- end
50
+ specify { Foo::Subclass.her_api.should == Foo::Superclass.her_api }
51
+ end
99
52
 
100
- Foo::Subclass = Class.new(Foo::Superclass) do
101
- uses_api api2
53
+ context "when changing her_api without changing the parent class' her_api" do
54
+ before do
55
+ spawn_model "Foo::Superclass"
56
+ Foo::Subclass = Class.new(Foo::Superclass)
57
+ Foo::Superclass.uses_api api1
58
+ Foo::Subclass.uses_api api2
102
59
  end
103
60
 
104
- Foo::Subclass.her_api.should_not == Foo::Superclass.her_api
61
+ specify { Foo::Subclass.her_api.should_not == Foo::Superclass.her_api }
105
62
  end
106
63
  end
107
64
 
@@ -111,155 +68,117 @@ describe Her::Model::HTTP do
111
68
  builder.use Her::Middleware::FirstLevelParseJSON
112
69
  builder.use Faraday::Request::UrlEncoded
113
70
  builder.adapter :test do |stub|
114
- stub.get("/users") do |env|
71
+ stub.get("/users") { |env| [200, {}, [{ :id => 1 }].to_json] }
72
+ stub.get("/users/1") { |env| [200, {}, { :id => 1 }.to_json] }
73
+ stub.get("/users/popular") do |env|
115
74
  if env[:params]["page"] == "2"
116
- [200, {}, [{ :id => 2 }].to_json]
75
+ [200, {}, [{ :id => 3 }, { :id => 4 }].to_json]
117
76
  else
118
- [200, {}, [{ :id => 1 }].to_json]
77
+ [200, {}, [{ :id => 1 }, { :id => 2 }].to_json]
119
78
  end
120
79
  end
121
- stub.get("/users/popular") { |env| [200, {}, [{ :id => 1 }, { :id => 2 }].to_json] }
122
- stub.get("/users/1") { |env| [200, {}, { :id => 1 }.to_json] }
123
- stub.post("/users") { |env| [200, {}, [{ :id => 3 }].to_json] }
124
- stub.put("/users/4") { |env| [200, {}, [{ :id => 4 }].to_json] }
125
- stub.patch("/users/6") { |env| [200, {}, [{ :id => 6 }].to_json] }
126
- stub.delete("/users/5") { |env| [200, {}, [{ :id => 5 }].to_json] }
127
80
  end
128
81
  end
129
82
 
130
83
  spawn_model "Foo::User"
131
84
  end
132
85
 
133
- it "handles GET wrapper method" do
134
- @users = Foo::User.get(:popular)
135
- @users.length.should == 2
136
- @users.first.id.should == 1
137
-
138
- @user = Foo::User.get(:"1")
139
- @user.id.should == 1
86
+ describe :get do
87
+ subject { Foo::User.get(:popular) }
88
+ its(:length) { should == 2 }
89
+ specify { subject.first.id.should == 1 }
140
90
  end
141
91
 
142
- it "handles raw GET with a block" do
143
- Foo::User.get_raw("/users") do |parsed_data|
144
- parsed_data[:data].should == [{ :id => 1 }]
92
+ describe :get_raw do
93
+ context "with a block" do
94
+ specify do
95
+ Foo::User.get_raw("/users") do |parsed_data|
96
+ parsed_data[:data].should == [{ :id => 1 }]
97
+ end
98
+ end
145
99
  end
146
- end
147
-
148
- it "handles raw GET with return value" do
149
- parsed_data = Foo::User.get_raw("/users")
150
- parsed_data[:data].should == [{ :id => 1 }]
151
- end
152
100
 
153
- it "handles raw POST with a block" do
154
- Foo::User.post_raw("/users") do |parsed_data|
155
- parsed_data[:data].should == [{ :id => 3 }]
101
+ context "with a return value" do
102
+ subject { Foo::User.get_raw("/users") }
103
+ specify { subject[:data].should == [{ :id => 1 }] }
156
104
  end
157
105
  end
158
106
 
159
- it "handles raw POST with return value" do
160
- parsed_data = Foo::User.post_raw("/users")
161
- parsed_data[:data].should == [{ :id => 3 }]
162
- end
163
-
164
- it "handles raw PUT with a block" do
165
- Foo::User.put_raw("/users/4") do |parsed_data|
166
- parsed_data[:data].should == [{ :id => 4 }]
107
+ describe :get_collection do
108
+ context "with a String path" do
109
+ subject { Foo::User.get_collection("/users/popular") }
110
+ its(:length) { should == 2 }
111
+ specify { subject.first.id.should == 1 }
167
112
  end
168
- end
169
-
170
- it "handles raw PUT with return value" do
171
- parsed_data = Foo::User.put_raw("/users/4")
172
- parsed_data[:data].should == [{ :id => 4 }]
173
- end
174
113
 
175
- it "handles raw PATCH with a block" do
176
- Foo::User.patch_raw("/users/6") do |parsed_data|
177
- parsed_data[:data].should == [{ :id => 6 }]
114
+ context "with a Symbol" do
115
+ subject { Foo::User.get_collection(:popular) }
116
+ its(:length) { should == 2 }
117
+ specify { subject.first.id.should == 1 }
178
118
  end
179
- end
180
-
181
- it "handles raw PATCH with return value" do
182
- parsed_data = Foo::User.patch_raw("/users/6")
183
- parsed_data[:data].should == [{ :id => 6 }]
184
- end
185
119
 
186
- it "handles raw DELETE with a block" do
187
- Foo::User.delete_raw("/users/5") do |parsed_data|
188
- parsed_data[:data].should == [{ :id => 5 }]
120
+ context "with extra parameters" do
121
+ subject { Foo::User.get_collection(:popular, :page => 2) }
122
+ its(:length) { should == 2 }
123
+ specify { subject.first.id.should == 3 }
189
124
  end
190
125
  end
191
126
 
192
- it "handles raw DELETE with return value" do
193
- parsed_data = Foo::User.delete_raw("/users/5")
194
- parsed_data[:data].should == [{ :id => 5 }]
195
- end
196
-
197
- it "handles querystring parameters" do
198
- Foo::User.get_raw("/users", :page => 2) do |parsed_data|
199
- parsed_data[:data].should == [{ :id => 2 }]
127
+ describe :get_resource do
128
+ context "with a String path" do
129
+ subject { Foo::User.get_resource("/users/1") }
130
+ its(:id) { should == 1 }
200
131
  end
201
- end
202
-
203
- it "handles GET collection" do
204
- @users = Foo::User.get_collection("/users/popular")
205
- @users.length.should == 2
206
- @users.first.id.should == 1
207
- end
208
-
209
- it "handles GET resource" do
210
- @user = Foo::User.get_resource("/users/1")
211
- @user.id.should == 1
212
- end
213
-
214
- it "handles GET collection through a symbol" do
215
- @users = Foo::User.get_collection(:popular)
216
- @users.length.should == 2
217
- @users.first.id.should == 1
218
- end
219
132
 
220
- it "handles GET resource through a symbol" do
221
- @user = Foo::User.get_resource(:"1")
222
- @user.id.should == 1
133
+ context "with a Symbol" do
134
+ subject { Foo::User.get_resource(:"1") }
135
+ its(:id) { should == 1 }
136
+ end
223
137
  end
224
138
 
225
- it "handles raw GET through a symbol" do
226
- Foo::User.get_raw(:popular) do |parsed_data|
227
- parsed_data[:data].should == [{ :id => 1 }, { :id => 2 }]
139
+ describe :get_raw do
140
+ specify do
141
+ Foo::User.get_raw(:popular) do |parsed_data|
142
+ parsed_data[:data].should == [{ :id => 1 }, { :id => 2 }]
143
+ end
228
144
  end
229
145
  end
230
146
  end
231
147
 
232
- context "setting custom requests" do
148
+ context "setting custom HTTP requests" do
233
149
  before do
234
- Her::API.setup :url => "https://api.example.com" do |builder|
235
- builder.use Her::Middleware::FirstLevelParseJSON
236
- builder.use Faraday::Request::UrlEncoded
237
- builder.adapter :test do |stub|
150
+ Her::API.setup :url => "https://api.example.com" do |connection|
151
+ connection.use Her::Middleware::FirstLevelParseJSON
152
+ connection.adapter :test do |stub|
238
153
  stub.get("/users/popular") { |env| [200, {}, [{ :id => 1 }, { :id => 2 }].to_json] }
239
154
  stub.post("/users/from_default") { |env| [200, {}, { :id => 4 }.to_json] }
240
155
  end
241
156
  end
242
157
 
243
158
  spawn_model "Foo::User"
244
- Foo::User.custom_get :popular, :foobar
245
- Foo::User.custom_post :from_default
246
159
  end
247
160
 
248
- it "handles custom methods" do
249
- Foo::User.respond_to?(:popular).should be_true
250
- Foo::User.respond_to?(:foobar).should be_true
251
- Foo::User.respond_to?(:from_default).should be_true
252
- end
161
+ subject { Foo::User }
253
162
 
254
- it "handles custom GET requests" do
255
- @users = Foo::User.popular
256
- @users.length.should == 2
257
- @users.first.id.should == 1
163
+ describe :custom_get do
164
+ before { Foo::User.custom_get :popular, :recent }
165
+ it { should respond_to(:popular) }
166
+ it { should respond_to(:recent) }
167
+
168
+ context "making the HTTP request" do
169
+ subject { Foo::User.popular }
170
+ its(:length) { should == 2 }
171
+ end
258
172
  end
259
173
 
260
- it "handles custom POST requests" do
261
- @user = Foo::User.from_default(:name => "Tobias Fünke")
262
- @user.id.should be_true
174
+ describe :custom_post do
175
+ before { Foo::User.custom_post :from_default }
176
+ it { should respond_to(:from_default) }
177
+
178
+ context "making the HTTP request" do
179
+ subject { Foo::User.from_default(:name => "Tobias Fünke") }
180
+ its(:id) { should == 4 }
181
+ end
263
182
  end
264
183
  end
265
184
  end