her 0.2 → 0.2.1

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.
@@ -116,8 +116,7 @@ describe Her::Model::Hooks do
116
116
  FakeWeb.register_uri(:put, "https://api.example.com/users/1", :body => { :id => 1, :name => "Tobias Fünke" }.to_json)
117
117
  FakeWeb.register_uri(:delete, "https://api.example.com/users/1", :body => { :id => 1, :name => "Tobias Fünke" }.to_json)
118
118
 
119
- spawn_model :User
120
- class User
119
+ spawn_model :User do
121
120
  attr_accessor :internal_save_id, :internal_create_id, :internal_update_id, :internal_destroy_id
122
121
  attr_accessor :internal_after_save_id, :internal_after_create_id, :internal_after_update_id, :internal_after_destroy_id
123
122
 
@@ -12,12 +12,12 @@ describe Her::Model::Introspection do
12
12
  describe "#inspect" do
13
13
  it "outputs resource attributs for an existing resource" do # {{{
14
14
  @user = User.find(1)
15
- @user.inspect.should == "#<User(users/1) id=1 name=\"Tobias Fünke\">"
15
+ @user.inspect.should == "#<User(/users/1) id=1 name=\"Tobias Fünke\">"
16
16
  end # }}}
17
17
 
18
18
  it "outputs resource attributs for an not-saved-yet resource" do # {{{
19
19
  @user = User.new(:name => "Tobias Fünke")
20
- @user.inspect.should == "#<User(users) name=\"Tobias Fünke\">"
20
+ @user.inspect.should == "#<User(/users) name=\"Tobias Fünke\">"
21
21
  end # }}}
22
22
  end
23
23
  end
@@ -4,13 +4,14 @@ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
4
4
  describe Her::Model::ORM do
5
5
  context "mapping data to Ruby objects" do
6
6
  before do # {{{
7
- @api = Her::API.new
8
- @api.setup :base_uri => "https://api.example.com"
7
+ api = Her::API.new
8
+ api.setup :base_uri => "https://api.example.com"
9
9
  FakeWeb.register_uri(:get, "https://api.example.com/users/1", :body => { :id => 1, :name => "Tobias Fünke" }.to_json)
10
10
  FakeWeb.register_uri(:get, "https://api.example.com/users", :body => [{ :id => 1, :name => "Tobias Fünke" }, { :id => 2, :name => "Lindsay Fünke" }].to_json)
11
11
 
12
- spawn_model :User
13
- User.uses_api @api
12
+ spawn_model :User do
13
+ uses_api api
14
+ end
14
15
  end # }}}
15
16
 
16
17
  it "maps a single resource to a Ruby object" do # {{{
