rails-api 0.1.0 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 200603c563a12a084abd7f3f46fd12ba552a5564
4
- data.tar.gz: c757f33c4958d7b161f5b7fd0f744ba61503b48b
3
+ metadata.gz: 5253c491976015f9cfbe4ed845107e35804473e0
4
+ data.tar.gz: b74a7076b3aba37160e2b136c0207433a9f94d7c
5
5
  SHA512:
6
- metadata.gz: 421034d4885439e69a73e417e171dfaf5e8c3fedaca8e90182e0865cbd2d3502f03b25243e9db47084444891f33767689072f309118b88625ca66893dc81f75f
7
- data.tar.gz: fbe970847a57d9ba05c9bbfd21016a73bece79ee71a9c0a87b1a7ebcc9e174cbcf5f70c3903dc8abf0d31c4977816f8049c59c587373c013da00173f94e081fb
6
+ metadata.gz: 1641fe3267a1137ade2ca4323350e8a70a43fde041ecd28e2179a406125796529e5b2ad5f56e80fb47b324213011c35d7b2a4e9eeba24309c38073fa1665ed30
7
+ data.tar.gz: 44a72a3a29b7178023ada10e2eb6ede998322fcb1af61675c7f127e4a059be36a1d4ce8c8b67471478a931db838de5a0e3461debdda5193a74a32b01c27fbd67
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Santiago Pastorino and Carlos Antonio da Silva
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -84,6 +84,31 @@ This will do two main things for you:
84
84
  * 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.
85
85
  * Configure the generators to skip generating views, helpers and assets when you generate a new resource.
86
86
 
87
+ Rails includes all of the sub-frameworks (ActiveRecord, ActionMailer, etc) by default. Some API projects won't need them all, so at the top of config/application.rb, you can replace `require 'rails/all'` with specific sub-frameworks:
88
+
89
+ # config/application.rb
90
+ # require "active_record/railtie"
91
+ require "action_controller/railtie"
92
+ require "action_mailer/railtie"
93
+ # require "sprockets/railtie"
94
+ require "rails/test_unit/railtie"
95
+
96
+ This can also be achieved with flags when creating a new **Rails::API** app:
97
+
98
+ rails-api new my_api --skip-active-record --skip-sprockets
99
+
100
+ Note: There are references to ActionMailer and ActiveRecord in the various
101
+ config/environment files. If you decide to exclude any of these from your project
102
+ its best to comment these out in case you need them later.
103
+
104
+ # comment out this in config/environments/development.rb
105
+ config.active_record.migration_error = :page_load
106
+ config.action_mailer.raise_delivery_errors = false
107
+
108
+ # comment out this in config/environments/test.rb
109
+ config.action_mailer.delivery_method = :test
110
+
111
+
87
112
  #### For already existing apps
88
113
 
89
114
  If you want to take an existing app and make it a **Rails::API** app, you'll have to do some quick setup manually.
@@ -112,7 +137,7 @@ If you want to use the Rails default middleware stack (avoid the reduction that
112
137
 
113
138
  ### Serialization
114
139
 
115
- We suggest using [ActiveModel::Serializers][ams] to deserialize your ActiveModel/ActiveRecord objects into the desired response format (e.g. JSON).
140
+ We suggest using [ActiveModel::Serializers][ams] to serialize your ActiveModel/ActiveRecord objects into the desired response format (e.g. JSON).
116
141
 
117
142
  ### Choosing Middlewares
118
143
 
@@ -196,7 +221,7 @@ Some common modules you might want to add:
196
221
  * *AbstractController::Translation*: Support for the *l* and *t* localization and translation methods. These delegate to *I18n.translate* and *I18n.localize*.
197
222
  * *ActionController::HttpAuthentication::Basic::ControllerMethods* (or *Digest* or *Token*): Support for basic, digest or token HTTP authentication.
198
223
  * *AbstractController::Layouts*: Support for layouts when rendering.
199
- * *ActionController::MimeResponds*: Support for content negotiation (*respond_to*, *respond_with*).
224
+ * *ActionController::MimeResponds* (and *ActionController::ImplicitRender* for Rails 4): Support for content negotiation (*respond_to*, *respond_with*).
200
225
  * *ActionController::Cookies*: Support for *cookies*, which includes support for signed and encrypted cookies. This requires the cookie middleware.
201
226
 
202
227
  The best place to add a module is in your *ApplicationController*. You can also add modules to individual controllers.
@@ -1,3 +1,4 @@
1
+ require 'action_view'
1
2
  require 'action_controller'
2
3
  require 'action_controller/log_subscriber'
3
4
 
@@ -145,10 +146,19 @@ module ActionController
145
146
  Instrumentation
146
147
  ]
147
148
 
149
+ if Rails::VERSION::MAJOR >= 4 && Rails::VERSION::MINOR > 0
150
+ include AbstractController::Rendering
151
+ include ActionView::Rendering
152
+ end
153
+
148
154
  MODULES.each do |mod|
149
155
  include mod
150
156
  end
151
157
 
158
+ if Rails::VERSION::MAJOR >= 4
159
+ include StrongParameters
160
+ end
161
+
152
162
  ActiveSupport.run_load_hooks(:action_controller, self)
153
163
  end
154
164
  end
@@ -1,12 +1,13 @@
1
1
  require 'rails/version'
2
2
  require 'rails/application'
3
3
  require 'rails-api/public_exceptions'
4
+ require 'rails-api/application/default_rails_four_middleware_stack'
4
5
 
5
6
  module Rails
6
7
  class Application < Engine
7
8
  def default_middleware_stack
8
9
  if Rails::API.rails4?
9
- rails_four_stack
10
+ DefaultRailsFourMiddlewareStack.new(self, config, paths).build_stack
10
11
  else
11
12
  rails_three_stack
12
13
  end
@@ -36,62 +37,6 @@ module Rails
36
37
  setup_generators!
37
38
  end
38
39
 
39
- def rails_four_stack
40
- ActionDispatch::MiddlewareStack.new.tap do |middleware|
41
- app = self
42
- if rack_cache = config.action_dispatch.rack_cache
43
- begin
44
- require 'rack/cache'
45
- rescue LoadError => error
46
- error.message << ' Be sure to add rack-cache to your Gemfile'
47
- raise
48
- end
49
-
50
- if rack_cache == true
51
- rack_cache = {
52
- metastore: "rails:/",
53
- entitystore: "rails:/",
54
- verbose: false
55
- }
56
- end
57
-
58
- require "action_dispatch/http/rack_cache"
59
- middleware.use ::Rack::Cache, rack_cache
60
- end
61
-
62
- if config.force_ssl
63
- middleware.use ::ActionDispatch::SSL, config.ssl_options
64
- end
65
-
66
- if config.action_dispatch.x_sendfile_header.present?
67
- middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header
68
- end
69
-
70
- if config.serve_static_assets
71
- middleware.use ::ActionDispatch::Static, paths["public"].first, config.static_cache_control
72
- end
73
-
74
- middleware.use ::Rack::Lock unless config.cache_classes
75
- middleware.use ::Rack::Runtime
76
- middleware.use ::ActionDispatch::RequestId
77
- middleware.use ::Rails::Rack::Logger, config.log_tags # must come after Rack::MethodOverride to properly log overridden methods
78
- middleware.use ::ActionDispatch::ShowExceptions, config.exceptions_app || ActionDispatch::PublicExceptions.new(Rails.public_path)
79
- middleware.use ::ActionDispatch::DebugExceptions, app
80
- middleware.use ::ActionDispatch::RemoteIp, config.action_dispatch.ip_spoofing_check, config.action_dispatch.trusted_proxies
81
-
82
- unless config.cache_classes
83
- middleware.use ::ActionDispatch::Reloader, lambda { app.reload_dependencies? }
84
- end
85
-
86
- middleware.use ::ActionDispatch::Callbacks
87
-
88
- middleware.use ::ActionDispatch::ParamsParser
89
- middleware.use ::Rack::Head
90
- middleware.use ::Rack::ConditionalGet
91
- middleware.use ::Rack::ETag, "no-cache"
92
- end
93
- end
94
-
95
40
  def rails_three_stack
96
41
  ActionDispatch::MiddlewareStack.new.tap do |middleware|
97
42
  if rack_cache = config.action_controller.perform_caching && config.action_dispatch.rack_cache
@@ -0,0 +1,101 @@
1
+ module Rails
2
+ class Application
3
+ class DefaultRailsFourMiddlewareStack
4
+ attr_reader :config, :paths, :app
5
+
6
+ def initialize(app, config, paths)
7
+ @app = app
8
+ @config = config
9
+ @paths = paths
10
+ end
11
+
12
+ def build_stack
13
+ ActionDispatch::MiddlewareStack.new.tap do |middleware|
14
+ if rack_cache = load_rack_cache
15
+ require "action_dispatch/http/rack_cache"
16
+ middleware.use ::Rack::Cache, rack_cache
17
+ end
18
+
19
+ if config.force_ssl
20
+ middleware.use ::ActionDispatch::SSL, config.ssl_options
21
+ end
22
+
23
+ if config.action_dispatch.x_sendfile_header.present?
24
+ middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header
25
+ end
26
+
27
+ if config.serve_static_assets
28
+ middleware.use ::ActionDispatch::Static, paths["public"].first, config.static_cache_control
29
+ end
30
+
31
+ middleware.use ::Rack::Lock unless allow_concurrency?
32
+ middleware.use ::Rack::Runtime
33
+ middleware.use ::Rack::MethodOverride unless config.api_only
34
+ middleware.use ::ActionDispatch::RequestId
35
+
36
+ # Must come after Rack::MethodOverride to properly log overridden methods
37
+ middleware.use ::Rails::Rack::Logger, config.log_tags
38
+ middleware.use ::ActionDispatch::ShowExceptions, show_exceptions_app
39
+ middleware.use ::ActionDispatch::DebugExceptions, app
40
+ middleware.use ::ActionDispatch::RemoteIp, config.action_dispatch.ip_spoofing_check, config.action_dispatch.trusted_proxies
41
+
42
+ unless config.cache_classes
43
+ middleware.use ::ActionDispatch::Reloader, lambda { reload_dependencies? }
44
+ end
45
+
46
+ middleware.use ::ActionDispatch::Callbacks
47
+ middleware.use ::ActionDispatch::Cookies unless config.api_only
48
+
49
+ if !config.api_only && config.session_store
50
+ if config.force_ssl && !config.session_options.key?(:secure)
51
+ config.session_options[:secure] = true
52
+ end
53
+ middleware.use config.session_store, config.session_options
54
+ middleware.use ::ActionDispatch::Flash
55
+ end
56
+
57
+ middleware.use ::ActionDispatch::ParamsParser
58
+ middleware.use ::Rack::Head
59
+ middleware.use ::Rack::ConditionalGet
60
+ middleware.use ::Rack::ETag, "no-cache"
61
+ end
62
+ end
63
+
64
+ private
65
+
66
+ def reload_dependencies?
67
+ config.reload_classes_only_on_change != true || app.reloaders.map(&:updated?).any?
68
+ end
69
+
70
+ def allow_concurrency?
71
+ config.allow_concurrency.nil? ? config.cache_classes : config.allow_concurrency
72
+ end
73
+
74
+ def load_rack_cache
75
+ rack_cache = config.action_dispatch.rack_cache
76
+ return unless rack_cache
77
+
78
+ begin
79
+ require 'rack/cache'
80
+ rescue LoadError => error
81
+ error.message << ' Be sure to add rack-cache to your Gemfile'
82
+ raise
83
+ end
84
+
85
+ if rack_cache == true
86
+ {
87
+ metastore: "rails:/",
88
+ entitystore: "rails:/",
89
+ verbose: false
90
+ }
91
+ else
92
+ rack_cache
93
+ end
94
+ end
95
+
96
+ def show_exceptions_app
97
+ config.exceptions_app || ActionDispatch::PublicExceptions.new(Rails.public_path)
98
+ end
99
+ end
100
+ end
101
+ end
@@ -11,7 +11,7 @@
11
11
  # if you're sharing your code publicly.
12
12
 
13
13
  # Although this is not needed for an api-only application, rails4
14
- # requires secret_key_base or secret_toke to be defined, otherwise an
14
+ # requires secret_key_base or secret_token to be defined, otherwise an
15
15
  # error is raised.
16
16
  # Using secret_token for rails3 compatibility. Change to secret_key_base
17
17
  # to avoid deprecation warning.
@@ -1,5 +1,5 @@
1
1
  module Rails
2
2
  module API
3
- VERSION = "0.1.0"
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
@@ -6,6 +6,10 @@ class OmgController < ActionController::API
6
6
  def index
7
7
  render :text => "OMG"
8
8
  end
9
+
10
+ def unauthorized
11
+ render :text => "get out", :status => :unauthorized
12
+ end
9
13
  end
10
14
 
11
15
  class ApiApplicationTest < ActiveSupport::TestCase
@@ -15,10 +19,14 @@ class ApiApplicationTest < ActiveSupport::TestCase
15
19
 
16
20
  def test_boot_api_app
17
21
  get "/omg"
18
- assert_equal 200, last_response.status
19
22
  assert_equal "OMG", last_response.body
20
23
  end
21
24
 
25
+ def test_proper_status_set
26
+ get "/omg/unauthorized"
27
+ assert_equal 401, last_response.status
28
+ end
29
+
22
30
  def test_api_middleware_stack
23
31
  expected_middleware_stack =
24
32
  rails4? ? expected_middleware_stack_rails4 : expected_middleware_stack_rails3
@@ -45,7 +45,7 @@ class AppGeneratorTest < Rails::Generators::TestCase
45
45
  lib/assets
46
46
  log
47
47
  test/fixtures
48
- test/#{generated_test_funcional_dir}
48
+ test/#{generated_test_functional_dir}
49
49
  test/integration
50
50
  test/#{generated_test_unit_dir}
51
51
  )
@@ -28,7 +28,7 @@ module GeneratorsTestHelper
28
28
  rails4? ? 'models' : 'unit'
29
29
  end
30
30
 
31
- def generated_test_funcional_dir
31
+ def generated_test_functional_dir
32
32
  rails4? ? 'controllers' : 'functional'
33
33
  end
34
34
 
@@ -68,7 +68,7 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
68
68
  end
69
69
  end
70
70
 
71
- assert_file "test/#{generated_test_funcional_dir}/product_lines_controller_test.rb" do |test|
71
+ assert_file "test/#{generated_test_functional_dir}/product_lines_controller_test.rb" do |test|
72
72
  assert_match(/class ProductLinesControllerTest < ActionController::TestCase/, test)
73
73
  if rails4?
74
74
  assert_match(/post :create, product_line: \{ product_id: @product_line.product_id, title: @product_line.title, user_id: @product_line.user_id \}/, test)
metadata CHANGED
@@ -1,69 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Santiago Pastorino and Carlos Antonio da Silva
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-01 00:00:00.000000000 Z
11
+ date: 2014-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 3.2.11
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 3.2.11
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: railties
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: 3.2.11
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 3.2.11
41
- - !ruby/object:Gem::Dependency
42
- name: tzinfo
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ~>
46
- - !ruby/object:Gem::Version
47
- version: 0.3.31
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ~>
53
- - !ruby/object:Gem::Version
54
- version: 0.3.31
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: rails
57
43
  requirement: !ruby/object:Gem::Requirement
58
44
  requirements:
59
- - - '>='
45
+ - - ">="
60
46
  - !ruby/object:Gem::Version
61
47
  version: 3.2.11
62
48
  type: :development
63
49
  prerelease: false
64
50
  version_requirements: !ruby/object:Gem::Requirement
65
51
  requirements:
66
- - - '>='
52
+ - - ">="
67
53
  - !ruby/object:Gem::Version
68
54
  version: 3.2.11
69
55
  description: |-
@@ -71,28 +57,30 @@ description: |-
71
57
  created for applications that don't require all
72
58
  functionality that a complete Rails application provides
73
59
  email:
74
- - <santiago@wyeworks.com>
75
- - <carlosantoniodasilva@gmail.com>
60
+ - "<santiago@wyeworks.com>"
61
+ - "<carlosantoniodasilva@gmail.com>"
76
62
  executables:
77
63
  - rails-api
78
64
  extensions: []
79
65
  extra_rdoc_files: []
80
66
  files:
67
+ - LICENSE
81
68
  - README.md
82
69
  - bin/rails-api
83
70
  - lib/generators/rails/api_resource_route/api_resource_route_generator.rb
71
+ - lib/rails-api.rb
84
72
  - lib/rails-api/action_controller/api.rb
85
73
  - lib/rails-api/application.rb
74
+ - lib/rails-api/application/default_rails_four_middleware_stack.rb
86
75
  - lib/rails-api/generators/rails/app/app_generator.rb
87
76
  - lib/rails-api/public_exceptions.rb
77
+ - lib/rails-api/templates/rails/app/Gemfile
88
78
  - lib/rails-api/templates/rails/app/app/controllers/application_controller.rb.tt
89
79
  - lib/rails-api/templates/rails/app/config/initializers/secret_token.rb.tt
90
80
  - lib/rails-api/templates/rails/app/config/initializers/wrap_parameters.rb.tt
91
- - lib/rails-api/templates/rails/app/Gemfile
92
81
  - lib/rails-api/templates/rails/scaffold_controller/controller.rb
93
82
  - lib/rails-api/templates/test_unit/scaffold/functional_test.rb
94
83
  - lib/rails-api/version.rb
95
- - lib/rails-api.rb
96
84
  - test/api_application/api_application_test.rb
97
85
  - test/api_controller/action_methods_test.rb
98
86
  - test/api_controller/conditional_get_test.rb
@@ -107,7 +95,7 @@ files:
107
95
  - test/generators/resource_generator_test.rb
108
96
  - test/generators/scaffold_generator_test.rb
109
97
  - test/test_helper.rb
110
- homepage: https://github.com/spastorino/rails-api
98
+ homepage: https://github.com/rails-api/rails-api
111
99
  licenses:
112
100
  - MIT
113
101
  metadata: {}
@@ -117,17 +105,17 @@ require_paths:
117
105
  - lib
118
106
  required_ruby_version: !ruby/object:Gem::Requirement
119
107
  requirements:
120
- - - '>='
108
+ - - ">="
121
109
  - !ruby/object:Gem::Version
122
110
  version: '0'
123
111
  required_rubygems_version: !ruby/object:Gem::Requirement
124
112
  requirements:
125
- - - '>='
113
+ - - ">="
126
114
  - !ruby/object:Gem::Version
127
115
  version: 1.3.6
128
116
  requirements: []
129
117
  rubyforge_project:
130
- rubygems_version: 2.0.0
118
+ rubygems_version: 2.2.0
131
119
  signing_key:
132
120
  specification_version: 4
133
121
  summary: Rails for API only Applications
@@ -146,4 +134,3 @@ test_files:
146
134
  - test/generators/resource_generator_test.rb
147
135
  - test/generators/scaffold_generator_test.rb
148
136
  - test/test_helper.rb
149
- has_rdoc: