her 0.4 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/{docs/CONTRIBUTING.md → CONTRIBUTING.md} +0 -0
- data/README.md +46 -44
- data/{docs/UPGRADE.md → UPGRADE.md} +0 -0
- data/her.gemspec +1 -1
- data/lib/her/api.rb +18 -1
- data/lib/her/errors.rb +1 -0
- data/lib/her/middleware/first_level_parse_json.rb +6 -1
- data/lib/her/model.rb +2 -0
- data/lib/her/model/http.rb +11 -11
- data/lib/her/model/nested_attributes.rb +57 -0
- data/lib/her/model/orm.rb +14 -9
- data/lib/her/model/relationships.rb +1 -1
- data/lib/her/version.rb +1 -1
- data/spec/api_spec.rb +77 -76
- data/spec/collection_spec.rb +10 -11
- data/spec/middleware/first_level_parse_json_spec.rb +10 -0
- data/spec/model/http_spec.rb +105 -186
- data/spec/model/nested_attributes_spec.rb +98 -0
- data/spec/model/relationships_spec.rb +26 -0
- data/spec/model_spec.rb +15 -27
- data/spec/spec_helper.rb +4 -4
- metadata +12 -29
data/lib/her/version.rb
CHANGED
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
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
27
|
-
|
28
|
-
|
26
|
+
context "when setting custom middleware" do
|
27
|
+
before do
|
28
|
+
class Foo; end;
|
29
|
+
class Bar; end;
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
31
|
+
subject.setup :url => "https://api.example.com" do |connection|
|
32
|
+
connection.use Foo
|
33
|
+
connection.use Bar
|
34
|
+
end
|
34
35
|
end
|
35
|
-
|
36
|
+
|
37
|
+
specify { subject.connection.builder.handlers.should == [Foo, Bar] }
|
36
38
|
end
|
37
39
|
|
38
|
-
|
39
|
-
|
40
|
-
|
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
|
-
|
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
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
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
|
-
|
63
|
-
parsed_data[:data] == "Foo, it is."
|
64
|
+
specify { parsed_data[:data].should == "Foo, it is." }
|
64
65
|
end
|
65
66
|
|
66
|
-
|
67
|
-
|
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
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
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
|
-
|
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
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
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
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
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
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
:
|
109
|
-
:
|
110
|
-
|
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
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
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
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
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
|
data/spec/collection_spec.rb
CHANGED
@@ -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
|
data/spec/model/http_spec.rb
CHANGED
@@ -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
|
-
|
7
|
-
|
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
|
-
|
11
|
-
Foo::User
|
12
|
-
Foo::
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
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
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
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
|
-
|
76
|
-
|
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
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
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
|
-
|
91
|
-
|
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
|
-
|
95
|
-
|
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
|
-
|
101
|
-
|
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")
|
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 =>
|
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
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
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
|
-
|
143
|
-
|
144
|
-
|
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
|
-
|
154
|
-
|
155
|
-
|
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
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
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
|
-
|
176
|
-
|
177
|
-
|
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
|
-
|
187
|
-
|
188
|
-
|
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
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
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
|
-
|
221
|
-
|
222
|
-
|
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
|
-
|
226
|
-
|
227
|
-
|
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 |
|
235
|
-
|
236
|
-
|
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
|
-
|
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
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
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
|
-
|
261
|
-
|
262
|
-
|
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
|