rails-api 0.0.1 → 0.0.2
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.
- data/README.md +17 -33
- data/lib/rails-api/action_controller/api.rb +2 -0
- data/lib/rails-api/application.rb +10 -1
- data/lib/rails-api/generators/rails/app/app_generator.rb +5 -0
- data/lib/rails-api/templates/rails/app/Gemfile +0 -2
- data/lib/rails-api/version.rb +1 -1
- data/test/generators/app_generator_test.rb +7 -4
- metadata +4 -4
data/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# Rails::API
|
|
2
2
|
|
|
3
|
+
[](http://travis-ci.org/spastorino/rails-api)
|
|
4
|
+
|
|
3
5
|
**Rails::API** is a subset of a normal Rails application, created for applications that don't require all functionality that a complete Rails application provides. It is a bit more lightweight, and consequently a bit faster than a normal Rails application. The main example for its usage is in API applications only, where you usually don't need the entire Rails middleware stack nor template generation.
|
|
4
6
|
|
|
5
7
|
## Using Rails for API-only Apps
|
|
@@ -76,9 +78,8 @@ Then generate a new **Rails::API** app:
|
|
|
76
78
|
|
|
77
79
|
rails-api new my_api
|
|
78
80
|
|
|
79
|
-
This will do
|
|
81
|
+
This will do two main things for you:
|
|
80
82
|
|
|
81
|
-
* Make the application inherit from *Rails::ApiApplication* instead of *Rails::Application*. This will configure your application to start with a more limited set of middleware than normal. Specifically, it will not include any middleware primarily useful for browser applications (like cookie support) by default.
|
|
82
83
|
* Make *ApplicationController* inherit from *ActionController::API* instead of *ActionController::Base*. As with middleware, this will leave out any *ActionController* modules that provide functionality primarily used by browser applications.
|
|
83
84
|
* Configure the generators to skip generating views, helpers and assets when you generate a new resource.
|
|
84
85
|
|
|
@@ -92,22 +93,6 @@ Add the gem to your *Gemfile*:
|
|
|
92
93
|
|
|
93
94
|
And run `bundle` to install the gem.
|
|
94
95
|
|
|
95
|
-
In *config/application.rb*, change your *Application* class to inherit from *Rails::ApiApplication*:
|
|
96
|
-
|
|
97
|
-
```ruby
|
|
98
|
-
# instead of
|
|
99
|
-
module MyApi
|
|
100
|
-
class Application < Rails::Application
|
|
101
|
-
end
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
# do
|
|
105
|
-
module MyApi
|
|
106
|
-
class Application < Rails::ApiApplication
|
|
107
|
-
end
|
|
108
|
-
end
|
|
109
|
-
```
|
|
110
|
-
|
|
111
96
|
Change *app/controllers/application_controller.rb*:
|
|
112
97
|
|
|
113
98
|
```ruby
|
|
@@ -124,22 +109,22 @@ And comment out the `protect_from_forgery` call if you are using it.
|
|
|
124
109
|
|
|
125
110
|
### Choosing Middlewares
|
|
126
111
|
|
|
127
|
-
An
|
|
112
|
+
An API application comes with the following middlewares by default.
|
|
128
113
|
|
|
129
|
-
* *Rack::Cache*: Caches responses with public *Cache-Control* headers using HTTP caching semantics.
|
|
130
|
-
* *Rack::Sendfile*: Uses a front-end server's file serving support from your Rails application.
|
|
131
|
-
* *Rack::Lock*: If your application is not marked as threadsafe (`config.threadsafe!`), this middleware will add a mutex around your requests.
|
|
132
|
-
* *ActionDispatch::RequestId*
|
|
133
|
-
* *Rails::Rack::Logger*
|
|
134
|
-
* *Rack::Runtime*: Adds a header to the response listing the total runtime of the request.
|
|
135
|
-
* *ActionDispatch::ShowExceptions*: Rescue exceptions and re-dispatch them to an exception handling application.
|
|
136
114
|
* *ActionDispatch::DebugExceptions*: Log exceptions.
|
|
137
|
-
* *ActionDispatch::RemoteIp*: Protect against IP spoofing attacks.
|
|
138
|
-
* *ActionDispatch::Reloader*: In development mode, support code reloading.
|
|
139
|
-
* *ActionDispatch::ParamsParser*: Parse XML, YAML and JSON parameters when the request's *Content-Type* is one of those.
|
|
140
115
|
* *ActionDispatch::Head*: Dispatch *HEAD* requests as *GET* requests, and return only the status code and headers.
|
|
116
|
+
* *ActionDispatch::ParamsParser*: Parse XML, YAML and JSON parameters when the request's *Content-Type* is one of those.
|
|
117
|
+
* *ActionDispatch::Reloader*: In development mode, support code reloading.
|
|
118
|
+
* *ActionDispatch::RemoteIp*: Protect against IP spoofing attacks.
|
|
119
|
+
* *ActionDispatch::RequestId*: Makes a unique request id available, sending the id to the client via the X-Request-Id header. The unique request id can be used to trace a request end-to-end and would typically end up being part of log files from multiple pieces of the stack.
|
|
120
|
+
* *ActionDispatch::ShowExceptions*: Rescue exceptions and re-dispatch them to an exception handling application.
|
|
121
|
+
* *Rack::Cache*: Caches responses with public *Cache-Control* headers using HTTP caching semantics.
|
|
141
122
|
* *Rack::ConditionalGet*: Supports the `stale?` feature in Rails controllers.
|
|
142
123
|
* *Rack::ETag*: Automatically set an *ETag* on all string responses. This means that if the same response is returned from a controller for the same URL, the server will return a *304 Not Modified*, even if no additional caching steps are taken. This is primarily a client-side optimization; it reduces bandwidth costs but not server processing time.
|
|
124
|
+
* *Rack::Lock*: If your application is not marked as threadsafe (`config.threadsafe!`), this middleware will add a mutex around your requests.
|
|
125
|
+
* *Rack::Runtime*: Adds a header to the response listing the total runtime of the request.
|
|
126
|
+
* *Rack::Sendfile*: Uses a front-end server's file serving support from your Rails application.
|
|
127
|
+
* *Rails::Rack::Logger*: Log the request started and flush all loggers after it.
|
|
143
128
|
|
|
144
129
|
Other plugins, including *ActiveRecord*, may add additional middlewares. In general, these middlewares are agnostic to the type of app you are building, and make sense in an API-only Rails application.
|
|
145
130
|
|
|
@@ -165,7 +150,7 @@ config.middleware.use Rack::MethodOverride
|
|
|
165
150
|
|
|
166
151
|
#### Removing Middlewares
|
|
167
152
|
|
|
168
|
-
If you don't want to use a middleware that is included by default in the
|
|
153
|
+
If you don't want to use a middleware that is included by default in the API middleware set, you can remove it using *config.middleware.delete*:
|
|
169
154
|
|
|
170
155
|
```ruby
|
|
171
156
|
config.middleware.delete ::Rack::Sendfile
|
|
@@ -175,7 +160,7 @@ Keep in mind that removing these features may remove support for certain feature
|
|
|
175
160
|
|
|
176
161
|
### Choosing Controller Modules
|
|
177
162
|
|
|
178
|
-
An
|
|
163
|
+
An API application (using *ActionController::API*) comes with the following controller modules by default:
|
|
179
164
|
|
|
180
165
|
* *ActionController::UrlFor*: Makes *url_for* and friends available
|
|
181
166
|
* *ActionController::Redirecting*: Support for *redirect_to*
|
|
@@ -202,14 +187,13 @@ All Action Controller modules know about their dependent modules, so you can fee
|
|
|
202
187
|
Some common modules you might want to add:
|
|
203
188
|
|
|
204
189
|
* *AbstractController::Translation*: Support for the *l* and *t* localization and translation methods. These delegate to *I18n.translate* and *I18n.localize*.
|
|
205
|
-
* *ActionController::
|
|
190
|
+
* *ActionController::HttpAuthentication::Basic::ControllerMethods* (or *Digest* or *Token*): Support for basic, digest or token HTTP authentication.
|
|
206
191
|
* *AbstractController::Layouts*: Support for layouts when rendering.
|
|
207
192
|
* *ActionController::MimeResponds*: Support for content negotiation (*respond_to*, *respond_with*).
|
|
208
193
|
* *ActionController::Cookies*: Support for *cookies*, which includes support for signed and encrypted cookies. This requires the cookie middleware.
|
|
209
194
|
|
|
210
195
|
The best place to add a module is in your *ApplicationController*. You can also add modules to individual controllers.
|
|
211
196
|
|
|
212
|
-
|
|
213
197
|
## Contributing
|
|
214
198
|
|
|
215
199
|
1. Fork it
|
|
@@ -11,7 +11,7 @@ module Rails
|
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
if config.force_ssl
|
|
14
|
-
middleware.use
|
|
14
|
+
middleware.use ssl_module, config.ssl_options
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
if config.action_dispatch.x_sendfile_header.present?
|
|
@@ -64,6 +64,15 @@ module Rails
|
|
|
64
64
|
|
|
65
65
|
private
|
|
66
66
|
|
|
67
|
+
def ssl_module
|
|
68
|
+
if defined? ::ActionDispatch::SSL
|
|
69
|
+
::ActionDispatch::SSL
|
|
70
|
+
else
|
|
71
|
+
require 'rack/ssl'
|
|
72
|
+
::Rack::SSL
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
67
76
|
def setup_generators!
|
|
68
77
|
generators = config.generators
|
|
69
78
|
|
data/lib/rails-api/version.rb
CHANGED
|
@@ -10,6 +10,7 @@ class AppGeneratorTest < Rails::Generators::TestCase
|
|
|
10
10
|
run_generator
|
|
11
11
|
|
|
12
12
|
default_files.each { |path| assert_file path }
|
|
13
|
+
skipped_files.each { |path| assert_no_file path }
|
|
13
14
|
end
|
|
14
15
|
|
|
15
16
|
def test_api_modified_files
|
|
@@ -18,6 +19,7 @@ class AppGeneratorTest < Rails::Generators::TestCase
|
|
|
18
19
|
assert_file "Gemfile" do |content|
|
|
19
20
|
assert_match(/gem 'rails-api'/, content)
|
|
20
21
|
assert_no_match(/gem 'coffee-rails'/, content)
|
|
22
|
+
assert_no_match(/gem 'jquery-rails'/, content)
|
|
21
23
|
assert_no_match(/gem 'sass-rails'/, content)
|
|
22
24
|
end
|
|
23
25
|
assert_file "app/controllers/application_controller.rb", /ActionController::API/
|
|
@@ -47,10 +49,11 @@ class AppGeneratorTest < Rails::Generators::TestCase
|
|
|
47
49
|
test/functional
|
|
48
50
|
test/integration
|
|
49
51
|
test/performance
|
|
50
|
-
test/unit
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
test/unit)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def skipped_files
|
|
56
|
+
%w(vendor/assets
|
|
54
57
|
tmp/cache/assets)
|
|
55
58
|
end
|
|
56
59
|
end
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rails-api
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
hash:
|
|
4
|
+
hash: 27
|
|
5
5
|
prerelease: false
|
|
6
6
|
segments:
|
|
7
7
|
- 0
|
|
8
8
|
- 0
|
|
9
|
-
-
|
|
10
|
-
version: 0.0.
|
|
9
|
+
- 2
|
|
10
|
+
version: 0.0.2
|
|
11
11
|
platform: ruby
|
|
12
12
|
authors:
|
|
13
13
|
- Santiago Pastorino and Carlos Antonio da Silva
|
|
@@ -15,7 +15,7 @@ autorequire:
|
|
|
15
15
|
bindir: bin
|
|
16
16
|
cert_chain: []
|
|
17
17
|
|
|
18
|
-
date: 2012-
|
|
18
|
+
date: 2012-06-06 00:00:00 -03:00
|
|
19
19
|
default_executable:
|
|
20
20
|
dependencies:
|
|
21
21
|
- !ruby/object:Gem::Dependency
|