rack-core-data 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,33 +1,39 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rack-core-data (0.1.0)
4
+ rack-core-data (0.2.0)
5
5
  activesupport (~> 3.2.6)
6
6
  nokogiri (~> 1.4)
7
7
  rack (~> 1.4)
8
+ rack-contrib (~> 1.1.0)
8
9
  sequel (~> 3.37.0)
9
10
  sinatra (~> 1.3.2)
11
+ sinatra-param (~> 0.1.1)
10
12
 
11
13
  GEM
12
14
  remote: http://rubygems.org/
13
15
  specs:
14
- activesupport (3.2.8)
16
+ activesupport (3.2.12)
15
17
  i18n (~> 0.6)
16
18
  multi_json (~> 1.0)
17
19
  i18n (0.6.1)
18
- multi_json (1.3.7)
19
- nokogiri (1.5.5)
20
- rack (1.4.1)
21
- rack-protection (1.2.0)
20
+ multi_json (1.6.1)
21
+ nokogiri (1.5.6)
22
+ rack (1.5.2)
23
+ rack-contrib (1.1.0)
24
+ rack (>= 0.9.1)
25
+ rack-protection (1.4.0)
22
26
  rack
23
27
  rake (0.9.2.2)
24
28
  rspec (0.6.4)
25
29
  sequel (3.37.0)
26
- sinatra (1.3.3)
27
- rack (~> 1.3, >= 1.3.6)
28
- rack-protection (~> 1.2)
30
+ sinatra (1.3.5)
31
+ rack (~> 1.4)
32
+ rack-protection (~> 1.3)
29
33
  tilt (~> 1.3, >= 1.3.3)
30
- tilt (1.3.3)
34
+ sinatra-param (0.1.2)
35
+ sinatra (~> 1.3)
36
+ tilt (1.3.4)
31
37
 
32
38
  PLATFORMS
33
39
  ruby
data/README.md CHANGED
@@ -31,10 +31,17 @@ Simply point `Rack::CoreData` at your Core Data model file, and a RESTful webser
31
31
 
32
32
  ## Usage
33
33
 
34
+ > The easiest way to use Rack::CoreData is to deploy to Heroku using the [Core Data Buildpack](https://github.com/mattt/heroku-buildpack-core-data). Just `git push` your `.xcdatamodel` file to Heroku, and you'll have a REST webservice up and running in a matter of seconds.
35
+
34
36
  ### Gemfile
35
37
 
36
38
  ```Ruby
37
- $ gem 'rack-core-data', :require => 'rack/core-data'
39
+ source :rubygems
40
+
41
+ gem 'rack-core-data', :require => 'rack/core-data'
42
+
43
+ gem 'thin'
44
+ gem 'pg'
38
45
  ```
39
46
 
40
47
  ### config.ru
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  module CoreData
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
@@ -1,5 +1,7 @@
1
1
  require 'rack'
2
+ require 'rack/contrib'
2
3
  require 'sinatra/base'
4
+ require 'sinatra/param'
3
5
 
4
6
  require 'sequel'
5
7
  require 'active_support/inflector'
@@ -12,17 +14,38 @@ end
12
14
 
13
15
  module Rack
14
16
  def self.CoreData(xcdatamodel)
17
+ model = CoreData::DataModel.new(xcdatamodel)
18
+
19
+ # Create each model class before implementing, in order to correctly set up relationships
20
+ model.entities.each do |entity|
21
+ klass = Rack::CoreData::Models.const_set(entity.name.capitalize, Class.new(Sequel::Model))
22
+ end
23
+
15
24
  app = Class.new(Sinatra::Base) do
25
+ use Rack::PostBodyContentTypeParser
26
+
16
27
  before do
17
28
  content_type :json
18
29
  end
19
- end
20
30
 
21
- model = CoreData::DataModel.new(xcdatamodel)
31
+ helpers Sinatra::Param
22
32
 
23
- # Create each model class before implementing, in order to correctly set up relationships
24
- model.entities.each do |entity|
25
- klass = Rack::CoreData::Models.const_set(entity.name.capitalize, Class.new(Sequel::Model))
33
+ options '/' do
34
+ links = []
35
+ model.entities.each do |entity|
36
+ links << %{</#{entity.name.downcase.pluralize}>; rel="resource"}
37
+ end
38
+
39
+ response['Link'] = links.join("\n")
40
+
41
+ model.entities.collect{ |entity|
42
+ {
43
+ name: entity.name,
44
+ url: "/#{entity.name.downcase.pluralize}",
45
+ columns: entity.attributes.collect(&:name)
46
+ }
47
+ }.to_json
48
+ end
26
49
  end
27
50
 
28
51
  model.entities.each do |entity|
@@ -33,12 +56,12 @@ module Rack
33
56
  self.strict_param_setting = false
34
57
  self.raise_on_save_failure = false
35
58
 
36
- plugin :json_serializer, :naked => true, :include => :url, :except => :id
59
+ plugin :json_serializer, naked: true, include: [:url]
37
60
  plugin :schema
38
61
  plugin :validation_helpers
39
62
 
40
63
  def url
41
- "/#{self.class.table_name}/#{id}"
64
+ "/#{self.class.table_name}/#{self[primary_key]}"
42
65
  end
43
66
 
44
67
  entity.relationships.each do |relationship|
@@ -76,7 +99,7 @@ module Rack
76
99
  else :varchar
77
100
  end
78
101
 
79
- column attribute.name, type, options
102
+ column attribute.name.to_sym, type, options
80
103
  end
81
104
 
82
105
  entity.relationships.each do |relationship|
@@ -104,7 +127,6 @@ module Rack
104
127
  when "String"
105
128
  validates_min_length attribute.minimum_value, attribute.name if attribute.minimum_value
106
129
  validates_max_length attribute.maximum_value, attribute.name if attribute.maximum_value
107
- validates_min_length attribute.minimum_value, attribute.name if attribute.minimum_value
108
130
  end
109
131
  end
110
132
  end
@@ -115,37 +137,54 @@ module Rack
115
137
 
116
138
  disable :raise_errors, :show_exceptions
117
139
 
118
- get "/#{entity.name.downcase.pluralize}/?" do
119
- klass.all.to_json
140
+ get "/#{klass.table_name}/?" do
141
+ if params[:page] or params[:per_page]
142
+ param :page, Integer, default: 1, min: 1
143
+ param :per_page, Integer, default: 100, in: (1..100)
144
+
145
+ {
146
+ "#{klass.table_name}" => klass.limit(params[:per_page], (params[:page] - 1) * params[:per_page]),
147
+ page: params[:page],
148
+ total: klass.count
149
+ }.to_json
150
+ else
151
+ param :limit, Integer, default: 100, in: (1..100)
152
+ param :offset, Integer, default: 0, min: 0
153
+
154
+ {
155
+ "#{klass.table_name}" => klass.limit(params[:limit], params[:offset])
156
+ }.to_json
157
+ end
120
158
  end
121
159
 
122
- post "/#{entity.name.downcase.pluralize}/?" do
160
+ post "/#{klass.table_name}/?" do
123
161
  record = klass.new(params)
124
162
  if record.save
125
163
  status 201
126
- record.to_json
164
+ {entity.name.downcase => record}.to_json
127
165
  else
128
166
  status 406
129
167
  {errors: record.errors}.to_json
130
168
  end
131
169
  end
132
170
 
133
- get "/#{entity.name.downcase.pluralize}/:id/?" do
134
- klass[params[:id]].to_json
171
+ get "/#{klass.table_name}/:id/?" do
172
+ record = klass[params[:id]] or halt 404
173
+ {entity.name.downcase => record}.to_json
135
174
  end
136
175
 
137
- put "/#{entity.name.downcase.pluralize}/:id/?" do
176
+ put "/#{klass.table_name}/:id/?" do
138
177
  record = klass[params[:id]] or halt 404
139
178
  if record.update(params)
140
179
  status 200
141
- record.to_json
180
+ {entity.name.downcase => record}.to_json
142
181
  else
143
182
  status 406
144
183
  {errors: record.errors}.to_json
145
184
  end
146
185
  end
147
186
 
148
- delete "/#{entity.name.downcase.pluralize}/:id/?" do
187
+ delete "/#{klass.table_name}/:id/?" do
149
188
  record = klass[params[:id]] or halt 404
150
189
  if record.destroy
151
190
  status 200
@@ -158,8 +197,8 @@ module Rack
158
197
  entity.relationships.each do |relationship|
159
198
  next unless relationship.to_many?
160
199
 
161
- get "/#{entity.name.downcase.pluralize}/:id/#{relationship.name}/?" do
162
- klass[params[:id]].send(relationship.name).to_json
200
+ get "/#{klass.table_name}/:id/#{relationship.name}/?" do
201
+ {relationship.name => klass[params[:id]].send(relationship.name)}.to_json
163
202
  end
164
203
  end
165
204
  end
@@ -16,8 +16,10 @@ Gem::Specification.new do |s|
16
16
  s.add_development_dependency "rake", "~> 0.9.2"
17
17
 
18
18
  s.add_dependency "rack", "~> 1.4"
19
+ s.add_dependency "rack-contrib", "~> 1.1.0"
19
20
  s.add_dependency "nokogiri", "~> 1.4"
20
21
  s.add_dependency "sinatra", "~> 1.3.2"
22
+ s.add_dependency "sinatra-param", "~> 0.1.1"
21
23
  s.add_dependency "sequel", "~> 3.37.0"
22
24
  s.add_dependency "activesupport", "~> 3.2.6"
23
25
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-core-data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-09 00:00:00.000000000 Z
12
+ date: 2013-03-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: !ruby/object:Gem::Requirement
16
+ requirement: &70164102486760 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,15 +21,10 @@ dependencies:
21
21
  version: 0.6.1
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ~>
28
- - !ruby/object:Gem::Version
29
- version: 0.6.1
24
+ version_requirements: *70164102486760
30
25
  - !ruby/object:Gem::Dependency
31
26
  name: rake
32
- requirement: !ruby/object:Gem::Requirement
27
+ requirement: &70164102486160 !ruby/object:Gem::Requirement
33
28
  none: false
34
29
  requirements:
35
30
  - - ~>
@@ -37,15 +32,10 @@ dependencies:
37
32
  version: 0.9.2
38
33
  type: :development
39
34
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ~>
44
- - !ruby/object:Gem::Version
45
- version: 0.9.2
35
+ version_requirements: *70164102486160
46
36
  - !ruby/object:Gem::Dependency
47
37
  name: rack
48
- requirement: !ruby/object:Gem::Requirement
38
+ requirement: &70164102485660 !ruby/object:Gem::Requirement
49
39
  none: false
50
40
  requirements:
51
41
  - - ~>
@@ -53,15 +43,21 @@ dependencies:
53
43
  version: '1.4'
54
44
  type: :runtime
55
45
  prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
46
+ version_requirements: *70164102485660
47
+ - !ruby/object:Gem::Dependency
48
+ name: rack-contrib
49
+ requirement: &70164102485160 !ruby/object:Gem::Requirement
57
50
  none: false
58
51
  requirements:
59
52
  - - ~>
60
53
  - !ruby/object:Gem::Version
61
- version: '1.4'
54
+ version: 1.1.0
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70164102485160
62
58
  - !ruby/object:Gem::Dependency
63
59
  name: nokogiri
64
- requirement: !ruby/object:Gem::Requirement
60
+ requirement: &70164102484640 !ruby/object:Gem::Requirement
65
61
  none: false
66
62
  requirements:
67
63
  - - ~>
@@ -69,15 +65,10 @@ dependencies:
69
65
  version: '1.4'
70
66
  type: :runtime
71
67
  prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ~>
76
- - !ruby/object:Gem::Version
77
- version: '1.4'
68
+ version_requirements: *70164102484640
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: sinatra
80
- requirement: !ruby/object:Gem::Requirement
71
+ requirement: &70164102484160 !ruby/object:Gem::Requirement
81
72
  none: false
82
73
  requirements:
83
74
  - - ~>
@@ -85,15 +76,21 @@ dependencies:
85
76
  version: 1.3.2
86
77
  type: :runtime
87
78
  prerelease: false
88
- version_requirements: !ruby/object:Gem::Requirement
79
+ version_requirements: *70164102484160
80
+ - !ruby/object:Gem::Dependency
81
+ name: sinatra-param
82
+ requirement: &70164102483660 !ruby/object:Gem::Requirement
89
83
  none: false
90
84
  requirements:
91
85
  - - ~>
92
86
  - !ruby/object:Gem::Version
93
- version: 1.3.2
87
+ version: 0.1.1
88
+ type: :runtime
89
+ prerelease: false
90
+ version_requirements: *70164102483660
94
91
  - !ruby/object:Gem::Dependency
95
92
  name: sequel
96
- requirement: !ruby/object:Gem::Requirement
93
+ requirement: &70164102483100 !ruby/object:Gem::Requirement
97
94
  none: false
98
95
  requirements:
99
96
  - - ~>
@@ -101,15 +98,10 @@ dependencies:
101
98
  version: 3.37.0
102
99
  type: :runtime
103
100
  prerelease: false
104
- version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
- requirements:
107
- - - ~>
108
- - !ruby/object:Gem::Version
109
- version: 3.37.0
101
+ version_requirements: *70164102483100
110
102
  - !ruby/object:Gem::Dependency
111
103
  name: activesupport
112
- requirement: !ruby/object:Gem::Requirement
104
+ requirement: &70164102482580 !ruby/object:Gem::Requirement
113
105
  none: false
114
106
  requirements:
115
107
  - - ~>
@@ -117,12 +109,7 @@ dependencies:
117
109
  version: 3.2.6
118
110
  type: :runtime
119
111
  prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
- requirements:
123
- - - ~>
124
- - !ruby/object:Gem::Version
125
- version: 3.2.6
112
+ version_requirements: *70164102482580
126
113
  description: Automatically generate REST APIs for Core Data models.
127
114
  email: m@mattt.me
128
115
  executables: []
@@ -138,7 +125,6 @@ files:
138
125
  - ./lib/rack/core-data/version.rb
139
126
  - ./lib/rack/core-data.rb
140
127
  - ./LICENSE
141
- - ./rack-core-data-0.0.3.gem
142
128
  - ./rack-core-data.gemspec
143
129
  - ./Rakefile
144
130
  - ./README.md
@@ -156,7 +142,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
156
142
  version: '0'
157
143
  segments:
158
144
  - 0
159
- hash: -107443950940570454
145
+ hash: 454791044675933494
160
146
  required_rubygems_version: !ruby/object:Gem::Requirement
161
147
  none: false
162
148
  requirements:
@@ -165,10 +151,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
165
151
  version: '0'
166
152
  segments:
167
153
  - 0
168
- hash: -107443950940570454
154
+ hash: 454791044675933494
169
155
  requirements: []
170
156
  rubyforge_project:
171
- rubygems_version: 1.8.24
157
+ rubygems_version: 1.8.15
172
158
  signing_key:
173
159
  specification_version: 3
174
160
  summary: Rack::CoreData
Binary file