storage_room 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/.gitignore +22 -0
  2. data/History.txt +3 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +51 -0
  5. data/Rakefile +50 -0
  6. data/TODO +9 -0
  7. data/VERSION +1 -0
  8. data/examples/authentication.rb +6 -0
  9. data/examples/create_resource.rb +24 -0
  10. data/examples/destroy_resource.rb +11 -0
  11. data/examples/get_collections.rb +11 -0
  12. data/examples/guidebooks.csv +21 -0
  13. data/examples/import_csv.rb +21 -0
  14. data/examples/search_resources.rb +13 -0
  15. data/examples/update_resource.rb +15 -0
  16. data/lib/storage_room/array.rb +64 -0
  17. data/lib/storage_room/attributes.rb +37 -0
  18. data/lib/storage_room/base.rb +56 -0
  19. data/lib/storage_room/embedded.rb +6 -0
  20. data/lib/storage_room/embeddeds/file.rb +5 -0
  21. data/lib/storage_room/embeddeds/location.rb +5 -0
  22. data/lib/storage_room/field.rb +7 -0
  23. data/lib/storage_room/model.rb +111 -0
  24. data/lib/storage_room/models/collection.rb +31 -0
  25. data/lib/storage_room/models/resource.rb +53 -0
  26. data/lib/storage_room.rb +89 -0
  27. data/spec/fixtures/collection.json +38 -0
  28. data/spec/fixtures/collections.json +42 -0
  29. data/spec/fixtures/validation_error.json +7 -0
  30. data/spec/spec_helper.rb +34 -0
  31. data/spec/storage_room/array_spec.rb +77 -0
  32. data/spec/storage_room/attributes_spec.rb +82 -0
  33. data/spec/storage_room/base_spec.rb +110 -0
  34. data/spec/storage_room/embedded_spec.rb +5 -0
  35. data/spec/storage_room/embeddeds/file_spec.rb +5 -0
  36. data/spec/storage_room/embeddeds/location_spec.rb +5 -0
  37. data/spec/storage_room/field_spec.rb +5 -0
  38. data/spec/storage_room/model_spec.rb +239 -0
  39. data/spec/storage_room/models/collection_spec.rb +54 -0
  40. data/spec/storage_room/models/resource_spec.rb +68 -0
  41. data/spec/storage_room_spec.rb +99 -0
  42. metadata +188 -0
@@ -0,0 +1,239 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe StorageRoom::Model do
4
+ context "Class" do
5
+ context "Methods" do
6
+ describe "#show_path" do
7
+ it "should raise" do
8
+ lambda {
9
+ StorageRoom::Model.show_path(1)
10
+ }.should raise_error(StorageRoom::AbstractMethod)
11
+ end
12
+ end
13
+
14
+ describe "#index_path" do
15
+ it "should raise" do
16
+ lambda {
17
+ StorageRoom::Model.index_path
18
+ }.should raise_error(StorageRoom::AbstractMethod)
19
+ end
20
+ end
21
+
22
+ describe "#json_name" do
23
+ it "should raise" do
24
+ lambda {
25
+ StorageRoom::Model.json_name
26
+ }.should raise_error(StorageRoom::AbstractMethod)
27
+ end
28
+ end
29
+
30
+ describe "#create" do
31
+ it "should create" do
32
+ object = stub('StorageRoom::Model')
33
+ object.should_receive(:create)
34
+
35
+ StorageRoom::Model.stub(:new).and_return(object)
36
+
37
+ result = StorageRoom::Model.create(:one => 1, :two => 2)
38
+ result.should == object
39
+ end
40
+ end
41
+
42
+ describe "#all" do
43
+ it "should load" do
44
+ stub_request(:get, stub_url('/collections')).to_return(:body => fixture_file('collections.json'), :status => 200)
45
+
46
+ array = StorageRoom::Collection.all
47
+ array[:@type].should == 'Array'
48
+ end
49
+ end
50
+
51
+ describe "#find" do
52
+ it "should load" do
53
+ stub_request(:get, stub_url('/collections/1')).to_return(:body => fixture_file('collection.json'), :status => 200)
54
+
55
+ collection = StorageRoom::Collection.find(1)
56
+ collection[:@type].should == 'Collection'
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ context "Instance" do
63
+ before(:each) do
64
+ @model = StorageRoom::Model.new(:test => 1, :@attr => 2)
65
+ end
66
+
67
+ describe "#initialize" do
68
+ it "should set attributes" do
69
+ @model[:test].should == 1
70
+ @model[:@attr].should == 2
71
+ end
72
+
73
+ it "should set new record" do
74
+ @model.should be_new_record
75
+ end
76
+
77
+ it "should set errors" do
78
+ @model.errors.should == []
79
+ end
80
+ end
81
+
82
+ describe "#set_from_api" do
83
+ before(:each) do
84
+ @model.set_from_api(:test2 => 3)
85
+ end
86
+
87
+ it "should reset attributes" do
88
+ @model[:test].should be_nil
89
+ @model[:@attr].should be_nil
90
+ end
91
+
92
+ it "should set new attributes" do
93
+ @model[:test2].should == 3
94
+ end
95
+
96
+ it "should set new record" do
97
+ @model.should_not be_new_record
98
+ end
99
+ end
100
+
101
+ describe "#reset!" do
102
+ it "should reset" do
103
+ @model.instance_variable_set(:@errors, [1,2,3])
104
+
105
+ @model.reset!
106
+ @model.attributes.should == {}
107
+ @model.should be_new_record
108
+ @model.errors.should == []
109
+ end
110
+ end
111
+
112
+ describe "#valid?" do
113
+ it "should be valid" do
114
+ @model.should be_valid
115
+ end
116
+
117
+ it "should be invalid" do
118
+ @model.errors.push(1)
119
+ @model.should_not be_valid
120
+ end
121
+ end
122
+
123
+ describe "#errors" do
124
+ it "should have errors" do
125
+ @model.errors.should == []
126
+ end
127
+ end
128
+
129
+ describe "#new_record?" do
130
+ it "should be new record" do
131
+ @model.should be_new_record
132
+ @model.instance_variable_set :@new_record, false
133
+ @model.should_not be_new_record
134
+ end
135
+ end
136
+
137
+ describe "#save" do
138
+ before(:each) do
139
+ @model.stub(:create)
140
+ @model.stub(:update)
141
+ end
142
+
143
+ it "should create on new record" do
144
+ @model.should_receive(:create)
145
+ @model.save
146
+ end
147
+
148
+ it "should update on existing record" do
149
+ @model.instance_variable_set(:@new_record, false)
150
+ @model.should_receive(:update)
151
+ @model.save
152
+ end
153
+ end
154
+
155
+ describe "#as_json" do
156
+ it "should return hash without meta data" do
157
+ resource = StorageRoom::Resource.new(:field => 1, :@attr => 2)
158
+ hash = resource.as_json
159
+
160
+ hash['resource'][:field].should == 1
161
+ hash['resource'][:@attr].should_not be_present
162
+ end
163
+ end
164
+
165
+ describe "#reload" do
166
+ it "should load" do
167
+ collection = StorageRoom::Collection.new
168
+ collection[:@url] = '/collections/1'
169
+ stub_request(:get, stub_url('/collections/1')).to_return(:body => fixture_file('collection.json'), :status => 200)
170
+
171
+ collection.reload
172
+ collection[:name].should == 'Guidebooks'
173
+ end
174
+ end
175
+
176
+ describe "#create" do
177
+ it "should create" do
178
+ klass = StorageRoom.class_for_name('Guidebook')
179
+ guidebook = klass.new
180
+
181
+ stub_request(:post, stub_url('/collections/guidebooks/resources')).to_return(:body => fixture_file('collection.json'), :status => 200)
182
+
183
+ guidebook.create
184
+ guidebook[:name].should == 'Guidebooks'
185
+ end
186
+
187
+ it "should have errors on validation error" do
188
+ klass = StorageRoom.class_for_name('Guidebook')
189
+ guidebook = klass.new
190
+
191
+ stub_request(:post, stub_url('/collections/guidebooks/resources')).to_return(:body => fixture_file('validation_error.json'), :status => 422)
192
+
193
+ guidebook.create
194
+ guidebook[:name].should be_nil
195
+
196
+ guidebook.errors.should have(1).items
197
+ end
198
+ end
199
+
200
+ describe "#update" do
201
+ it "should update" do
202
+ collection = StorageRoom::Collection.new
203
+ collection.instance_variable_set(:@new_record, false)
204
+ collection[:@url] = '/collections/1'
205
+
206
+ stub_request(:put, stub_url('/collections/1')).to_return(:body => fixture_file('collection.json'), :status => 200)
207
+
208
+ collection.update
209
+ collection[:name].should == 'Guidebooks'
210
+ end
211
+
212
+ it "should have errors on validation error" do
213
+ collection = StorageRoom::Collection.new
214
+ collection.instance_variable_set(:@new_record, false)
215
+ collection[:@url] = '/collections/1'
216
+
217
+ stub_request(:put, stub_url('/collections/1')).to_return(:body => fixture_file('validation_error.json'), :status => 422)
218
+
219
+ collection.update
220
+ collection[:name].should be_nil
221
+ collection.errors.should have(1).items
222
+ end
223
+ end
224
+
225
+ describe "#destroy" do
226
+ it "should destroy" do
227
+ collection = StorageRoom::Collection.new
228
+ collection.instance_variable_set(:@new_record, false)
229
+ collection[:@url] = '/collections/1'
230
+
231
+ stub_request(:delete, stub_url('/collections/1')).to_return(:status => 200)
232
+
233
+ collection.destroy
234
+ end
235
+ end
236
+
237
+ end
238
+
239
+ end
@@ -0,0 +1,54 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe StorageRoom::Collection do
4
+ context "Class" do
5
+ context "Methods" do
6
+ describe "#show_path" do
7
+ it "should be defined" do
8
+ StorageRoom::Collection.show_path(1).should == '/collections/1'
9
+ end
10
+ end
11
+
12
+ describe "#index_path" do
13
+ it "should be defined" do
14
+ StorageRoom::Collection.index_path.should == '/collections'
15
+ end
16
+ end
17
+
18
+ describe "#resources_path" do
19
+ it "should be defined" do
20
+ StorageRoom::Collection.resources_path(1).should == '/collections/1/resources'
21
+ end
22
+ end
23
+
24
+ describe "#json_name" do
25
+ it "should be defined" do
26
+ StorageRoom::Collection.json_name.should == 'collection'
27
+ end
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ context "Instance" do
34
+ before(:each) do
35
+ @collection = StorageRoom::Collection.new
36
+ end
37
+
38
+ describe "#resources" do
39
+ it "should load" do
40
+ StorageRoom::Array.should_receive(:load)
41
+ @collection.resources
42
+ end
43
+ end
44
+
45
+ describe "#resource_class" do
46
+ it "should return class" do
47
+ @collection[:identifier] = 'guidebook'
48
+ klass = @collection.resource_class
49
+ klass.should == Guidebook
50
+ end
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,68 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe StorageRoom::Resource do
4
+ context "Class" do
5
+ context "Methods" do
6
+ before(:each) do
7
+ StorageRoom.class_for_name('Guidebook')
8
+ end
9
+
10
+ describe "#show_path" do
11
+ it "should be defined" do
12
+ Guidebook.show_path(1).should == '/collections/guidebooks/resources/1'
13
+ end
14
+ end
15
+
16
+ describe "#index_path" do
17
+ it "should be defined" do
18
+ Guidebook.index_path.should == '/collections/guidebooks/resources'
19
+ end
20
+ end
21
+
22
+ describe "#collection_path" do
23
+ it "should be defined" do
24
+ Guidebook.collection_path.should == '/collections/guidebooks'
25
+ end
26
+ end
27
+
28
+ describe "#search_path" do
29
+ it "should be defined" do
30
+ Guidebook.search_path(:test =>1).should == '/collections/guidebooks/resources?test=1'
31
+ end
32
+ end
33
+
34
+ describe "#json_name" do
35
+ it "should be defined" do
36
+ StorageRoom::Resource.json_name.should == 'resource'
37
+ Guidebook.json_name.should == 'resource'
38
+ end
39
+ end
40
+
41
+ describe "#collection_id" do
42
+ it "should be defined" do
43
+ Guidebook.collection_id.should == 'guidebooks'
44
+ end
45
+ end
46
+
47
+ describe "#search" do
48
+ it "should load" do
49
+ pending
50
+ end
51
+ end
52
+ end
53
+
54
+ end
55
+
56
+ context "Instance" do
57
+ before(:each) do
58
+ @resource = StorageRoom::Resource.new
59
+ end
60
+
61
+ describe "#collection" do
62
+ it "should load" do
63
+ StorageRoom::Collection.should_receive(:load)
64
+ @resource.collection
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,99 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe StorageRoom do
4
+ describe "#authenticate" do
5
+ before(:each) do
6
+ StorageRoom.authenticate('account_id', 'api_key')
7
+ end
8
+
9
+ it "should set basic auth" do
10
+ StorageRoom::Base.default_options[:basic_auth].should == {:username => 'api_key', :password => 'X'}
11
+ end
12
+
13
+ it "should set variables" do
14
+ StorageRoom.account_id == 'account_id'
15
+ StorageRoom.api_key == 'api_key'
16
+ end
17
+
18
+ it "should update uri" do
19
+ StorageRoom::Base.base_uri.should include('account_id')
20
+ end
21
+ end
22
+
23
+ describe "#user_agent" do
24
+ before(:each) do
25
+ StorageRoom.user_agent = 'agent'
26
+ end
27
+
28
+ it "should set user agent" do
29
+ StorageRoom::Base.headers['User-Agent'].should == 'agent'
30
+ end
31
+
32
+ it "should set variables" do
33
+ StorageRoom.user_agent.should == 'agent'
34
+ end
35
+ end
36
+
37
+ describe "#server" do
38
+ before(:each) do
39
+ StorageRoom.server = 'server'
40
+ end
41
+
42
+ it "should update uri" do
43
+ StorageRoom::Base.base_uri.should include('server')
44
+ end
45
+
46
+ it "should set variables" do
47
+ StorageRoom.server.should == 'server'
48
+ end
49
+
50
+ it "should return default" do
51
+ StorageRoom.server = nil
52
+ StorageRoom.server.should be_present
53
+ end
54
+ end
55
+
56
+ describe "#ssl" do
57
+ before(:each) do
58
+ StorageRoom.ssl = true
59
+ end
60
+
61
+ it "should update uri" do
62
+ StorageRoom::Base.base_uri.should include('https://')
63
+ end
64
+
65
+ it "should set variables" do
66
+ StorageRoom.ssl.should be_true
67
+ end
68
+ end
69
+
70
+ describe "#http_proxy" do
71
+ before(:each) do
72
+ StorageRoom.http_proxy('http_proxy', 123)
73
+ end
74
+
75
+ it "should set proxy" do
76
+ StorageRoom::Base.default_options[:http_proxyaddr].should == 'http_proxy'
77
+ StorageRoom::Base.default_options[:http_proxyport].should == 123
78
+ end
79
+
80
+ it "should set variables" do
81
+ StorageRoom.proxy_server.should == 'http_proxy'
82
+ StorageRoom.proxy_port.should == 123
83
+ end
84
+ end
85
+
86
+ describe "#class_for_name" do
87
+ it "should get class" do
88
+ klass = StorageRoom.class_for_name('Recipe')
89
+ klass.should be_an_instance_of(Class)
90
+ klass.name.should == 'Recipe'
91
+ end
92
+
93
+ it "should get StorageRoom class" do
94
+ klass = StorageRoom.class_for_name('Resource')
95
+ klass.should be_an_instance_of(Class)
96
+ klass.should == StorageRoom::Resource
97
+ end
98
+ end
99
+ end
metadata ADDED
@@ -0,0 +1,188 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: storage_room
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Sascha Konietzke
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-08 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: webmock
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :development
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: httparty
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 5
60
+ segments:
61
+ - 0
62
+ - 6
63
+ - 1
64
+ version: 0.6.1
65
+ type: :runtime
66
+ version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ name: activesupport
69
+ prerelease: false
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 7
76
+ segments:
77
+ - 3
78
+ - 0
79
+ - 0
80
+ version: 3.0.0
81
+ type: :runtime
82
+ version_requirements: *id004
83
+ description: StorageRoom is a CMS system for Mobile Applications (iPhone, Android, BlackBerry, ...). This library gives you an ActiveModel-like interface to your data.
84
+ email: sascha@thriventures.com
85
+ executables: []
86
+
87
+ extensions: []
88
+
89
+ extra_rdoc_files:
90
+ - LICENSE
91
+ - README.rdoc
92
+ - TODO
93
+ files:
94
+ - .gitignore
95
+ - History.txt
96
+ - LICENSE
97
+ - README.rdoc
98
+ - Rakefile
99
+ - TODO
100
+ - VERSION
101
+ - examples/authentication.rb
102
+ - examples/create_resource.rb
103
+ - examples/destroy_resource.rb
104
+ - examples/get_collections.rb
105
+ - examples/guidebooks.csv
106
+ - examples/import_csv.rb
107
+ - examples/search_resources.rb
108
+ - examples/update_resource.rb
109
+ - lib/storage_room.rb
110
+ - lib/storage_room/array.rb
111
+ - lib/storage_room/attributes.rb
112
+ - lib/storage_room/base.rb
113
+ - lib/storage_room/embedded.rb
114
+ - lib/storage_room/embeddeds/file.rb
115
+ - lib/storage_room/embeddeds/location.rb
116
+ - lib/storage_room/field.rb
117
+ - lib/storage_room/model.rb
118
+ - lib/storage_room/models/collection.rb
119
+ - lib/storage_room/models/resource.rb
120
+ - spec/fixtures/collection.json
121
+ - spec/fixtures/collections.json
122
+ - spec/fixtures/validation_error.json
123
+ - spec/spec_helper.rb
124
+ - spec/storage_room/array_spec.rb
125
+ - spec/storage_room/attributes_spec.rb
126
+ - spec/storage_room/base_spec.rb
127
+ - spec/storage_room/embedded_spec.rb
128
+ - spec/storage_room/embeddeds/file_spec.rb
129
+ - spec/storage_room/embeddeds/location_spec.rb
130
+ - spec/storage_room/field_spec.rb
131
+ - spec/storage_room/model_spec.rb
132
+ - spec/storage_room/models/collection_spec.rb
133
+ - spec/storage_room/models/resource_spec.rb
134
+ - spec/storage_room_spec.rb
135
+ has_rdoc: true
136
+ homepage: http://github.com/thriventures/storage_room_gem
137
+ licenses: []
138
+
139
+ post_install_message:
140
+ rdoc_options:
141
+ - --charset=UTF-8
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ hash: 3
150
+ segments:
151
+ - 0
152
+ version: "0"
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ hash: 3
159
+ segments:
160
+ - 0
161
+ version: "0"
162
+ requirements: []
163
+
164
+ rubyforge_project:
165
+ rubygems_version: 1.3.7
166
+ signing_key:
167
+ specification_version: 3
168
+ summary: StorageRoom API Wrapper (ActiveModel style)
169
+ test_files:
170
+ - spec/spec_helper.rb
171
+ - spec/storage_room/array_spec.rb
172
+ - spec/storage_room/attributes_spec.rb
173
+ - spec/storage_room/base_spec.rb
174
+ - spec/storage_room/embedded_spec.rb
175
+ - spec/storage_room/embeddeds/file_spec.rb
176
+ - spec/storage_room/embeddeds/location_spec.rb
177
+ - spec/storage_room/field_spec.rb
178
+ - spec/storage_room/model_spec.rb
179
+ - spec/storage_room/models/collection_spec.rb
180
+ - spec/storage_room/models/resource_spec.rb
181
+ - spec/storage_room_spec.rb
182
+ - examples/authentication.rb
183
+ - examples/create_resource.rb
184
+ - examples/destroy_resource.rb
185
+ - examples/get_collections.rb
186
+ - examples/import_csv.rb
187
+ - examples/search_resources.rb
188
+ - examples/update_resource.rb