@@ -0,0 +1,131 @@
1
+ # encoding: utf-8
2
+ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
3
+
4
+ describe Her::Model::Paths do
5
+ context "building request paths" do
6
+ before do # {{{
7
+ spawn_model :User
8
+ end # }}}
9
+
10
+ describe "#build_request_path" do
11
+ it "builds paths with defaults" do # {{{
12
+ User.build_request_path(id: "foo").should == "/users/foo"
13
+ User.build_request_path.should == "/users"
14
+ end # }}}
15
+
16
+ it "builds paths with custom collection path" do # {{{
17
+ User.collection_path "/utilisateurs"
18
+ User.build_request_path(id: "foo").should == "/utilisateurs/foo"
19
+ User.build_request_path.should == "/utilisateurs"
20
+ end # }}}
21
+
22
+ it "builds paths with custom collection path with multiple variables" do # {{{
23
+ User.collection_path "/organizations/:organization_id/utilisateurs"
24
+ User.build_request_path(:id => "foo", :_organization_id => "acme").should == "/organizations/acme/utilisateurs/foo"
25
+ User.build_request_path(:_organization_id => "acme").should == "/organizations/acme/utilisateurs"
26
+ end # }}}
27
+
28
+ it "builds paths with custom item path" do # {{{
29
+ User.resource_path "/utilisateurs/:id"
30
+ User.build_request_path(id: "foo").should == "/utilisateurs/foo"
31
+ User.build_request_path.should == "/users"
32
+ end # }}}
33
+
34
+ it "raises exceptions when building a path without required custom variables" do # {{{
35
+ User.collection_path "/organizations/:organization_id/utilisateurs"
36
+ expect { User.build_request_path(:id => "foo") }.should raise_error(Her::Errors::PathError)
37
+ end # }}}
38
+ end
39
+ end
40
+
41
+ context "making HTTP requests" do
42
+ before do # {{{
43
+ api = Her::API.new
44
+ api.setup :base_uri => "https://api.example.com"
45
+ FakeWeb.register_uri(:get, "https://api.example.com/organizations/2/users", :body => [{ :id => 1, :fullname => "Tobias Fünke", :organization_id => 2 }, { :id => 2, :fullname => "Lindsay Fünke", :organization_id => 2 }].to_json)
46
+ FakeWeb.register_uri(:post, "https://api.example.com/organizations/2/users", :body => { :id => 1, :fullname => "Tobias Fünke", :organization_id => 2 }.to_json)
47
+ FakeWeb.register_uri(:put, "https://api.example.com/organizations/2/users/1", :body => { :id => 1, :fullname => "Lindsay Fünke", :organization_id => 2 }.to_json)
48
+ FakeWeb.register_uri(:get, "https://api.example.com/organizations/2/users/1", :body => { :id => 1, :fullname => "Tobias Fünke", :organization_id => 2, :active => true }.to_json)
49
+ FakeWeb.register_uri(:delete, "https://api.example.com/organizations/2/users/1", :body => { :id => 1, :fullname => "Lindsay Fünke", :organization_id => 2, :active => false }.to_json)
50
+
51
+ spawn_model :User do
52
+ uses_api api
53
+ collection_path "/organizations/:organization_id/users"
54
+ end
55
+ end # }}}
56
+
57
+ describe "fetching a resource" do
58
+ it "maps a single resource to a Ruby object" do # {{{
59
+ @user = User.find(1, :_organization_id => 2)
60
+ @user.id.should == 1
61
+ @user.fullname.should == "Tobias Fünke"
62
+ end # }}}
63
+ end
64
+
65
+ describe "fetching a collection" do
66
+ it "maps a collection of resources to an array of Ruby objects" do # {{{
67
+ @users = User.all(:_organization_id => 2)
68
+ @users.length.should == 2
69
+ @users.first.fullname.should == "Tobias Fünke"
70
+ end # }}}
71
+ end
72
+
73
+ describe "handling new resource" do
74
+ it "handles new resource" do # {{{
75
+ @new_user = User.new(:fullname => "Tobias Fünke", :organization_id => 2)
76
+ @new_user.new?.should be_true
77
+
78
+ @existing_user = User.find(1, :_organization_id => 2)
79
+ @existing_user.new?.should be_false
80
+ end # }}}
81
+ end
82
+
83
+ describe "creating resources" do
84
+ it "handle one-line resource creation" do # {{{
85
+ @user = User.create(:fullname => "Tobias Fünke", :organization_id => 2)
86
+ @user.id.should == 1
87
+ @user.fullname.should == "Tobias Fünke"
88
+ end # }}}
89
+
90
+ it "handle resource creation through Model.new + #save" do # {{{
91
+ @user = User.new(:fullname => "Tobias Fünke", :organization_id => 2)
92
+ @user.save
93
+ @user.fullname.should == "Tobias Fünke"
94
+ end # }}}
95
+ end
96
+
97
+ context "updating resources" do
98
+ it "handle resource data update without saving it" do # {{{
99
+ @user = User.find(1, :_organization_id => 2)
100
+ @user.fullname.should == "Tobias Fünke"
101
+ @user.fullname = "Kittie Sanchez"
102
+ @user.fullname.should == "Kittie Sanchez"
103
+ end # }}}
104
+
105
+ it "handle resource update through the .update class method" do # {{{
106
+ @user = User.save_existing(1, { :fullname => "Lindsay Fünke", :organization_id => 2 })
107
+ @user.fullname.should == "Lindsay Fünke"
108
+ end # }}}
109
+
110
+ it "handle resource update through #save on an existing resource" do # {{{
111
+ @user = User.find(1, :_organization_id => 2)
112
+ @user.fullname = "Lindsay Fünke"
113
+ @user.save
114
+ @user.fullname.should == "Lindsay Fünke"
115
+ end # }}}
116
+ end
117
+
118
+ context "deleting resources" do
119
+ it "handle resource deletion through the .destroy class method" do # {{{
120
+ @user = User.destroy_existing(1, :_organization_id => 2)
121
+ @user.active.should be_false
122
+ end # }}}
123
+
124
+ it "handle resource deletion through #destroy on an existing resource" do # {{{
125
+ @user = User.find(1, :_organization_id => 2)
126
+ @user.destroy
127
+ @user.active.should be_false
128
+ end # }}}
129
+ end
130
+ end
131
+ end
@@ -50,14 +50,16 @@ describe Her::Model::Relationships do
50
50
  FakeWeb.register_uri(:get, "https://api.example.com/users/2/role", :body => { :id => 2, :body => "User" }.to_json)
51
51
  FakeWeb.register_uri(:get, "https://api.example.com/organizations/1", :body => { :id => 1, :name => "Bluth Company" }.to_json)
52
52
 
53
- spawn_model :User
53
+ spawn_model :User do
54
+ has_many :comments
55
+ has_one :role
56
+ belongs_to :organization
57
+ end
58
+
54
59
  spawn_model :Organization
55
60
  spawn_model :Comment
56
61
  spawn_model :Role
57
62
 
58
- User.has_many :comments
59
- User.has_one :role
60
- User.belongs_to :organization
61
63
 
62
64
  @user_with_included_data = User.find(1)
63
65
  @user_without_included_data = User.find(2)
data/spec/spec_helper.rb CHANGED
@@ -19,7 +19,8 @@ class Array
19
19
  def to_json; MultiJson.dump(self); end
20
20
  end
21
21
 
22
- def spawn_model(klass, attrs={})
22
+ def spawn_model(klass, attrs={}, &block)
23
23
  Object.instance_eval { remove_const klass } if Object.const_defined?(klass)
24
- eval "class #{klass}; include Her::Model; end"
24
+ Object.const_set(klass, Class.new).send(:include, Her::Model)
25
+ Object.const_get(klass).class_eval(&block) if block_given?
25
26
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: her
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,56 +9,56 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-27 00:00:00.000000000Z
12
+ date: 2012-04-30 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ! '>='
19
+ - - '='
20
20
  - !ruby/object:Gem::Version
21
- version: '0'
21
+ version: 0.9.2.2
22
22
  type: :development
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - ! '>='
27
+ - - '='
28
28
  - !ruby/object:Gem::Version
29
- version: '0'
29
+ version: 0.9.2.2
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: rspec
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
- - - ! '>='
35
+ - - '='
36
36
  - !ruby/object:Gem::Version
37
- version: '0'
37
+ version: 2.9.0
38
38
  type: :development
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - ! '>='
43
+ - - '='
44
44
  - !ruby/object:Gem::Version
45
- version: '0'
45
+ version: 2.9.0
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: yard
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
51
- - - ! '>='
51
+ - - '='
52
52
  - !ruby/object:Gem::Version
53
- version: '0'
53
+ version: 0.7.5
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  none: false
58
58
  requirements:
59
- - - ! '>='
59
+ - - '='
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: 0.7.5
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: redcarpet
64
64
  requirement: !ruby/object:Gem::Requirement
@@ -80,82 +80,146 @@ dependencies:
80
80
  requirement: !ruby/object:Gem::Requirement
81
81
  none: false
82
82
  requirements:
83
- - - ! '>='
83
+ - - '='
84
84
  - !ruby/object:Gem::Version
85
- version: '0'
85
+ version: 0.11.3
86
86
  type: :development
87
87
  prerelease: false
88
88
  version_requirements: !ruby/object:Gem::Requirement
89
89
  none: false
90
90
  requirements:
91
- - - ! '>='
91
+ - - '='
92
92
  - !ruby/object:Gem::Version
93
- version: '0'
93
+ version: 0.11.3
94
94
  - !ruby/object:Gem::Dependency
95
95
  name: fakeweb
96
96
  requirement: !ruby/object:Gem::Requirement
97
97
  none: false
98
98
  requirements:
99
- - - ! '>='
99
+ - - '='
100
+ - !ruby/object:Gem::Version
101
+ version: 1.3.0
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - '='
108
+ - !ruby/object:Gem::Version
109
+ version: 1.3.0
110
+ - !ruby/object:Gem::Dependency
111
+ name: guard
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - '='
116
+ - !ruby/object:Gem::Version
117
+ version: 1.0.1
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - '='
124
+ - !ruby/object:Gem::Version
125
+ version: 1.0.1
126
+ - !ruby/object:Gem::Dependency
127
+ name: guard-rspec
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - '='
132
+ - !ruby/object:Gem::Version
133
+ version: 0.7.0
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - '='
140
+ - !ruby/object:Gem::Version
141
+ version: 0.7.0
142
+ - !ruby/object:Gem::Dependency
143
+ name: rb-fsevent
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - '='
148
+ - !ruby/object:Gem::Version
149
+ version: 0.9.1
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - '='
100
156
  - !ruby/object:Gem::Version
101
- version: '0'
157
+ version: 0.9.1
158
+ - !ruby/object:Gem::Dependency
159
+ name: growl
160
+ requirement: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - '='
164
+ - !ruby/object:Gem::Version
165
+ version: 1.0.3
102
166
  type: :development
103
167
  prerelease: false
104
168
  version_requirements: !ruby/object:Gem::Requirement
105
169
  none: false
106
170
  requirements:
107
- - - ! '>='
171
+ - - '='
108
172
  - !ruby/object:Gem::Version
109
- version: '0'
173
+ version: 1.0.3
110
174
  - !ruby/object:Gem::Dependency
111
175
  name: activesupport
112
176
  requirement: !ruby/object:Gem::Requirement
113
177
  none: false
114
178
  requirements:
115
- - - ! '>='
179
+ - - '='
116
180
  - !ruby/object:Gem::Version
117
- version: '0'
181
+ version: 3.2.3
118
182
  type: :runtime
