intermodal 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +7 -0
- data/README +27 -0
- data/Rakefile +38 -0
- data/VERSION +1 -0
- data/lib/intermodal/base.rb +13 -0
- data/lib/intermodal/declare_controllers.rb +102 -0
- data/lib/intermodal/mapping/acceptor.rb +22 -0
- data/lib/intermodal/mapping/dsl.rb +76 -0
- data/lib/intermodal/mapping/mapper.rb +49 -0
- data/lib/intermodal/mapping/presenter.rb +30 -0
- data/lib/intermodal/mapping.rb +4 -0
- metadata +78 -0
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2011 Ho-Sheng Hsiao
|
2
|
+
|
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
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
Intermodal 0.0.1
|
2
|
+
|
3
|
+
=== Summary ===
|
4
|
+
|
5
|
+
Intermodal lets you quickly put together a pure, JSON/XML-only RESTful web service.
|
6
|
+
|
7
|
+
=== Features ===
|
8
|
+
|
9
|
+
- Declarative DSL for top-level, nested, and linked CRUD resource endpoints, producing dynamically generated resource controllers
|
10
|
+
- Override resource controller behavior
|
11
|
+
- Declarative DSL for Presenters (remap ORM to JSON/XML)
|
12
|
+
- Declarative DSL for Acceptors (remap and filter incoming data)
|
13
|
+
- Authentication mechanism using HTTP X- headers (similar to Rackspace Cloud API)
|
14
|
+
- API versioning
|
15
|
+
|
16
|
+
=== Requirements ==
|
17
|
+
|
18
|
+
Rails 3.0.x
|
19
|
+
|
20
|
+
=== TODO ===
|
21
|
+
|
22
|
+
- Fix known bug with class reloading in development mode
|
23
|
+
- Not everything is packaged well
|
24
|
+
- Should include standardized paging
|
25
|
+
- Integration as Rails 3.1 Engine for versioning endpoints
|
26
|
+
- Cross-account authorization
|
27
|
+
- Override stock Rails 3.0 error handling to produce JSON/XML instead of HTML traces
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
desc 'Default: run specs.'
|
7
|
+
task :default => :spec
|
8
|
+
|
9
|
+
desc 'Run the specs'
|
10
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
11
|
+
#t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
|
12
|
+
#t.spec_files = FileList['spec/**/*_spec.rb']
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the foreigner plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'Intermodal'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
23
|
+
|
24
|
+
begin
|
25
|
+
require 'jeweler'
|
26
|
+
Jeweler::Tasks.new do |gemspec|
|
27
|
+
gemspec.name = "intermodal"
|
28
|
+
gemspec.summary = "Intermodal lets you quickly put together a pure, JSON/XML-only RESTful web service."
|
29
|
+
gemspec.description = "Declarative DSL for top-level, nested, linked CRUD resource endpoints; DSL for Presenters and Acceptors; API Versioning"
|
30
|
+
gemspec.email = "hosh@sparkfly.com"
|
31
|
+
gemspec.homepage = "http://github.com/hosh/intermodal"
|
32
|
+
gemspec.authors = ["Ho-Sheng Hsiao"]
|
33
|
+
end
|
34
|
+
Jeweler::GemcutterTasks.new
|
35
|
+
rescue LoadError
|
36
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
37
|
+
end
|
38
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Intermodal
|
2
|
+
class Base
|
3
|
+
include Intermodal::Mapping::DSL
|
4
|
+
extend Intermodal::DeclareControllers
|
5
|
+
class_inheritable_accessor :routes
|
6
|
+
|
7
|
+
def self.inherited(klass)
|
8
|
+
ActiveSupport.on_load(:after_initialize) do
|
9
|
+
klass.load_presentations!
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
module Intermodal
|
2
|
+
module DeclareControllers
|
3
|
+
attr_accessor :controller_definitions
|
4
|
+
|
5
|
+
def controllers(&blk)
|
6
|
+
self.controller_definitions = blk
|
7
|
+
end
|
8
|
+
|
9
|
+
def resources(name, &blk)
|
10
|
+
controller_name = _controller_name(name)
|
11
|
+
_create_controller(name, controller_name, blk, :ancestor => ResourceController, :api => self)
|
12
|
+
end
|
13
|
+
|
14
|
+
def nested_resources(parent_name, name, options = {}, &blk)
|
15
|
+
_parent_model = options[:parent_model]
|
16
|
+
_parent_resource = parent_name.to_s.singularize.to_sym
|
17
|
+
_namespace = _module(parent_name)
|
18
|
+
controller_name = _controller_name(name)
|
19
|
+
|
20
|
+
_create_controller(name, controller_name, blk,
|
21
|
+
:ancestor => NestedResourceController,
|
22
|
+
:namespace => _namespace,
|
23
|
+
:model => options[:model],
|
24
|
+
:parent_resource => _parent_resource,
|
25
|
+
:parent_model => _parent_model,
|
26
|
+
:api => self)
|
27
|
+
end
|
28
|
+
|
29
|
+
def link_resources_from(parent_name, options = {}, &blk)
|
30
|
+
_model = options[:model]
|
31
|
+
_parent_model = options[:parent_model]
|
32
|
+
_parent_resource = parent_name.to_s.singularize.to_sym
|
33
|
+
_target_resource = options[:to]
|
34
|
+
_namespace = _module(parent_name)
|
35
|
+
controller_name = _controller_name(_target_resource)
|
36
|
+
|
37
|
+
customize_linking_resource = proc do
|
38
|
+
let(:model) { _model }
|
39
|
+
let(:target_ids) { params[_target_resource] }
|
40
|
+
end
|
41
|
+
|
42
|
+
controller = _create_controller(name, controller_name, customize_linking_resource,
|
43
|
+
:ancestor => LinkingResourceController,
|
44
|
+
:namespace => _namespace,
|
45
|
+
:model => _model,
|
46
|
+
:parent_resource => _parent_resource,
|
47
|
+
:parent_model => _parent_model)
|
48
|
+
controller.instance_eval(&blk) if blk
|
49
|
+
end
|
50
|
+
|
51
|
+
def load_controllers!
|
52
|
+
self.controller_definitions.call
|
53
|
+
end
|
54
|
+
|
55
|
+
# API: private
|
56
|
+
def _create_controller(resource_name, controller_name, customizations, options = {}, &blk)
|
57
|
+
_ancestor = options[:ancestor] || ApplicationController
|
58
|
+
_namespace = options[:namespace] || Object
|
59
|
+
|
60
|
+
_unload_controller_if_exists(controller_name, _namespace)
|
61
|
+
controller = Class.new(_ancestor)
|
62
|
+
_namespace.const_set(controller_name, controller)
|
63
|
+
Rails.logger.warn "Creating new resource controller: #{_namespace}::#{controller}"
|
64
|
+
|
65
|
+
controller.resource = resource_name
|
66
|
+
controller.model = options[:model] if options[:model]
|
67
|
+
controller.parent_resource = options[:parent_resource] if options[:parent_resource]
|
68
|
+
controller.parent_model = parent_model if options[:parent_model]
|
69
|
+
controller.class_eval(&customizations) if customizations
|
70
|
+
controller.api = options[:api] if options[:api]
|
71
|
+
|
72
|
+
Rails.logger.info "New class: #{controller.inspect}: #{controller.controller_name}"
|
73
|
+
controller
|
74
|
+
end
|
75
|
+
|
76
|
+
def _controller_name(name)
|
77
|
+
case name
|
78
|
+
when String then name.to_s
|
79
|
+
when Symbol then "#{name.to_s.camelize}Controller"
|
80
|
+
else
|
81
|
+
raise "resources require String or Symbol"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def _module(module_name)
|
86
|
+
module_name = module_name.to_s.camelize
|
87
|
+
if Object.const_defined?(module_name)
|
88
|
+
module_name.constantize
|
89
|
+
else
|
90
|
+
Object.const_set(module_name, Module.new)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def _unload_controller_if_exists(controller_name, _namespace = Object)
|
95
|
+
begin
|
96
|
+
_namespace.send(:remove_const, controller_name) if _namespace.const_defined?(controller_name) # Unload existing class
|
97
|
+
rescue Exception => err
|
98
|
+
Rails.logger.warn "Unable to unload #{controller_name}: #{err.inspect}"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Intermodal
|
2
|
+
module Mapping
|
3
|
+
class Acceptor < Mapper
|
4
|
+
|
5
|
+
self._mapping_strategy = EXCLUDE_NILS
|
6
|
+
|
7
|
+
class << self
|
8
|
+
alias exclude_from_acceptance exclude_properties
|
9
|
+
|
10
|
+
# Convenience alias to maps
|
11
|
+
# Examples:
|
12
|
+
#
|
13
|
+
# accepts :name
|
14
|
+
# accepts :name, :as => lambda { |o| o.name.camelize }
|
15
|
+
# accepts :name, :as => [ :first_name, :last_name, :suffix, :prefix ]
|
16
|
+
#
|
17
|
+
alias accepts maps
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module Intermodal
|
2
|
+
module Mapping
|
3
|
+
module DSL
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
class_inheritable_accessor :_presentation_description, :_presenters, :_acceptors
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
# DSL
|
12
|
+
def map_data(&blk)
|
13
|
+
self._presentation_description = blk
|
14
|
+
end
|
15
|
+
|
16
|
+
def presentation_for(resource, &customizations)
|
17
|
+
model = resource.to_s.camelize.constantize
|
18
|
+
|
19
|
+
model.send(:include, Models::Presentation)
|
20
|
+
presenter_template = Class.new(Presenter)
|
21
|
+
presenter_template._property_mapping = []
|
22
|
+
presenter_template.instance_eval(&customizations)
|
23
|
+
presenters[resource.to_sym] = presenter_template
|
24
|
+
end
|
25
|
+
|
26
|
+
def acceptance_for(resource, &customizations)
|
27
|
+
model = resource.to_s.camelize.constantize
|
28
|
+
|
29
|
+
#model.send(:include, Models::Acceptance)
|
30
|
+
acceptor = Class.new(Acceptor)
|
31
|
+
acceptor._property_mapping = []
|
32
|
+
acceptor.instance_eval(&customizations)
|
33
|
+
acceptors[resource.to_sym] = acceptor
|
34
|
+
end
|
35
|
+
|
36
|
+
# Setup
|
37
|
+
def load_presentations!
|
38
|
+
self._presentation_description.call
|
39
|
+
end
|
40
|
+
|
41
|
+
# Accessors
|
42
|
+
def presenters
|
43
|
+
self._presenters ||= {}
|
44
|
+
end
|
45
|
+
|
46
|
+
def presenter_for(model)
|
47
|
+
presenters[model_name(model)]
|
48
|
+
end
|
49
|
+
|
50
|
+
def acceptors
|
51
|
+
self._acceptors ||= {}
|
52
|
+
end
|
53
|
+
|
54
|
+
def acceptor_for(model)
|
55
|
+
acceptors[model_name(model)]
|
56
|
+
end
|
57
|
+
|
58
|
+
def model_name(model)
|
59
|
+
model.name.underscore.to_sym
|
60
|
+
end
|
61
|
+
|
62
|
+
def resource_name(resource)
|
63
|
+
model_name(resource.class)
|
64
|
+
end
|
65
|
+
|
66
|
+
def presents_resource(resource)
|
67
|
+
presenters[resource_name(resource)].call(resource)
|
68
|
+
end
|
69
|
+
|
70
|
+
def accepts_resource(resource)
|
71
|
+
accepts[resource_name(resource)].call(resource)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Intermodal
|
2
|
+
module Mapping
|
3
|
+
class Mapper
|
4
|
+
class_inheritable_accessor :_exclude_properties, :_property_mapping, :_mapping_strategy
|
5
|
+
|
6
|
+
INCLUDE_NILS = lambda { |h, resource, mapped_from, mapped_to| h[mapped_from] = map_attribute(resource, mapped_to) }
|
7
|
+
EXCLUDE_NILS = lambda { |h, resource, mapped_from, mapped_to| a = map_attribute(resource, mapped_to); h[mapped_from] = a if a }
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def exclude_properties(*args)
|
11
|
+
(self._exclude_properties ||= []).push *(args.map(&:to_s))
|
12
|
+
end
|
13
|
+
|
14
|
+
def property_mapping
|
15
|
+
self._property_mapping ||= []
|
16
|
+
end
|
17
|
+
|
18
|
+
# Examples:
|
19
|
+
#
|
20
|
+
# maps :name
|
21
|
+
# maps :name, :as => lambda { |o| o.name.camelize }
|
22
|
+
# maps :name, :as => [ :first_name, :last_name, :suffix, :prefix ]
|
23
|
+
#
|
24
|
+
def maps(property, opts = {})
|
25
|
+
property_mapping.push [property, (opts[:as] ? opts[:as] : property)]
|
26
|
+
end
|
27
|
+
|
28
|
+
def map_attributes(resource)
|
29
|
+
self._property_mapping.inject({}) { |m, p|
|
30
|
+
m.tap { |h| self._mapping_strategy[h, resource, p[0], p[1]] }
|
31
|
+
}.except(*self._exclude_properties)
|
32
|
+
end
|
33
|
+
|
34
|
+
def map_attribute(resource, mapped_to)
|
35
|
+
case mapped_to
|
36
|
+
when Symbol then resource[mapped_to]
|
37
|
+
when Proc then mapped_to[resource]
|
38
|
+
when Array then mapped_to.inject({}) { |m, _element| m.tap { |_m| _m[_element] = map_attribute(resource, _element) } }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def call(resource)
|
43
|
+
map_attributes(resource)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Intermodal
|
2
|
+
module Mapping
|
3
|
+
class Presenter < Mapper
|
4
|
+
|
5
|
+
self._mapping_strategy = INCLUDE_NILS
|
6
|
+
|
7
|
+
class << self
|
8
|
+
alias exclude_from_presentation exclude_properties
|
9
|
+
|
10
|
+
# Convenience alias for maps
|
11
|
+
# Examples:
|
12
|
+
#
|
13
|
+
# presents :name
|
14
|
+
# presents :name, :as => lambda { |o| o.name.camelize }
|
15
|
+
# presents :name, :as => [ :first_name, :last_name, :suffix, :prefix ]
|
16
|
+
#
|
17
|
+
alias presents maps
|
18
|
+
|
19
|
+
def model_name(resource)
|
20
|
+
resource.class.name.demodulize.underscore
|
21
|
+
end
|
22
|
+
|
23
|
+
def call(resource)
|
24
|
+
{ model_name(resource) => map_attributes(resource) }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: intermodal
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Ho-Sheng Hsiao
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-06-28 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Declarative DSL for top-level, nested, linked CRUD resource endpoints; DSL for Presenters and Acceptors; API Versioning
|
23
|
+
email: hosh@sparkfly.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- LICENSE
|
30
|
+
- README
|
31
|
+
files:
|
32
|
+
- LICENSE
|
33
|
+
- README
|
34
|
+
- Rakefile
|
35
|
+
- VERSION
|
36
|
+
- lib/intermodal/base.rb
|
37
|
+
- lib/intermodal/declare_controllers.rb
|
38
|
+
- lib/intermodal/mapping.rb
|
39
|
+
- lib/intermodal/mapping/acceptor.rb
|
40
|
+
- lib/intermodal/mapping/dsl.rb
|
41
|
+
- lib/intermodal/mapping/mapper.rb
|
42
|
+
- lib/intermodal/mapping/presenter.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://github.com/hosh/intermodal
|
45
|
+
licenses: []
|
46
|
+
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
hash: 3
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.3.7
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Intermodal lets you quickly put together a pure, JSON/XML-only RESTful web service.
|
77
|
+
test_files: []
|
78
|
+
|