intermodal 0.0.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/Gemfile +4 -0
- data/LICENSE +1 -1
- data/README +6 -4
- data/Rakefile +6 -0
- data/intermodal.gemspec +51 -0
- data/lib/generators/intermodal_generator.rb +8 -0
- data/lib/intermodal.rb +122 -0
- data/lib/intermodal/api.rb +246 -0
- data/lib/intermodal/api/configuration.rb +40 -0
- data/lib/intermodal/api/railties.rb +21 -0
- data/lib/intermodal/concerns/acceptors/named_resource.rb +12 -0
- data/lib/intermodal/concerns/acceptors/resource.rb +12 -0
- data/lib/intermodal/concerns/controllers/accountability.rb +17 -0
- data/lib/intermodal/concerns/controllers/anonymous.rb +24 -0
- data/lib/intermodal/concerns/controllers/authenticatable.rb +24 -0
- data/lib/intermodal/concerns/controllers/paginated_collection.rb +25 -0
- data/lib/intermodal/concerns/controllers/presentation.rb +17 -0
- data/lib/intermodal/concerns/controllers/resource.rb +51 -0
- data/lib/intermodal/concerns/controllers/resource_linking.rb +58 -0
- data/lib/intermodal/concerns/let.rb +21 -0
- data/lib/intermodal/concerns/models/access_credential.rb +28 -0
- data/lib/intermodal/concerns/models/account.rb +16 -0
- data/lib/intermodal/concerns/models/accountability.rb +47 -0
- data/lib/intermodal/concerns/models/db_access_token.rb +29 -0
- data/lib/intermodal/concerns/models/has_parent_resource.rb +45 -0
- data/lib/intermodal/concerns/models/presentation.rb +25 -0
- data/lib/intermodal/concerns/models/redis_access_token.rb +60 -0
- data/lib/intermodal/concerns/models/resource_linking.rb +126 -0
- data/lib/intermodal/concerns/models/sanitize_html.rb +35 -0
- data/lib/intermodal/concerns/presenters/named_resource.rb +12 -0
- data/lib/intermodal/concerns/presenters/resource.rb +14 -0
- data/lib/intermodal/concerns/rails/rails_3_stack.rb +42 -0
- data/lib/intermodal/concerns/rails/rails_4_stack.rb +17 -0
- data/lib/intermodal/concerns/rails/use_warden.rb +21 -0
- data/lib/intermodal/config.rb +15 -0
- data/lib/intermodal/configuration.rb +11 -0
- data/lib/intermodal/controllers/api_controller.rb +26 -0
- data/lib/intermodal/controllers/linking_resource_controller.rb +8 -0
- data/lib/intermodal/controllers/nested_resource_controller.rb +18 -0
- data/lib/intermodal/controllers/resource_controller.rb +11 -0
- data/lib/intermodal/dsl/controllers.rb +125 -0
- data/lib/intermodal/dsl/mapping.rb +79 -0
- data/lib/intermodal/dsl/presentation_helpers.rb +107 -0
- data/lib/intermodal/mapping/acceptor.rb +2 -2
- data/lib/intermodal/mapping/mapper.rb +39 -13
- data/lib/intermodal/mapping/presenter.rb +12 -6
- data/lib/intermodal/proxies/linking_resources.rb +58 -0
- data/lib/intermodal/proxies/will_paginate.rb +85 -0
- data/lib/intermodal/rack/auth.rb +29 -0
- data/lib/intermodal/rack/dummy_store.rb +24 -0
- data/lib/intermodal/rack/rescue.rb +82 -0
- data/lib/intermodal/responders/linking_resource_responder.rb +21 -0
- data/lib/intermodal/responders/resource_responder.rb +64 -0
- data/lib/intermodal/rspec/acceptors.rb +79 -0
- data/lib/intermodal/rspec/models/accountability.rb +114 -0
- data/lib/intermodal/rspec/models/has_parent_resource.rb +132 -0
- data/lib/intermodal/rspec/models/resource_linking.rb +234 -0
- data/lib/intermodal/rspec/models/sanitization.rb +84 -0
- data/lib/intermodal/rspec/presenters.rb +92 -0
- data/lib/intermodal/rspec/requests/authenticated_requests.rb +17 -0
- data/lib/intermodal/rspec/requests/linked_resources.rb +180 -0
- data/lib/intermodal/rspec/requests/paginated_collection.rb +60 -0
- data/lib/intermodal/rspec/requests/rack.rb +142 -0
- data/lib/intermodal/rspec/requests/request_validations.rb +36 -0
- data/lib/intermodal/rspec/requests/resources.rb +275 -0
- data/lib/intermodal/rspec/requests/rfc2616_status_codes.rb +51 -0
- data/lib/intermodal/rspec/validators.rb +86 -0
- data/lib/intermodal/validators/account_validator.rb +27 -0
- data/lib/intermodal/validators/different_account_validator.rb +27 -0
- data/lib/intermodal/version.rb +3 -0
- data/spec/mapping/acceptors_spec.rb +142 -0
- data/spec/mapping/presenters_spec.rb +186 -0
- data/spec/models/accountability_spec.rb +13 -0
- data/spec/models/has_parent_resource_spec.rb +18 -0
- data/spec/models/resource_linking_spec.rb +21 -0
- data/spec/proxies/will_paginate_spec.rb +163 -0
- data/spec/rack/auth_spec.rb +51 -0
- data/spec/requests/linked_resources.rb +37 -0
- data/spec/requests/nested_resources_spec.rb +54 -0
- data/spec/requests/resources_spec.rb +50 -0
- data/spec/spec_helper.rb +53 -0
- data/spec/support/api.rb +50 -0
- data/spec/support/app/class_builder.rb +41 -0
- data/spec/support/app/db/adapter_helper.rb +53 -0
- data/spec/support/app/db/authentication_schema_helper.rb +62 -0
- data/spec/support/app/db/migration_helper.rb +44 -0
- data/spec/support/app/schema.rb +101 -0
- data/spec/support/application.rb +23 -0
- data/spec/support/blueprints.rb +41 -0
- data/spec/support/epiphyte.rb +29 -0
- metadata +393 -52
- data/lib/intermodal/base.rb +0 -13
- data/lib/intermodal/declare_controllers.rb +0 -102
- data/lib/intermodal/mapping.rb +0 -4
- data/lib/intermodal/mapping/dsl.rb +0 -76
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6819c724b038a70c2a3db2e93e971496a9cef118
|
4
|
+
data.tar.gz: ad9b851f517f20640f614019fa5f05e3d2179300
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 92d1ef56ad8141fd0c46e9e9666d1fa4b2acc976d838957c8d804e6de8df7193c9c754715fe8f4eb384c6f41b184949e21b11ac48b962a0ef83f413dda0c114c
|
7
|
+
data.tar.gz: 595cc3c434619e8a50ddeb0e9d9baa9e2e7c5fd72635cab2fe4f5461a5299cc680f13ee75da44845c0b02b80ae58266ceaf9296b12ecbaf5de79b9ac902b0c22
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c) 2011
|
1
|
+
Copyright (c) 2011 Sparkfly, Inc.
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
4
|
|
data/README
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Intermodal 0.0
|
1
|
+
Intermodal 0.4.0
|
2
2
|
|
3
3
|
=== Summary ===
|
4
4
|
|
@@ -15,13 +15,15 @@ Intermodal lets you quickly put together a pure, JSON/XML-only RESTful web servi
|
|
15
15
|
|
16
16
|
=== Requirements ==
|
17
17
|
|
18
|
-
Rails
|
18
|
+
Rails 4.2.x
|
19
|
+
|
20
|
+
Anything below that is no longer supported.
|
19
21
|
|
20
22
|
=== TODO ===
|
21
23
|
|
22
24
|
- Fix known bug with class reloading in development mode
|
23
25
|
- Not everything is packaged well
|
24
26
|
- Should include standardized paging
|
25
|
-
- Integration as Rails 3.1 Engine for versioning endpoints
|
26
27
|
- Cross-account authorization
|
27
|
-
-
|
28
|
+
- Reorganize controller concerns
|
29
|
+
- Quick-Start and HOWTO guides
|
data/Rakefile
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
1
2
|
require 'rake'
|
2
3
|
require 'rake/testtask'
|
3
4
|
require 'rake/rdoctask'
|
@@ -13,6 +14,7 @@ RSpec::Core::RakeTask.new(:spec) do |t|
|
|
13
14
|
end
|
14
15
|
|
15
16
|
desc 'Generate documentation for the foreigner plugin.'
|
17
|
+
|
16
18
|
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
19
|
rdoc.rdoc_dir = 'rdoc'
|
18
20
|
rdoc.title = 'Intermodal'
|
@@ -21,6 +23,7 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
21
23
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
24
|
end
|
23
25
|
|
26
|
+
=begin
|
24
27
|
begin
|
25
28
|
require 'jeweler'
|
26
29
|
Jeweler::Tasks.new do |gemspec|
|
@@ -30,9 +33,12 @@ begin
|
|
30
33
|
gemspec.email = "hosh@sparkfly.com"
|
31
34
|
gemspec.homepage = "http://github.com/hosh/intermodal"
|
32
35
|
gemspec.authors = ["Ho-Sheng Hsiao"]
|
36
|
+
|
37
|
+
gemspec.add_dependency 'will_paginate', '>=3.0.0'
|
33
38
|
end
|
34
39
|
Jeweler::GemcutterTasks.new
|
35
40
|
rescue LoadError
|
36
41
|
puts "Jeweler not available. Install it with: gem install jeweler"
|
37
42
|
end
|
43
|
+
=end
|
38
44
|
|
data/intermodal.gemspec
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "intermodal/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "intermodal"
|
7
|
+
s.version = Intermodal::VERSION
|
8
|
+
s.authors = ["Ho-Sheng Hsiao"]
|
9
|
+
s.email = %w{talktohosh@gmail.com}
|
10
|
+
|
11
|
+
s.date = %q{2011-08-26}
|
12
|
+
|
13
|
+
s.rubygems_version = %q{1.3.7}
|
14
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
15
|
+
|
16
|
+
s.homepage = %q{http://github.com/intermodal/intermodal}
|
17
|
+
s.summary = %q{Intermodal lets you quickly put together a pure, JSON-only RESTful web service.}
|
18
|
+
s.description = %q{DSL for top-level, nested, linked CRUD resource endpoints; DSL for Presenters and Acceptors; API Versioning}
|
19
|
+
s.rubyforge_project = "intermodal"
|
20
|
+
|
21
|
+
s.files = `git ls-files`.split("\n")
|
22
|
+
|
23
|
+
s.extra_rdoc_files = %w(LICENSE README)
|
24
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
25
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
26
|
+
s.require_paths = %w(lib)
|
27
|
+
|
28
|
+
|
29
|
+
s.add_runtime_dependency 'rails', '~> 4.2.0'
|
30
|
+
s.add_runtime_dependency 'responders', '~> 2.1.0'
|
31
|
+
s.add_runtime_dependency 'will_paginate', '>= 3.0.0'
|
32
|
+
s.add_runtime_dependency 'warden'
|
33
|
+
s.add_runtime_dependency 'bcrypt'
|
34
|
+
s.add_runtime_dependency 'rlet', '~> 0.6.1'
|
35
|
+
|
36
|
+
s.add_development_dependency 'awesome_print'
|
37
|
+
s.add_development_dependency 'pry'
|
38
|
+
s.add_development_dependency 'rspec'
|
39
|
+
s.add_development_dependency 'rspec-rails'
|
40
|
+
s.add_development_dependency 'rspec-core'
|
41
|
+
s.add_development_dependency 'multi_json'
|
42
|
+
s.add_development_dependency 'yajl-ruby'
|
43
|
+
s.add_development_dependency 'database_cleaner'
|
44
|
+
s.add_development_dependency 'machinist', '>= 2.0.0.beta2'
|
45
|
+
s.add_development_dependency 'faker'
|
46
|
+
s.add_development_dependency 'forgery'
|
47
|
+
s.add_development_dependency 'sqlite3'
|
48
|
+
s.add_development_dependency 'pg'
|
49
|
+
s.add_development_dependency 'redis'
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
# Sets up Intermodal on Rails
|
2
|
+
class IntermodalGenerator < Rails::Generators::Base
|
3
|
+
def create_initializer_file
|
4
|
+
generate "model", "account name:string --timestamps"
|
5
|
+
generate "model", "access_token account_id:integer token:string" # Needs foreign key
|
6
|
+
generate "model", "access_credential account_id:integer identity:string password_digest:string" # Needs foreign key
|
7
|
+
end
|
8
|
+
end
|
data/lib/intermodal.rb
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'intermodal/version'
|
2
|
+
require 'active_support'
|
3
|
+
require 'will_paginate'
|
4
|
+
require 'rlet/functional'
|
5
|
+
|
6
|
+
module Intermodal
|
7
|
+
# Core
|
8
|
+
autoload :API, 'intermodal/api'
|
9
|
+
autoload :Configuration, 'intermodal/configuration'
|
10
|
+
|
11
|
+
module DSL
|
12
|
+
autoload :Controllers, 'intermodal/dsl/controllers'
|
13
|
+
autoload :Mapping, 'intermodal/dsl/mapping'
|
14
|
+
autoload :PresentationHelpers, 'intermodal/dsl/presentation_helpers'
|
15
|
+
end
|
16
|
+
|
17
|
+
module Mapping
|
18
|
+
autoload :Mapper, 'intermodal/mapping/mapper'
|
19
|
+
autoload :Presenter, 'intermodal/mapping/presenter'
|
20
|
+
autoload :Acceptor, 'intermodal/mapping/acceptor'
|
21
|
+
end
|
22
|
+
|
23
|
+
# Controller Templates
|
24
|
+
autoload :APIController, 'intermodal/controllers/api_controller'
|
25
|
+
autoload :ResourceController, 'intermodal/controllers/resource_controller'
|
26
|
+
autoload :NestedResourceController, 'intermodal/controllers/nested_resource_controller'
|
27
|
+
autoload :LinkingResourceController, 'intermodal/controllers/linking_resource_controller'
|
28
|
+
|
29
|
+
autoload :ResourceResponder, 'intermodal/responders/resource_responder'
|
30
|
+
autoload :LinkingResourceResponder, 'intermodal/responders/linking_resource_responder'
|
31
|
+
|
32
|
+
# Authentication
|
33
|
+
# TOOD: Make this more configurable. Not everyone wants X-Auth-Token auth
|
34
|
+
module Rack
|
35
|
+
autoload :Auth, 'intermodal/rack/auth'
|
36
|
+
autoload :Rescue, 'intermodal/rack/rescue'
|
37
|
+
autoload :DummyStore, 'intermodal/rack/dummy_store'
|
38
|
+
end
|
39
|
+
|
40
|
+
# Authentication currently require two models
|
41
|
+
# Credentials are like passwords.
|
42
|
+
# Tokens are like cookie sessions. It currently stores against Redis
|
43
|
+
module Models
|
44
|
+
autoload :Account, 'intermodal/concerns/models/account'
|
45
|
+
autoload :AccessCredential, 'intermodal/concerns/models/access_credential'
|
46
|
+
autoload :AccessToken, 'intermodal/concerns/models/access_token'
|
47
|
+
end
|
48
|
+
|
49
|
+
# Concerns
|
50
|
+
autoload :Let, 'intermodal/concerns/let'
|
51
|
+
|
52
|
+
module Models
|
53
|
+
autoload :Accountability, 'intermodal/concerns/models/accountability'
|
54
|
+
autoload :HasParentResource, 'intermodal/concerns/models/has_parent_resource'
|
55
|
+
autoload :ResourceLinking, 'intermodal/concerns/models/resource_linking'
|
56
|
+
autoload :Presentation, 'intermodal/concerns/models/presentation'
|
57
|
+
|
58
|
+
autoload :SanitizeHTML, 'intermodal/concerns/models/sanitize_html'
|
59
|
+
end
|
60
|
+
|
61
|
+
module Controllers
|
62
|
+
autoload :Accountability, 'intermodal/concerns/controllers/accountability'
|
63
|
+
autoload :Anonymous, 'intermodal/concerns/controllers/anonymous'
|
64
|
+
autoload :Presentation, 'intermodal/concerns/controllers/presentation'
|
65
|
+
autoload :PaginatedCollection, 'intermodal/concerns/controllers/paginated_collection'
|
66
|
+
autoload :Resource, 'intermodal/concerns/controllers/resource'
|
67
|
+
autoload :ResourceLinking, 'intermodal/concerns/controllers/resource_linking'
|
68
|
+
end
|
69
|
+
|
70
|
+
module Validators
|
71
|
+
autoload :AccountValidator, 'intermodal/validators/account_validator'
|
72
|
+
autoload :DifferentAccountValidator, 'intermodal/validators/different_account_validator'
|
73
|
+
end
|
74
|
+
|
75
|
+
module Acceptors
|
76
|
+
autoload :Resource, 'intermodal/concerns/acceptors/resource'
|
77
|
+
autoload :NamedResource, 'intermodal/concerns/acceptors/named_resource'
|
78
|
+
end
|
79
|
+
|
80
|
+
module Presenters
|
81
|
+
autoload :Resource, 'intermodal/concerns/presenters/resource'
|
82
|
+
autoload :NamedResource, 'intermodal/concerns/presenters/named_resource'
|
83
|
+
end
|
84
|
+
|
85
|
+
module Proxies
|
86
|
+
autoload :LinkingResources, 'intermodal/proxies/linking_resources'
|
87
|
+
end
|
88
|
+
|
89
|
+
# Exceptions
|
90
|
+
BadRequest = Class.new(Exception)
|
91
|
+
|
92
|
+
# Extensions
|
93
|
+
ActiveSupport.on_load(:after_initialize) do
|
94
|
+
# Make sure this loads after Will Paginate loads
|
95
|
+
require 'intermodal/proxies/will_paginate'
|
96
|
+
|
97
|
+
::WillPaginate::Collection.send(:include, Intermodal::Proxies::WillPaginate::Collection)
|
98
|
+
end
|
99
|
+
|
100
|
+
Functional = RLet::Functional
|
101
|
+
|
102
|
+
# Rspec Macros
|
103
|
+
module RSpec
|
104
|
+
# Models
|
105
|
+
autoload :Accountability, 'intermodal/rspec/models/accountability'
|
106
|
+
autoload :HasParentResource, 'intermodal/rspec/models/has_parent_resource'
|
107
|
+
autoload :ResourceLinking, 'intermodal/rspec/models/resource_linking'
|
108
|
+
autoload :Sanitization, 'intermodal/rspec/models/sanitization'
|
109
|
+
autoload :Validators, 'intermodal/rspec/validators'
|
110
|
+
|
111
|
+
# Requests
|
112
|
+
autoload :Rack, 'intermodal/rspec/requests/rack'
|
113
|
+
autoload :HTTP, 'intermodal/rspec/requests/rfc2616_status_codes'
|
114
|
+
autoload :AuthenticatedRequests, 'intermodal/rspec/requests/authenticated_requests'
|
115
|
+
autoload :PaginatedCollection, 'intermodal/rspec/requests/paginated_collection'
|
116
|
+
autoload :RequestValidations, 'intermodal/rspec/requests/request_validations'
|
117
|
+
autoload :Resources, 'intermodal/rspec/requests/resources'
|
118
|
+
autoload :LinkedResources, 'intermodal/rspec/requests/linked_resources'
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
require 'intermodal/config'
|
@@ -0,0 +1,246 @@
|
|
1
|
+
require 'rails/railtie'
|
2
|
+
require 'intermodal/api/railties'
|
3
|
+
require 'intermodal/rack/rescue'
|
4
|
+
|
5
|
+
require 'active_support/core_ext/module/delegation'
|
6
|
+
|
7
|
+
# Modified from Rail 4.2 rails/engine.rb
|
8
|
+
module Intermodal
|
9
|
+
class API < Rails::Railtie
|
10
|
+
autoload :Configuration, "intermodal/api/configuration"
|
11
|
+
|
12
|
+
include Intermodal::DSL::Mapping
|
13
|
+
include Intermodal::DSL::Controllers
|
14
|
+
include Intermodal::DSL::PresentationHelpers
|
15
|
+
|
16
|
+
class_attribute :max_per_page, :default_per_page
|
17
|
+
|
18
|
+
class << self
|
19
|
+
attr_accessor :called_from
|
20
|
+
|
21
|
+
alias :api_name :railtie_name
|
22
|
+
alias :engine_name :railtie_name
|
23
|
+
|
24
|
+
delegate :eager_load!, to: :instance
|
25
|
+
|
26
|
+
def inherited(base)
|
27
|
+
unless base.abstract_railtie?
|
28
|
+
Rails::Railtie::Configuration.eager_load_namespaces << base
|
29
|
+
|
30
|
+
base.called_from = begin
|
31
|
+
call_stack = if Kernel.respond_to?(:caller_locations)
|
32
|
+
caller_locations.map { |l| l.absolute_path || l.path }
|
33
|
+
else
|
34
|
+
# Remove the line number from backtraces making sure we don't leave anything behind
|
35
|
+
caller.map { |p| p.sub(/:\d+.*/, '') }
|
36
|
+
end
|
37
|
+
|
38
|
+
File.dirname(call_stack.detect { |p| p !~ %r[railties[\w.-]*/lib/rails|rack[\w.-]*/lib/rack] })
|
39
|
+
end
|
40
|
+
|
41
|
+
# Add another initializer each time this has been inherited
|
42
|
+
# This covers a bug here, where the initializers declared in the API itself doesn't
|
43
|
+
# seem to run for whatever reason.
|
44
|
+
initializer "intermodal.load_#{base.name}", before: 'build_middleware_stack' do |app|
|
45
|
+
base.generate_api!
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
super
|
50
|
+
end
|
51
|
+
|
52
|
+
def endpoint(endpoint = nil)
|
53
|
+
@endpoint ||= nil
|
54
|
+
@endpoint = endpoint if endpoint
|
55
|
+
@endpoint
|
56
|
+
end
|
57
|
+
|
58
|
+
def routes(&blk)
|
59
|
+
if block_given?
|
60
|
+
instance.routes(&blk)
|
61
|
+
else
|
62
|
+
instance.routes
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# Setup presenters, acceptors, and controllers
|
67
|
+
def generate_api!
|
68
|
+
self.load_presentations!
|
69
|
+
self.load_controllers!
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
delegate :middleware, to: :config
|
75
|
+
delegate :engine_name, to: :class
|
76
|
+
delegate :api_name, to: :class
|
77
|
+
|
78
|
+
def initialize
|
79
|
+
@app = nil
|
80
|
+
@config = nil
|
81
|
+
@env_config = nil
|
82
|
+
@helpers = nil
|
83
|
+
@routes = nil
|
84
|
+
super
|
85
|
+
end
|
86
|
+
|
87
|
+
# Load console and invoke the registered hooks.
|
88
|
+
# Check <tt>Rails::Railtie.console</tt> for more info.
|
89
|
+
def load_console(app=self)
|
90
|
+
require "rails/console/app"
|
91
|
+
require "rails/console/helpers"
|
92
|
+
run_console_blocks(app)
|
93
|
+
self
|
94
|
+
end
|
95
|
+
|
96
|
+
# Load Rails runner and invoke the registered hooks.
|
97
|
+
# Check <tt>Rails::Railtie.runner</tt> for more info.
|
98
|
+
def load_runner(app=self)
|
99
|
+
run_runner_blocks(app)
|
100
|
+
self
|
101
|
+
end
|
102
|
+
|
103
|
+
# Load Rake, railties tasks and invoke the registered hooks.
|
104
|
+
# Check <tt>Rails::Railtie.rake_tasks</tt> for more info.
|
105
|
+
def load_tasks(app=self)
|
106
|
+
require "rake"
|
107
|
+
run_tasks_blocks(app)
|
108
|
+
self
|
109
|
+
end
|
110
|
+
|
111
|
+
# Load Rails generators and invoke the registered hooks.
|
112
|
+
# Check <tt>Rails::Railtie.generators</tt> for more info.
|
113
|
+
def load_generators(app=self)
|
114
|
+
require "rails/generators"
|
115
|
+
run_generators_blocks(app)
|
116
|
+
Rails::Generators.configure!(app.config.generators)
|
117
|
+
self
|
118
|
+
end
|
119
|
+
|
120
|
+
# Eager load the application by loading all ruby
|
121
|
+
# files inside eager_load paths.
|
122
|
+
def eager_load!
|
123
|
+
self.load_presentations!
|
124
|
+
self.class.load_controllers!
|
125
|
+
return
|
126
|
+
end
|
127
|
+
|
128
|
+
def railties
|
129
|
+
@railties ||= Railties.new
|
130
|
+
end
|
131
|
+
|
132
|
+
# Returns a module with all the helpers defined for the engine.
|
133
|
+
def helpers
|
134
|
+
# returns empty module (for now)
|
135
|
+
@helpers ||= begin
|
136
|
+
Module.new
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
# Returns the underlying rack application for this engine.
|
141
|
+
def app
|
142
|
+
@app ||= begin
|
143
|
+
config.middleware = config.middleware.merge_into(default_middleware_stack)
|
144
|
+
config.middleware.build(endpoint)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
# Returns the endpoint for this engine. If none is registered,
|
149
|
+
# defaults to an ActionDispatch::Routing::RouteSet.
|
150
|
+
def endpoint
|
151
|
+
self.class.endpoint || routes
|
152
|
+
end
|
153
|
+
|
154
|
+
# Define the Rack API for this engine.
|
155
|
+
def call(env)
|
156
|
+
env.merge!(env_config)
|
157
|
+
if env['SCRIPT_NAME']
|
158
|
+
env["ROUTES_#{routes.object_id}_SCRIPT_NAME"] = env['SCRIPT_NAME'].dup
|
159
|
+
end
|
160
|
+
app.call(env)
|
161
|
+
end
|
162
|
+
|
163
|
+
# Defines additional Rack env configuration that is added on each call.
|
164
|
+
def env_config
|
165
|
+
@env_config ||= {
|
166
|
+
'action_dispatch.routes' => routes
|
167
|
+
}
|
168
|
+
end
|
169
|
+
|
170
|
+
# Defines the routes for this engine. If a block is given to
|
171
|
+
# routes, it is appended to the engine.
|
172
|
+
def routes
|
173
|
+
@routes ||= ActionDispatch::Routing::RouteSet.new
|
174
|
+
@routes.append(&Proc.new) if block_given?
|
175
|
+
@routes
|
176
|
+
end
|
177
|
+
|
178
|
+
# Define the configuration object for the engine.
|
179
|
+
def config
|
180
|
+
@config ||= API::Configuration.new
|
181
|
+
end
|
182
|
+
|
183
|
+
initializer :add_routing_paths do |app|
|
184
|
+
app.routes_reloader.route_sets << routes if routes?
|
185
|
+
end
|
186
|
+
|
187
|
+
# This code, especially running under Rspec, is simply not thread-safe.
|
188
|
+
initializer 'intermodal.disable_concurrency', before: 'build_middleware_stack' do |app|
|
189
|
+
app.config.allow_concurrency = false
|
190
|
+
end
|
191
|
+
|
192
|
+
initializer 'intermodal.strip_middleware', after: 'build_middleware_stack' do |app|
|
193
|
+
app.config.middleware.delete 'Rack::Lock' # Run this in a multi-process server, not multithreaded
|
194
|
+
app.config.middleware.delete 'ActionDispatch::Flash'
|
195
|
+
# Use our own parsing code and responses, or drop this after Intermodal::Rack::Rescue
|
196
|
+
app.config.middleware.delete 'ActionDispatch::ParamsParser'
|
197
|
+
end
|
198
|
+
|
199
|
+
initializer 'intermodal.load_x_auth_token_warden', :before => 'build_middleware_stack' do |app|
|
200
|
+
Warden::Strategies.add(:x_auth_token) do
|
201
|
+
def valid?
|
202
|
+
env['HTTP_X_AUTH_TOKEN']
|
203
|
+
end
|
204
|
+
|
205
|
+
def authenticate!
|
206
|
+
a = AccessToken.authenticate!(env['HTTP_X_AUTH_TOKEN'])
|
207
|
+
a.nil? ? fail!("Could not log in") : success!(a)
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
initializer :engines_blank_point do
|
213
|
+
# We need this initializer so all extra initializers added in engines are
|
214
|
+
# consistently executed after all the initializers above across all engines.
|
215
|
+
end
|
216
|
+
|
217
|
+
def routes? #:nodoc:
|
218
|
+
@routes
|
219
|
+
end
|
220
|
+
|
221
|
+
protected
|
222
|
+
|
223
|
+
def default_middleware_stack
|
224
|
+
ActionDispatch::MiddlewareStack.new.tap do |middleware|
|
225
|
+
middleware.use ::Intermodal::Rack::Rescue
|
226
|
+
|
227
|
+
# TODO: Find a way to patch Warden so this is not necessary
|
228
|
+
# middleware.use config.session_store, config.session_options
|
229
|
+
# Use random secret for now until we can get rid of this.
|
230
|
+
middleware.use Intermodal::Rack::DummyStore
|
231
|
+
#middleware.use ::ActionDispatch::Flash
|
232
|
+
middleware.use Warden::Manager do |manager|
|
233
|
+
manager.default_strategies :x_auth_token #, :basic
|
234
|
+
manager.failure_app = proc do
|
235
|
+
Intermodal::Rack::Auth::UNAUTHORIZED
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
middleware.use ::ActionDispatch::ParamsParser
|
240
|
+
middleware.use ::Rack::MethodOverride
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
|
245
|
+
end
|
246
|
+
end
|