119
183
  prerelease: false
120
184
  version_requirements: !ruby/object:Gem::Requirement
121
185
  none: false
122
186
  requirements:
123
- - - ! '>='
187
+ - - '='
124
188
  - !ruby/object:Gem::Version
125
- version: '0'
189
+ version: 3.2.3
126
190
  - !ruby/object:Gem::Dependency
127
191
  name: faraday
128
192
  requirement: !ruby/object:Gem::Requirement
129
193
  none: false
130
194
  requirements:
131
- - - ! '>='
195
+ - - '='
132
196
  - !ruby/object:Gem::Version
133
- version: '0'
197
+ version: 0.8.0
134
198
  type: :runtime
135
199
  prerelease: false
136
200
  version_requirements: !ruby/object:Gem::Requirement
137
201
  none: false
138
202
  requirements:
139
- - - ! '>='
203
+ - - '='
140
204
  - !ruby/object:Gem::Version
141
- version: '0'
205
+ version: 0.8.0
142
206
  - !ruby/object:Gem::Dependency
143
207
  name: multi_json
144
208
  requirement: !ruby/object:Gem::Requirement
145
209
  none: false
146
210
  requirements:
147
- - - ! '>='
211
+ - - '='
148
212
  - !ruby/object:Gem::Version
149
- version: '0'
213
+ version: 1.3.4
150
214
  type: :runtime
151
215
  prerelease: false
152
216
  version_requirements: !ruby/object:Gem::Requirement
153
217
  none: false
154
218
  requirements:
155
- - - ! '>='
219
+ - - '='
156
220
  - !ruby/object:Gem::Version
157
- version: '0'
158
- description: Her is an ORM that maps REST resources to Ruby objects
221
+ version: 1.3.4
222
+ description: Her is an ORM that maps REST resources and collections to Ruby objects
159
223
  email:
160
224
  - remi@exomel.com
161
225
  executables: []
@@ -165,12 +229,22 @@ files:
165
229
  - .gitignore
166
230
  - .travis.yml
167
231
  - Gemfile
232
+ - Guardfile
168
233
  - LICENSE
169
234
  - README.md
170
235
  - Rakefile
236
+ - examples/twitter-oauth/Gemfile
237
+ - examples/twitter-oauth/app.rb
238
+ - examples/twitter-oauth/config.ru
239
+ - examples/twitter-oauth/views/index.haml
240
+ - examples/twitter-search/Gemfile
241
+ - examples/twitter-search/app.rb
242
+ - examples/twitter-search/config.ru
243
+ - examples/twitter-search/views/index.haml
171
244
  - her.gemspec
172
245
  - lib/her.rb
173
246
  - lib/her/api.rb
247
+ - lib/her/errors.rb
174
248
  - lib/her/middleware.rb
175
249
  - lib/her/middleware/first_level_parse_json.rb
176
250
  - lib/her/middleware/second_level_parse_json.rb
@@ -180,6 +254,7 @@ files:
180
254
  - lib/her/model/http.rb
181
255
  - lib/her/model/introspection.rb
182
256
  - lib/her/model/orm.rb
257
+ - lib/her/model/paths.rb
183
258
  - lib/her/model/relationships.rb
184
259
  - lib/her/version.rb
185
260
  - spec/api_spec.rb
@@ -189,9 +264,10 @@ files:
189
264
  - spec/model/http_spec.rb
190
265
  - spec/model/introspection_spec.rb
191
266
  - spec/model/orm_spec.rb
267
+ - spec/model/paths_spec.rb
192
268
  - spec/model/relationships_spec.rb
193
269
  - spec/spec_helper.rb
194
- homepage: https://github.com/remiprev/her
270
+ homepage: http://remiprev.github.com/her
195
271
  licenses: []
196
272
  post_install_message:
197
273
  rdoc_options: []
@@ -205,7 +281,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
205
281
  version: '0'
206
282
  segments:
207
283
  - 0
208
- hash: -2358268706124465287
284
+ hash: 453966821948784652
209
285
  required_rubygems_version: !ruby/object:Gem::Requirement
210
286
  none: false
211
287
  requirements:
@@ -214,7 +290,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
214
290
  version: '0'
215
291
  segments:
216
292
  - 0
217
- hash: -2358268706124465287
293
+ hash: 453966821948784652
218
294
  requirements: []
219
295
  rubyforge_project:
220
296
  rubygems_version: 1.8.18
@@ -230,6 +306,7 @@ test_files:
230
306
  - spec/model/http_spec.rb
231
307
  - spec/model/introspection_spec.rb
232
308
  - spec/model/orm_spec.rb
309
+ - spec/model/paths_spec.rb
233
310
  - spec/model/relationships_spec.rb
234
311
  - spec/spec_helper.rb
235
312
  has_rdoc: