her 0.8.2 → 0.10.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.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +1 -1
  3. data/.rubocop.yml +1291 -0
  4. data/.travis.yml +6 -1
  5. data/README.md +29 -11
  6. data/her.gemspec +3 -5
  7. data/lib/her/api.rb +16 -9
  8. data/lib/her/middleware/json_api_parser.rb +1 -1
  9. data/lib/her/model/associations/association.rb +32 -5
  10. data/lib/her/model/associations/association_proxy.rb +1 -1
  11. data/lib/her/model/associations/belongs_to_association.rb +1 -1
  12. data/lib/her/model/associations/has_many_association.rb +3 -3
  13. data/lib/her/model/attributes.rb +105 -75
  14. data/lib/her/model/http.rb +3 -3
  15. data/lib/her/model/introspection.rb +1 -1
  16. data/lib/her/model/orm.rb +96 -19
  17. data/lib/her/model/parse.rb +27 -17
  18. data/lib/her/model/relation.rb +46 -2
  19. data/lib/her/version.rb +1 -1
  20. data/spec/api_spec.rb +34 -31
  21. data/spec/collection_spec.rb +25 -10
  22. data/spec/json_api/model_spec.rb +75 -72
  23. data/spec/middleware/accept_json_spec.rb +1 -1
  24. data/spec/middleware/first_level_parse_json_spec.rb +20 -20
  25. data/spec/middleware/json_api_parser_spec.rb +26 -7
  26. data/spec/middleware/second_level_parse_json_spec.rb +8 -9
  27. data/spec/model/associations/association_proxy_spec.rb +2 -5
  28. data/spec/model/associations_spec.rb +617 -295
  29. data/spec/model/attributes_spec.rb +114 -107
  30. data/spec/model/callbacks_spec.rb +59 -27
  31. data/spec/model/dirty_spec.rb +70 -29
  32. data/spec/model/http_spec.rb +67 -35
  33. data/spec/model/introspection_spec.rb +26 -22
  34. data/spec/model/nested_attributes_spec.rb +31 -31
  35. data/spec/model/orm_spec.rb +332 -157
  36. data/spec/model/parse_spec.rb +250 -77
  37. data/spec/model/paths_spec.rb +109 -109
  38. data/spec/model/relation_spec.rb +89 -69
  39. data/spec/model/validations_spec.rb +6 -6
  40. data/spec/model_spec.rb +17 -17
  41. data/spec/spec_helper.rb +2 -3
  42. data/spec/support/macros/model_macros.rb +2 -2
  43. metadata +36 -63
@@ -3,7 +3,7 @@ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
3
3
 
4
4
  describe "Her::Model and ActiveModel::Callbacks" do
5
5
  before do
6
- Her::API.setup :url => "https://api.example.com" do |builder|
6
+ Her::API.setup url: "https://api.example.com" do |builder|
7
7
  builder.use Her::Middleware::FirstLevelParseJSON
8
8
  end
9
9
 
@@ -11,11 +11,11 @@ describe "Her::Model and ActiveModel::Callbacks" do
11
11
  end
12
12
 
13
13
  context :before_save do
14
- subject { Foo::User.create(:name => "Tobias Funke") }
14
+ subject { Foo::User.create(name: "Tobias Funke") }
15
15
  before do
16
16
  Her::API.default_api.connection.adapter :test do |stub|
17
- stub.post("/users") { |env| [200, {}, { :id => 1, :name => env[:body][:name] }.to_json] }
18
- stub.put("/users/1") { |env| [200, {}, { :id => 1, :name => env[:body][:name] }.to_json] }
17
+ stub.post("/users") { |env| [200, {}, { id: 1, name: env[:body][:name] }.to_json] }
18
+ stub.put("/users/1") { |env| [200, {}, { id: 1, name: env[:body][:name] }.to_json] }
19
19
  end
20
20
  end
21
21
 
@@ -23,21 +23,29 @@ describe "Her::Model and ActiveModel::Callbacks" do
23
23
  before do
24
24
  class Foo::User
25
25
  before_save :alter_name
26
- def alter_name; self.name.upcase!; end
26
+ def alter_name
27
+ name.upcase!
28
+ end
27
29
  end
28
30
  end
29
31
 
30
- its(:name) { should == "TOBIAS FUNKE" }
32
+ describe "#name" do
33
+ subject { super().name }
34
+ it { is_expected.to eq("TOBIAS FUNKE") }
35
+ end
31
36
  end
32
37
 
33
38
  context "when using a block callback" do
34
39
  before do
35
40
  class Foo::User
36
- before_save lambda { self.name.upcase! }
41
+ before_save -> { name.upcase! }
37
42
  end
38
43
  end
39
44
 
40
- its(:name) { should == "TOBIAS FUNKE" }
45
+ describe "#name" do
46
+ subject { super().name }
47
+ it { is_expected.to eq("TOBIAS FUNKE") }
48
+ end
41
49
  end
42
50
 
43
51
  context "when changing a value of an existing resource in a callback" do
@@ -50,19 +58,19 @@ describe "Her::Model and ActiveModel::Callbacks" do
50
58
  end
51
59
  end
52
60
 
53
- it "should call the server with the canged value" do
54
- subject.name.should == "Tobias Funke"
61
+ it "should call the server with the changed value" do
62
+ expect(subject.name).to eq("Tobias Funke")
55
63
  subject.save
56
- subject.name.should == "Lumberjack"
64
+ expect(subject.name).to eq("Lumberjack")
57
65
  end
58
66
  end
59
67
  end
60
68
 
61
69
  context :before_create do
62
- subject { Foo::User.create(:name => "Tobias Funke") }
70
+ subject { Foo::User.create(name: "Tobias Funke") }
63
71
  before do
64
72
  Her::API.default_api.connection.adapter :test do |stub|
65
- stub.post("/users") { |env| [200, {}, { :id => 1, :name => env[:body][:name] }.to_json] }
73
+ stub.post("/users") { |env| [200, {}, { id: 1, name: env[:body][:name] }.to_json] }
66
74
  end
67
75
  end
68
76
 
@@ -70,21 +78,29 @@ describe "Her::Model and ActiveModel::Callbacks" do
70
78
  before do
71
79
  class Foo::User
72
80
  before_create :alter_name
73
- def alter_name; self.name.upcase!; end
81
+ def alter_name
82
+ name.upcase!
83
+ end
74
84
  end
75
85
  end
76
86
 
77
- its(:name) { should == "TOBIAS FUNKE" }
87
+ describe "#name" do
88
+ subject { super().name }
89
+ it { is_expected.to eq("TOBIAS FUNKE") }
90
+ end
78
91
  end
79
92
 
80
93
  context "when using a block callback" do
81
94
  before do
82
95
  class Foo::User
83
- before_create lambda { self.name.upcase! }
96
+ before_create -> { name.upcase! }
84
97
  end
85
98
  end
86
99
 
87
- its(:name) { should == "TOBIAS FUNKE" }
100
+ describe "#name" do
101
+ subject { super().name }
102
+ it { is_expected.to eq("TOBIAS FUNKE") }
103
+ end
88
104
  end
89
105
  end
90
106
 
@@ -92,7 +108,7 @@ describe "Her::Model and ActiveModel::Callbacks" do
92
108
  subject { Foo::User.find(1) }
93
109
  before do
94
110
  Her::API.default_api.connection.adapter :test do |stub|
95
- stub.get("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Funke" }.to_json] }
111
+ stub.get("/users/1") { [200, {}, { id: 1, name: "Tobias Funke" }.to_json] }
96
112
  end
97
113
  end
98
114
 
@@ -100,46 +116,62 @@ describe "Her::Model and ActiveModel::Callbacks" do
100
116
  before do
101
117
  class Foo::User
102
118
  after_find :alter_name
103
- def alter_name; self.name.upcase!; end
119
+ def alter_name
120
+ name.upcase!
121
+ end
104
122
  end
105
123
  end
106
124
 
107
- its(:name) { should == "TOBIAS FUNKE" }
125
+ describe "#name" do
126
+ subject { super().name }
127
+ it { is_expected.to eq("TOBIAS FUNKE") }
128
+ end
108
129
  end
109
130
 
110
131
  context "when using a block callback" do
111
132
  before do
112
133
  class Foo::User
113
- after_find lambda { self.name.upcase! }
134
+ after_find -> { name.upcase! }
114
135
  end
115
136
  end
116
137
 
117
- its(:name) { should == "TOBIAS FUNKE" }
138
+ describe "#name" do
139
+ subject { super().name }
140
+ it { is_expected.to eq("TOBIAS FUNKE") }
141
+ end
118
142
  end
119
143
  end
120
144
 
121
145
  context :after_initialize do
122
- subject { Foo::User.new(:name => "Tobias Funke") }
146
+ subject { Foo::User.new(name: "Tobias Funke") }
123
147
 
124
148
  context "when using a symbol callback" do
125
149
  before do
126
150
  class Foo::User
127
151
  after_initialize :alter_name
128
- def alter_name; self.name.upcase!; end
152
+ def alter_name
153
+ name.upcase!
154
+ end
129
155
  end
130
156
  end
131
157
 
132
- its(:name) { should == "TOBIAS FUNKE" }
158
+ describe "#name" do
159
+ subject { super().name }
160
+ it { is_expected.to eq("TOBIAS FUNKE") }
161
+ end
133
162
  end
134
163
 
135
164
  context "when using a block callback" do
136
165
  before do
137
166
  class Foo::User
138
- after_initialize lambda { self.name.upcase! }
167
+ after_initialize -> { name.upcase! }
139
168
  end
140
169
  end
141
170
 
142
- its(:name) { should == "TOBIAS FUNKE" }
171
+ describe "#name" do
172
+ subject { super().name }
173
+ it { is_expected.to eq("TOBIAS FUNKE") }
174
+ end
143
175
  end
144
176
  end
145
177
  end
@@ -4,20 +4,28 @@ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
4
4
  describe "Her::Model and ActiveModel::Dirty" do
5
5
  context "checking dirty attributes" do
6
6
  before do
7
- Her::API.setup :url => "https://api.example.com" do |builder|
7
+ Her::API.setup url: "https://api.example.com" do |builder|
8
8
  builder.use Her::Middleware::FirstLevelParseJSON
9
9
  builder.use Faraday::Request::UrlEncoded
10
10
  builder.adapter :test do |stub|
11
- stub.get("/users/1") { |env| [200, {}, { :id => 1, :fullname => "Lindsay Fünke" }.to_json] }
12
- stub.get("/users/2") { |env| [200, {}, { :id => 2, :fullname => "Maeby Fünke" }.to_json] }
13
- stub.get("/users/3") { |env| [200, {}, { :user_id => 3, :fullname => "Maeby Fünke" }.to_json] }
14
- stub.put("/users/1") { |env| [200, {}, { :id => 1, :fullname => "Tobias Fünke" }.to_json] }
15
- stub.put("/users/2") { |env| [400, {}, { :errors => ["Email cannot be blank"] }.to_json] }
16
- stub.post("/users") { |env| [200, {}, { :id => 1, :fullname => "Tobias Fünke" }.to_json] }
11
+ stub.get("/users") { [200, {}, [{ id: 1, fullname: "Lindsay Fünke" }, { id: 2, fullname: "Maeby Fünke" }].to_json] }
12
+ stub.get("/users/1") { [200, {}, { id: 1, fullname: "Lindsay Fünke" }.to_json] }
13
+ stub.get("/users/2") { [200, {}, { id: 2, fullname: "Maeby Fünke" }.to_json] }
14
+ stub.get("/users/3") { [200, {}, { user_id: 3, fullname: "Maeby Fünke" }.to_json] }
15
+ stub.put("/users/1") { [200, {}, { id: 1, fullname: "Tobias Fünke" }.to_json] }
16
+ stub.put("/users/2") { [400, {}, { errors: ["Email cannot be blank"] }.to_json] }
17
+ stub.post("/users") { [200, {}, { id: 1, fullname: "Tobias Fünke" }.to_json] }
18
+ stub.get("/users/1/posts") { [200, {}, [{ id: 1, user_id: 1, body: "Hello" }].to_json] }
19
+ stub.get("/users/1/posts/1") { [200, {}, { id: 1, user_id: 1, body: "Hello" }.to_json] }
17
20
  end
18
21
  end
19
22
 
23
+ spawn_model "Foo::Post" do
24
+ belongs_to :user
25
+ attributes :body
26
+ end
20
27
  spawn_model "Foo::User" do
28
+ has_many :posts
21
29
  attributes :fullname, :email
22
30
  end
23
31
  spawn_model "Dynamic::User" do
@@ -28,36 +36,36 @@ describe "Her::Model and ActiveModel::Dirty" do
28
36
  context "for existing resource" do
29
37
  let(:user) { Foo::User.find(1) }
30
38
  it "has no changes" do
31
- user.changes.should be_empty
32
- user.should_not be_changed
39
+ expect(user.changes).to be_empty
40
+ expect(user).not_to be_changed
33
41
  end
34
42
  context "with successful save" do
35
43
  it "tracks dirty attributes" do
36
44
  user.fullname = "Tobias Fünke"
37
- user.fullname_changed?.should be_truthy
38
- user.email_changed?.should be_falsey
39
- user.should be_changed
45
+ expect(user.fullname_changed?).to be_truthy
46
+ expect(user.email_changed?).to be_falsey
47
+ expect(user).to be_changed
40
48
  user.save
41
- user.should_not be_changed
49
+ expect(user).not_to be_changed
42
50
  end
43
51
 
44
52
  it "tracks only changed dirty attributes" do
45
53
  user.fullname = user.fullname
46
- user.fullname_changed?.should be_falsey
54
+ expect(user.fullname_changed?).to be_falsey
47
55
  end
48
56
 
49
57
  it "tracks previous changes" do
50
58
  user.fullname = "Tobias Fünke"
51
59
  user.save
52
- user.previous_changes.should eq({"fullname"=>"Lindsay Fünke"})
60
+ expect(user.previous_changes).to eq("fullname" => ["Lindsay Fünke", "Tobias Fünke"])
53
61
  end
54
62
 
55
- it 'tracks dirty attribute for mass assign for dynamic created attributes' do
63
+ it "tracks dirty attribute for mass assign for dynamic created attributes" do
56
64
  user = Dynamic::User.find(3)
57
- user.assign_attributes(:fullname => 'New Fullname')
58
- user.fullname_changed?.should be_truthy
59
- user.should be_changed
60
- user.changes.length.should eq(1)
65
+ user.assign_attributes(fullname: "New Fullname")
66
+ expect(user.fullname_changed?).to be_truthy
67
+ expect(user).to be_changed
68
+ expect(user.changes.length).to eq(1)
61
69
  end
62
70
  end
63
71
 
@@ -65,26 +73,59 @@ describe "Her::Model and ActiveModel::Dirty" do
65
73
  it "tracks dirty attributes" do
66
74
  user = Foo::User.find(2)
67
75
  user.fullname = "Tobias Fünke"
68
- user.fullname_changed?.should be_truthy
69
- user.email_changed?.should be_falsey
70
- user.should be_changed
76
+ expect(user.fullname_changed?).to be_truthy
77
+ expect(user.email_changed?).to be_falsey
78
+ expect(user).to be_changed
71
79
  user.save
72
- user.should be_changed
80
+ expect(user).to be_changed
81
+ end
82
+ end
83
+ end
84
+
85
+ context "for an existing resource from an association" do
86
+ let(:post) { Foo::User.find(1).posts.find(1) }
87
+ it "has no changes" do
88
+ expect(post.changes).to be_empty
89
+ expect(post).to_not be_changed
90
+ end
91
+ end
92
+
93
+ context "for an existing resource from an association collection" do
94
+ let(:post) { Foo::User.find(1).posts.first }
95
+ it "has no changes" do
96
+ expect(post.changes).to be_empty
97
+ expect(post).to_not be_changed
98
+ end
99
+ end
100
+
101
+ context "for resources from a collection" do
102
+ let(:users) { Foo::User.all.fetch }
103
+ it "has no changes" do
104
+ users.each do |user|
105
+ expect(user.changes).to be_empty
106
+ expect(user).to_not be_changed
73
107
  end
74
108
  end
75
109
  end
76
110
 
77
111
  context "for new resource" do
78
- let(:user) { Foo::User.new(:fullname => "Lindsay Fünke") }
112
+ let(:user) { Foo::User.new(fullname: "Lindsay Fünke") }
79
113
  it "has changes" do
80
- user.should be_changed
114
+ expect(user).to be_changed
81
115
  end
82
116
  it "tracks dirty attributes" do
83
117
  user.fullname = "Tobias Fünke"
84
- user.fullname_changed?.should be_truthy
85
- user.should be_changed
118
+ expect(user.fullname_changed?).to be_truthy
119
+ expect(user).to be_changed
86
120
  user.save
87
- user.should_not be_changed
121
+ expect(user).not_to be_changed
122
+ end
123
+ end
124
+
125
+ context "for a new resource from an association" do
126
+ let(:post) { Foo::User.find(1).posts.build }
127
+ it "has changes" do
128
+ expect(post).to be_changed
88
129
  end
89
130
  end
90
131
  end
@@ -3,13 +3,13 @@ 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
- let(:api1) { Her::API.new :url => "https://api1.example.com" }
7
- let(:api2) { Her::API.new :url => "https://api2.example.com" }
6
+ let(:api1) { Her::API.new url: "https://api1.example.com" }
7
+ let(:api2) { Her::API.new url: "https://api2.example.com" }
8
8
 
9
9
  before do
10
10
  spawn_model("Foo::User")
11
11
  spawn_model("Foo::Comment")
12
- Her::API.setup :url => "https://api.example.com"
12
+ Her::API.setup url: "https://api.example.com"
13
13
  end
14
14
 
15
15
  context "when binding a model to its superclass' her_api" do
@@ -19,7 +19,7 @@ describe Her::Model::HTTP do
19
19
  Foo::Subclass = Class.new(Foo::Superclass)
20
20
  end
21
21
 
22
- specify { Foo::Subclass.her_api.should == Foo::Superclass.her_api }
22
+ specify { expect(Foo::Subclass.her_api).to eq(Foo::Superclass.her_api) }
23
23
  end
24
24
 
25
25
  context "when changing her_api without changing the parent class' her_api" do
@@ -30,23 +30,23 @@ describe Her::Model::HTTP do
30
30
  Foo::Subclass.uses_api api2
31
31
  end
32
32
 
33
- specify { Foo::Subclass.her_api.should_not == Foo::Superclass.her_api }
33
+ specify { expect(Foo::Subclass.her_api).not_to eq(Foo::Superclass.her_api) }
34
34
  end
35
35
  end
36
36
 
37
37
  context "making HTTP requests" do
38
38
  before do
39
- Her::API.setup :url => "https://api.example.com" do |builder|
39
+ Her::API.setup url: "https://api.example.com" do |builder|
40
40
  builder.use Her::Middleware::FirstLevelParseJSON
41
41
  builder.use Faraday::Request::UrlEncoded
42
42
  builder.adapter :test do |stub|
43
- stub.get("/users") { |env| [200, {}, [{ :id => 1 }].to_json] }
44
- stub.get("/users/1") { |env| [200, {}, { :id => 1 }.to_json] }
43
+ stub.get("/users") { [200, {}, [{ id: 1 }].to_json] }
44
+ stub.get("/users/1") { [200, {}, { id: 1 }.to_json] }
45
45
  stub.get("/users/popular") do |env|
46
46
  if env[:params]["page"] == "2"
47
- [200, {}, [{ :id => 3 }, { :id => 4 }].to_json]
47
+ [200, {}, [{ id: 3 }, { id: 4 }].to_json]
48
48
  else
49
- [200, {}, [{ :id => 1 }, { :id => 2 }].to_json]
49
+ [200, {}, [{ id: 1 }, { id: 2 }].to_json]
50
50
  end
51
51
  end
52
52
  end
@@ -57,61 +57,85 @@ describe Her::Model::HTTP do
57
57
 
58
58
  describe :get do
59
59
  subject { Foo::User.get(:popular) }
60
- its(:length) { should == 2 }
61
- specify { subject.first.id.should == 1 }
60
+
61
+ describe "#length" do
62
+ subject { super().length }
63
+ it { is_expected.to eq(2) }
64
+ end
65
+ specify { expect(subject.first.id).to eq(1) }
62
66
  end
63
67
 
64
68
  describe :get_raw do
65
69
  context "with a block" do
66
70
  specify do
67
- Foo::User.get_raw("/users") do |parsed_data, response|
68
- parsed_data[:data].should == [{ :id => 1 }]
71
+ Foo::User.get_raw("/users") do |parsed_data, _response|
72
+ expect(parsed_data[:data]).to eq([{ id: 1 }])
69
73
  end
70
74
  end
71
75
  end
72
76
 
73
77
  context "with a return value" do
74
78
  subject { Foo::User.get_raw("/users") }
75
- specify { subject[:parsed_data][:data].should == [{ :id => 1 }] }
79
+ specify { expect(subject[:parsed_data][:data]).to eq([{ id: 1 }]) }
76
80
  end
77
81
  end
78
82
 
79
83
  describe :get_collection do
80
84
  context "with a String path" do
81
85
  subject { Foo::User.get_collection("/users/popular") }
82
- its(:length) { should == 2 }
83
- specify { subject.first.id.should == 1 }
86
+
87
+ describe "#length" do
88
+ subject { super().length }
89
+ it { is_expected.to eq(2) }
90
+ end
91
+ specify { expect(subject.first.id).to eq(1) }
84
92
  end
85
93
 
86
94
  context "with a Symbol" do
87
95
  subject { Foo::User.get_collection(:popular) }
88
- its(:length) { should == 2 }
89
- specify { subject.first.id.should == 1 }
96
+
97
+ describe "#length" do
98
+ subject { super().length }
99
+ it { is_expected.to eq(2) }
100
+ end
101
+ specify { expect(subject.first.id).to eq(1) }
90
102
  end
91
103
 
92
104
  context "with extra parameters" do
93
- subject { Foo::User.get_collection(:popular, :page => 2) }
94
- its(:length) { should == 2 }
95
- specify { subject.first.id.should == 3 }
105
+ subject { Foo::User.get_collection(:popular, page: 2) }
106
+
107
+ describe "#length" do
108
+ subject { super().length }
109
+ it { is_expected.to eq(2) }
110
+ end
111
+ specify { expect(subject.first.id).to eq(3) }
96
112
  end
97
113
  end
98
114
 
99
115
  describe :get_resource do
100
116
  context "with a String path" do
101
117
  subject { Foo::User.get_resource("/users/1") }
102
- its(:id) { should == 1 }
118
+
119
+ describe "#id" do
120
+ subject { super().id }
121
+ it { is_expected.to eq(1) }
122
+ end
103
123
  end
104
124
 
105
125
  context "with a Symbol" do
106
126
  subject { Foo::User.get_resource(:"1") }
107
- its(:id) { should == 1 }
127
+
128
+ describe "#id" do
129
+ subject { super().id }
130
+ it { is_expected.to eq(1) }
131
+ end
108
132
  end
109
133
  end
110
134
 
111
135
  describe :get_raw do
112
136
  specify do
113
- Foo::User.get_raw(:popular) do |parsed_data, response|
114
- parsed_data[:data].should == [{ :id => 1 }, { :id => 2 }]
137
+ Foo::User.get_raw(:popular) do |parsed_data, _response|
138
+ expect(parsed_data[:data]).to eq([{ id: 1 }, { id: 2 }])
115
139
  end
116
140
  end
117
141
  end
@@ -119,11 +143,11 @@ describe Her::Model::HTTP do
119
143
 
120
144
  context "setting custom HTTP requests" do
121
145
  before do
122
- Her::API.setup :url => "https://api.example.com" do |connection|
146
+ Her::API.setup url: "https://api.example.com" do |connection|
123
147
  connection.use Her::Middleware::FirstLevelParseJSON
124
148
  connection.adapter :test do |stub|
125
- stub.get("/users/popular") { |env| [200, {}, [{ :id => 1 }, { :id => 2 }].to_json] }
126
- stub.post("/users/from_default") { |env| [200, {}, { :id => 4 }.to_json] }
149
+ stub.get("/users/popular") { [200, {}, [{ id: 1 }, { id: 2 }].to_json] }
150
+ stub.post("/users/from_default") { [200, {}, { id: 4 }.to_json] }
127
151
  end
128
152
  end
129
153
 
@@ -135,23 +159,31 @@ describe Her::Model::HTTP do
135
159
  describe :custom_get do
136
160
  context "without cache" do
137
161
  before { Foo::User.custom_get :popular, :recent }
138
- it { should respond_to(:popular) }
139
- it { should respond_to(:recent) }
162
+ it { is_expected.to respond_to(:popular) }
163
+ it { is_expected.to respond_to(:recent) }
140
164
 
141
165
  context "making the HTTP request" do
142
166
  subject { Foo::User.popular }
143
- its(:length) { should == 2 }
167
+
168
+ describe "#length" do
169
+ subject { super().length }
170
+ it { is_expected.to eq(2) }
171
+ end
144
172
  end
145
173
  end
146
174
  end
147
175
 
148
176
  describe :custom_post do
149
177
  before { Foo::User.custom_post :from_default }
150
- it { should respond_to(:from_default) }
178
+ it { is_expected.to respond_to(:from_default) }
151
179
 
152
180
  context "making the HTTP request" do
153
- subject { Foo::User.from_default(:name => "Tobias Fünke") }
154
- its(:id) { should == 4 }
181
+ subject { Foo::User.from_default(name: "Tobias Fünke") }
182
+
183
+ describe "#id" do
184
+ subject { super().id }
185
+ it { is_expected.to eq(4) }
186
+ end
155
187
  end
156
188
  end
157
189
  end
@@ -4,15 +4,15 @@ 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 :url => "https://api.example.com" do |builder|
7
+ Her::API.setup url: "https://api.example.com" do |builder|
8
8
  builder.use Her::Middleware::FirstLevelParseJSON
9
9
  builder.use Faraday::Request::UrlEncoded
10
10
  builder.adapter :test do |stub|
11
- stub.post("/users") { |env| [200, {}, { :id => 1, :name => "Tobias Funke" }.to_json] }
12
- stub.get("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Funke" }.to_json] }
13
- stub.put("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Funke" }.to_json] }
14
- stub.delete("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Funke" }.to_json] }
15
- stub.get("/projects/1/comments") { |env| [200, {}, [{ :id => 1, :body => "Hello!" }].to_json] }
11
+ stub.post("/users") { [200, {}, { id: 1, name: "Tobias Funke" }.to_json] }
12
+ stub.get("/users/1") { [200, {}, { id: 1, name: "Tobias Funke" }.to_json] }
13
+ stub.put("/users/1") { [200, {}, { id: 1, name: "Tobias Funke" }.to_json] }
14
+ stub.delete("/users/1") { [200, {}, { id: 1, name: "Tobias Funke" }.to_json] }
15
+ stub.get("/projects/1/comments") { [200, {}, [{ id: 1, body: "Hello!" }].to_json] }
16
16
  end
17
17
  end
18
18
 
@@ -25,33 +25,37 @@ describe Her::Model::Introspection do
25
25
  describe "#inspect" do
26
26
  it "outputs resource attributes for an existing resource" do
27
27
  @user = Foo::User.find(1)
28
- ["#<Foo::User(users/1) name=\"Tobias Funke\" id=1>", "#<Foo::User(users/1) id=1 name=\"Tobias Funke\">"].should include(@user.inspect)
28
+ expect(["#<Foo::User(users/1) name=\"Tobias Funke\" id=1>", "#<Foo::User(users/1) id=1 name=\"Tobias Funke\">"]).to include(@user.inspect)
29
29
  end
30
30
 
31
31
  it "outputs resource attributes for an not-saved-yet resource" do
32
- @user = Foo::User.new(:name => "Tobias Funke")
33
- @user.inspect.should == "#<Foo::User(users) name=\"Tobias Funke\">"
32
+ @user = Foo::User.new(name: "Tobias Funke")
33
+ expect(@user.inspect).to eq("#<Foo::User(users) name=\"Tobias Funke\">")
34
34
  end
35
35
 
36
36
  it "outputs resource attributes using getters" do
37
- @user = Foo::User.new(:name => "Tobias Funke", :password => "Funke")
38
- @user.instance_eval {def password; 'filtered'; end}
39
- @user.inspect.should include("name=\"Tobias Funke\"")
40
- @user.inspect.should include("password=\"filtered\"")
41
- @user.inspect.should_not include("password=\"Funke\"")
37
+ @user = Foo::User.new(name: "Tobias Funke", password: "Funke")
38
+ @user.instance_eval do
39
+ def password
40
+ "filtered"
41
+ end
42
+ end
43
+ expect(@user.inspect).to include("name=\"Tobias Funke\"")
44
+ expect(@user.inspect).to include("password=\"filtered\"")
45
+ expect(@user.inspect).not_to include("password=\"Funke\"")
42
46
  end
43
47
 
44
48
  it "support dash on attribute" do
45
49
  @user = Foo::User.new(:'life-span' => "3 years")
46
- @user.inspect.should include("life-span=\"3 years\"")
50
+ expect(@user.inspect).to include("life-span=\"3 years\"")
47
51
  end
48
52
  end
49
53
 
50
54
  describe "#inspect with errors in resource path" do
51
55
  it "prints the resource path as “unknown”" do
52
- @comment = Foo::Comment.where(:project_id => 1).first
53
- path = '<unknown path, missing `project_id`>'
54
- ["#<Foo::Comment(#{path}) body=\"Hello!\" id=1>", "#<Foo::Comment(#{path}) id=1 body=\"Hello!\">"].should include(@comment.inspect)
56
+ @comment = Foo::Comment.where(project_id: 1).first
57
+ path = "<unknown path, missing `project_id`>"
58
+ expect(["#<Foo::Comment(#{path}) body=\"Hello!\" id=1>", "#<Foo::Comment(#{path}) id=1 body=\"Hello!\">"]).to include(@comment.inspect)
55
59
  end
56
60
  end
57
61
  end
@@ -66,10 +70,10 @@ describe Her::Model::Introspection do
66
70
  end
67
71
 
68
72
  it "returns a sibling class, if found" do
69
- Foo::User.her_nearby_class("AccessRecord").should == Foo::AccessRecord
70
- AccessRecord.her_nearby_class("Log").should == Log
71
- Foo::User.her_nearby_class("Log").should == Log
72
- Foo::User.her_nearby_class("X").should be_nil
73
+ expect(Foo::User.her_nearby_class("AccessRecord")).to eq(Foo::AccessRecord)
74
+ expect(AccessRecord.her_nearby_class("Log")).to eq(Log)
75
+ expect(Foo::User.her_nearby_class("Log")).to eq(Log)
76
+ expect{Foo::User.her_nearby_class("X")}.to raise_error(NameError)
73
77
  end
74
78
  end
75
79
  end