raisin 0.0.5 → 0.1.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 +7 -0
- data/lib/raisin/base.rb +1 -23
- data/lib/raisin/configuration.rb +0 -9
- data/lib/raisin/middleware.rb +4 -0
- data/lib/raisin/rails/routes.rb +26 -31
- data/lib/raisin/railtie.rb +0 -3
- data/lib/raisin/routing.rb +14 -0
- data/lib/raisin/version.rb +1 -1
- data/lib/raisin.rb +0 -9
- metadata +8 -24
- data/lib/raisin/api/dsl.rb +0 -177
- data/lib/raisin/api.rb +0 -74
- data/lib/raisin/endpoint.rb +0 -39
- data/lib/raisin/exposable.rb +0 -21
- data/lib/raisin/mixin.rb +0 -53
- data/lib/raisin/namespace.rb +0 -27
- data/lib/raisin/router.rb +0 -55
- data/lib/raisin/testing/rspec/api_helper.rb +0 -12
- data/lib/raisin/testing/rspec/dsl.rb +0 -97
- data/lib/raisin/testing/rspec/functional_test.rb +0 -28
- data/lib/raisin/testing/rspec/test_request.rb +0 -21
- data/lib/raisin/testing/rspec/unit_helper.rb +0 -34
- data/lib/raisin/testing/rspec/unit_test.rb +0 -21
- data/lib/raisin/testing/rspec.rb +0 -10
- data/lib/raisin/version_constraint.rb +0 -12
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9b3878cbd61503984b0b018f8d6cff02f14ff59b
|
4
|
+
data.tar.gz: d9f5b25e36940ffc6527b60d72a691310d51ee5e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8cf72509e66b0dbc41fd4bb498c1bc066cc3d88864832a752c29db21630f2c84fcc7eab83234bf3342e67eb1cbb7f2f14d02c12ca1955690147f0a43a9406d97
|
7
|
+
data.tar.gz: 48bfaca74a995b61a8baaa1fbda7b7018508c2ed47affd110895bc2e8d134ebeac6e55617717d2f288eee6ac40678cb9a9e5b1a5dd40ac7664ac3575206cd9fd
|
data/lib/raisin/base.rb
CHANGED
@@ -39,35 +39,13 @@ module Raisin
|
|
39
39
|
AbstractController::Callbacks,
|
40
40
|
ActionController::Rescue,
|
41
41
|
|
42
|
-
ActionController::Instrumentation
|
42
|
+
ActionController::Instrumentation,
|
43
43
|
]
|
44
44
|
|
45
45
|
MODULES.each { |mod|
|
46
46
|
include mod
|
47
47
|
}
|
48
48
|
|
49
|
-
def self.controller_path
|
50
|
-
@controller_path ||= name && name.sub(/\:\:[^\:]+$/, '').sub(/api$/i, '').underscore
|
51
|
-
end
|
52
|
-
|
53
|
-
def _prefixes
|
54
|
-
@_prefixes ||= begin
|
55
|
-
parent_prefixes = self.class.parent_prefixes
|
56
|
-
parent_prefixes.compact.unshift(controller_path)#.map! { |pr| pr.split('/').last }
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def action_name
|
61
|
-
self.class.name.demodulize.underscore
|
62
|
-
end
|
63
|
-
|
64
|
-
#
|
65
|
-
# In test env, action is not :call. This is a bit of a hack
|
66
|
-
#
|
67
|
-
def process(action, *args)
|
68
|
-
super(:call, *args)
|
69
|
-
end
|
70
|
-
|
71
49
|
ActiveSupport.run_load_hooks(:action_controller, self)
|
72
50
|
end
|
73
51
|
end
|
data/lib/raisin/configuration.rb
CHANGED
@@ -9,15 +9,6 @@ module Raisin
|
|
9
9
|
end
|
10
10
|
|
11
11
|
module Configuration
|
12
|
-
mattr_accessor :enable_auth_by_default
|
13
|
-
@@enable_auth_by_default = false
|
14
|
-
|
15
|
-
mattr_accessor :default_auth_method
|
16
|
-
@@default_auth_method = :authenticate_user! # Devise FTW
|
17
|
-
|
18
|
-
mattr_accessor :response_formats
|
19
|
-
@@response_formats = [:json]
|
20
|
-
|
21
12
|
def self.version
|
22
13
|
@version_config ||= VersionConfig.new
|
23
14
|
end
|
data/lib/raisin/middleware.rb
CHANGED
data/lib/raisin/rails/routes.rb
CHANGED
@@ -1,42 +1,37 @@
|
|
1
|
-
require 'raisin/
|
1
|
+
require 'raisin/routing'
|
2
2
|
|
3
3
|
module ActionDispatch::Routing
|
4
4
|
class Mapper
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
send(:constraints, Raisin::VersionConstraint.new(version)) {
|
14
|
-
routes.each do |method, path, endpoint|
|
15
|
-
send(method, path, to: endpoint)
|
16
|
-
end
|
17
|
-
}
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
6
|
+
def api(options, &block)
|
7
|
+
return unless block_given?
|
8
|
+
|
9
|
+
version = options[:version].to_s
|
10
|
+
is_default = options.fetch(:default, false)
|
11
|
+
|
12
|
+
raise 'Version is missing in constraint' if version.blank?
|
21
13
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
#
|
28
|
-
def controller_reference_with_api(controller_param)
|
29
|
-
controller_name = "#{controller_param.camelize}Api"
|
30
|
-
unless controller = @controllers[controller_param]
|
31
|
-
controller = @controllers[controller_param] =
|
32
|
-
ActiveSupport::Dependencies.reference(controller_name)
|
14
|
+
@api_resources = true
|
15
|
+
|
16
|
+
send(:constraints, Raisin::Routing::VersionConstraint.new(version, is_default)) do
|
17
|
+
send(:scope, module: version) do
|
18
|
+
yield
|
33
19
|
end
|
34
|
-
controller.get(controller_name)
|
35
|
-
rescue NameError
|
36
|
-
controller_reference_without_api(controller_param)
|
37
20
|
end
|
21
|
+
ensure
|
22
|
+
@api_resources = nil
|
23
|
+
end
|
38
24
|
|
39
|
-
|
25
|
+
def resources(*resources, &block)
|
26
|
+
if @api_resources
|
27
|
+
options = resources.extract_options!
|
28
|
+
options[:except] ||= []
|
29
|
+
options[:except].concat([:new, :edit])
|
30
|
+
super(*resources, options)
|
31
|
+
else
|
32
|
+
super
|
33
|
+
end
|
40
34
|
end
|
35
|
+
|
41
36
|
end
|
42
37
|
end
|
data/lib/raisin/railtie.rb
CHANGED
@@ -0,0 +1,14 @@
|
|
1
|
+
module Raisin
|
2
|
+
module Routing
|
3
|
+
class VersionConstraint
|
4
|
+
def initialize(version, is_default)
|
5
|
+
@version = version
|
6
|
+
@bypass = is_default || version == ALL_VERSIONS
|
7
|
+
end
|
8
|
+
|
9
|
+
def matches?(req)
|
10
|
+
@bypass || @version == req.env['raisin.version']
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/raisin/version.rb
CHANGED
data/lib/raisin.rb
CHANGED
@@ -1,17 +1,8 @@
|
|
1
1
|
require 'raisin/version'
|
2
|
-
|
3
2
|
require 'raisin/configuration'
|
4
3
|
|
5
|
-
require 'raisin/exposable'
|
6
|
-
require 'raisin/namespace'
|
7
|
-
require 'raisin/mixin'
|
8
|
-
|
9
|
-
require 'raisin/router'
|
10
4
|
require 'raisin/base'
|
11
|
-
require 'raisin/api'
|
12
|
-
|
13
5
|
require 'raisin/middleware'
|
14
|
-
require 'raisin/version_constraint'
|
15
6
|
|
16
7
|
module Raisin
|
17
8
|
def self.configure
|
metadata
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: raisin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- ccocchi
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-
|
11
|
+
date: 2013-04-23 00:00:00.000000000 Z
|
13
12
|
dependencies: []
|
14
13
|
description: An opiniated micro-framework to easily build elegant API on top of Rails
|
15
14
|
email:
|
@@ -24,51 +23,36 @@ files:
|
|
24
23
|
- README.md
|
25
24
|
- Rakefile
|
26
25
|
- lib/raisin.rb
|
27
|
-
- lib/raisin/api.rb
|
28
|
-
- lib/raisin/api/dsl.rb
|
29
26
|
- lib/raisin/base.rb
|
30
27
|
- lib/raisin/configuration.rb
|
31
|
-
- lib/raisin/endpoint.rb
|
32
|
-
- lib/raisin/exposable.rb
|
33
28
|
- lib/raisin/middleware.rb
|
34
|
-
- lib/raisin/mixin.rb
|
35
|
-
- lib/raisin/namespace.rb
|
36
29
|
- lib/raisin/rails/request.rb
|
37
30
|
- lib/raisin/rails/routes.rb
|
38
31
|
- lib/raisin/railtie.rb
|
39
|
-
- lib/raisin/
|
40
|
-
- lib/raisin/testing/rspec.rb
|
41
|
-
- lib/raisin/testing/rspec/api_helper.rb
|
42
|
-
- lib/raisin/testing/rspec/dsl.rb
|
43
|
-
- lib/raisin/testing/rspec/functional_test.rb
|
44
|
-
- lib/raisin/testing/rspec/test_request.rb
|
45
|
-
- lib/raisin/testing/rspec/unit_helper.rb
|
46
|
-
- lib/raisin/testing/rspec/unit_test.rb
|
32
|
+
- lib/raisin/routing.rb
|
47
33
|
- lib/raisin/version.rb
|
48
|
-
- lib/raisin/version_constraint.rb
|
49
34
|
- raisin.gemspec
|
50
35
|
homepage: https://github.com/ccocchi/raisin
|
51
36
|
licenses: []
|
37
|
+
metadata: {}
|
52
38
|
post_install_message:
|
53
39
|
rdoc_options: []
|
54
40
|
require_paths:
|
55
41
|
- lib
|
56
42
|
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
43
|
requirements:
|
59
|
-
- -
|
44
|
+
- - '>='
|
60
45
|
- !ruby/object:Gem::Version
|
61
46
|
version: '0'
|
62
47
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
-
none: false
|
64
48
|
requirements:
|
65
|
-
- -
|
49
|
+
- - '>='
|
66
50
|
- !ruby/object:Gem::Version
|
67
51
|
version: '0'
|
68
52
|
requirements: []
|
69
53
|
rubyforge_project:
|
70
|
-
rubygems_version:
|
54
|
+
rubygems_version: 2.0.0
|
71
55
|
signing_key:
|
72
|
-
specification_version:
|
56
|
+
specification_version: 4
|
73
57
|
summary: An opiniated micro-framework to easily build elegant API on top of Rails
|
74
58
|
test_files: []
|
data/lib/raisin/api/dsl.rb
DELETED
@@ -1,177 +0,0 @@
|
|
1
|
-
module Raisin
|
2
|
-
module DSL
|
3
|
-
%w(get head post put delete).each do |via|
|
4
|
-
class_eval <<-EOF, __FILE__, __LINE__ + 1
|
5
|
-
def #{via}(path = '/', options = {}, &block)
|
6
|
-
path = normalize_path(path)
|
7
|
-
method_name = options.key?(:as) ? options[:as].to_s : extract_method_name(path, :#{via})
|
8
|
-
|
9
|
-
klass = self.const_set method_name.camelize, Class.new(@_klass, &block)
|
10
|
-
|
11
|
-
if current_namespace && current_namespace.expose?
|
12
|
-
current_namespace.exposures.each do |name, b|
|
13
|
-
klass.send(:expose, name, &b)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
current_namespace.add(method_name) if current_namespace
|
18
|
-
|
19
|
-
routes << [:#{via}, path, default_route(method_name)]
|
20
|
-
end
|
21
|
-
EOF
|
22
|
-
end
|
23
|
-
|
24
|
-
def included(&block)
|
25
|
-
self.action_klass.class_eval(&block) if block_given?
|
26
|
-
end
|
27
|
-
|
28
|
-
def member(&block)
|
29
|
-
namespace(':id') do
|
30
|
-
resource = self.api_name.singularize
|
31
|
-
expose(resource) { resource.camelize.constantize.send :find, params[:id] }
|
32
|
-
instance_eval(&block)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def nested_into_resource(parent)
|
37
|
-
parent = parent.to_s
|
38
|
-
sing = parent.singularize
|
39
|
-
id = "#{sing}_id"
|
40
|
-
|
41
|
-
@_namespaces << Namespace.new("#{parent}/:#{id}")
|
42
|
-
current_namespace.expose(sing) { sing.camelize.constantize.send :find, params[id.to_sym]}
|
43
|
-
@_namespaces << Namespace.new(@_prefix)
|
44
|
-
@_prefix = nil
|
45
|
-
end
|
46
|
-
|
47
|
-
def single_resource
|
48
|
-
@_single_resource = true
|
49
|
-
@_prefix = @_prefix.singularize if prefix?
|
50
|
-
end
|
51
|
-
|
52
|
-
def prefix(prefix)
|
53
|
-
@_prefix = prefix
|
54
|
-
end
|
55
|
-
|
56
|
-
def description(desc)
|
57
|
-
# noop
|
58
|
-
end
|
59
|
-
|
60
|
-
def expose(*args, &block)
|
61
|
-
current_namespace.expose(*args, &block)
|
62
|
-
end
|
63
|
-
|
64
|
-
def namespace(path, &block)
|
65
|
-
path = path.sub(%r(\A/?#{@_prefix}), '') if prefix?
|
66
|
-
@_namespaces.push Namespace.new(path)
|
67
|
-
yield
|
68
|
-
process_filters
|
69
|
-
@_namespaces.pop
|
70
|
-
end
|
71
|
-
|
72
|
-
%w(before around after).each do |type|
|
73
|
-
class_eval <<-EOF, __FILE__, __LINE__ + 1
|
74
|
-
def #{type}(*args, &block)
|
75
|
-
return unless current_namespace
|
76
|
-
current_namespace.filter(:#{type}, args, &block)
|
77
|
-
end
|
78
|
-
EOF
|
79
|
-
end
|
80
|
-
|
81
|
-
protected
|
82
|
-
|
83
|
-
def prefix?
|
84
|
-
!!@_prefix
|
85
|
-
end
|
86
|
-
|
87
|
-
def single_resource?
|
88
|
-
!!@_single_resource
|
89
|
-
end
|
90
|
-
|
91
|
-
def current_namespace
|
92
|
-
@_namespaces.at(0)
|
93
|
-
end
|
94
|
-
|
95
|
-
def current_namespace?
|
96
|
-
@_namespaces.length > 0
|
97
|
-
end
|
98
|
-
|
99
|
-
def process_filters
|
100
|
-
current_namespace.filters.each_pair { |type, filters|
|
101
|
-
filters.each do |name, block|
|
102
|
-
superclass.send("#{type}_filter", name, only: current_namespace.methods, &block)
|
103
|
-
end
|
104
|
-
}
|
105
|
-
end
|
106
|
-
|
107
|
-
def default_route(method)
|
108
|
-
"#{modules_prefix}#{self.api_name}##{method}"
|
109
|
-
end
|
110
|
-
|
111
|
-
def modules_prefix
|
112
|
-
@modules_prefix ||= begin
|
113
|
-
modules = self.name.split('::').slice(0..-2)
|
114
|
-
modules.empty? ? '' : "#{modules.map!(&:downcase).join('/')}/"
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
#
|
119
|
-
# Get method name from path
|
120
|
-
# Example:
|
121
|
-
# / => :index
|
122
|
-
# /users/:id => :users
|
123
|
-
# /users/:id/addresses => :addresses
|
124
|
-
#
|
125
|
-
def extract_method_name(path, via)
|
126
|
-
return method_name_for_single_resource(path, via) if single_resource?
|
127
|
-
|
128
|
-
if path =~ %r(\A/?#{@_prefix}\z)
|
129
|
-
return via == :get ? 'index' : 'create'
|
130
|
-
end
|
131
|
-
|
132
|
-
parts = path.split('/').reverse!
|
133
|
-
|
134
|
-
return parts.find { |part| !part.start_with?(':') } if parts.first != ':id'
|
135
|
-
|
136
|
-
case via
|
137
|
-
when :get
|
138
|
-
'show'
|
139
|
-
when :put
|
140
|
-
'update'
|
141
|
-
when :delete
|
142
|
-
'destroy'
|
143
|
-
else
|
144
|
-
raise "Cannot extract method name from #{path}"
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
def method_name_for_single_resource(path, via)
|
149
|
-
if path =~ %r(\A/?#{@_prefix}\z)
|
150
|
-
case via
|
151
|
-
when :get
|
152
|
-
'show'
|
153
|
-
when :post
|
154
|
-
'create'
|
155
|
-
when :put
|
156
|
-
'update'
|
157
|
-
when :delete
|
158
|
-
'destroy'
|
159
|
-
end
|
160
|
-
else
|
161
|
-
path.split('/').reverse!.last
|
162
|
-
end
|
163
|
-
end
|
164
|
-
|
165
|
-
#
|
166
|
-
# Creates path with version, namespace and
|
167
|
-
# given path, then normalizes it
|
168
|
-
#
|
169
|
-
def normalize_path(path)
|
170
|
-
parts = []
|
171
|
-
parts << @_prefix unless !prefix? || path =~ %r(\A/?#{@_prefix})
|
172
|
-
parts.concat @_namespaces.reject { |n| path =~ %r(/#{n.path}) }.map!(&:path) if current_namespace?
|
173
|
-
parts << path.to_s unless path == '/'
|
174
|
-
parts.join('/')
|
175
|
-
end
|
176
|
-
end
|
177
|
-
end
|
data/lib/raisin/api.rb
DELETED
@@ -1,74 +0,0 @@
|
|
1
|
-
require 'raisin/api/dsl'
|
2
|
-
|
3
|
-
module Raisin
|
4
|
-
class API
|
5
|
-
|
6
|
-
#
|
7
|
-
# Returns a proc representing our action to be called in the given
|
8
|
-
# environment.
|
9
|
-
#
|
10
|
-
def self.action(name, klass = ActionDispatch::Request)
|
11
|
-
->(env) { self.const_get(name.camelize).new.dispatch(:call, klass.new(env)) }
|
12
|
-
end
|
13
|
-
|
14
|
-
#
|
15
|
-
# The abstract class that is inherited by all actions.
|
16
|
-
#
|
17
|
-
# It includes actions DSL, default call method, global authentication
|
18
|
-
# filter and global respond_to
|
19
|
-
#
|
20
|
-
def self.action_klass
|
21
|
-
@_klass ||= begin
|
22
|
-
klass = Class.new(::Raisin::Base)
|
23
|
-
klass.send(:include, Raisin::Mixin)
|
24
|
-
|
25
|
-
if Configuration.enable_auth_by_default && Configuration.default_auth_method
|
26
|
-
klass.send(:before_filter, Configuration.default_auth_method)
|
27
|
-
end
|
28
|
-
|
29
|
-
klass.send(:respond_to, *Configuration.response_formats)
|
30
|
-
klass
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def self.action_klass=(klass) # :nodoc:
|
35
|
-
@_klass = klass
|
36
|
-
end
|
37
|
-
|
38
|
-
#
|
39
|
-
# Resets routes and namespaces.
|
40
|
-
# Sets default prefix to api_name (e.g PostsAPI => 'api')
|
41
|
-
#
|
42
|
-
def self.reset
|
43
|
-
@_routes = []
|
44
|
-
@_prefix = self.api_name
|
45
|
-
@_namespaces = []
|
46
|
-
@_single_resource = false
|
47
|
-
end
|
48
|
-
|
49
|
-
#
|
50
|
-
# Resets routes and namespaces for each new API class.
|
51
|
-
# The action class is copied for some reasons (??)
|
52
|
-
#
|
53
|
-
def self.inherited(subclass)
|
54
|
-
super
|
55
|
-
subclass.reset
|
56
|
-
subclass.action_klass = self.action_klass.dup
|
57
|
-
end
|
58
|
-
|
59
|
-
#
|
60
|
-
# Returns the last part of the api's name, underscored, without the ending
|
61
|
-
# <tt>API</tt>. For instance, PostsAPI returns <tt>posts</tt>.
|
62
|
-
# Namespaces are left out, so Admin::PostsAPI returns <tt>posts</tt> as well.
|
63
|
-
#
|
64
|
-
def self.api_name
|
65
|
-
@api_name ||= self.name.demodulize.sub(/api/i, '').underscore
|
66
|
-
end
|
67
|
-
|
68
|
-
def self.routes # :nodoc:
|
69
|
-
@_routes
|
70
|
-
end
|
71
|
-
|
72
|
-
extend Raisin::DSL
|
73
|
-
end
|
74
|
-
end
|
data/lib/raisin/endpoint.rb
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
module Raisin
|
2
|
-
class Endpoint
|
3
|
-
include Exposable
|
4
|
-
|
5
|
-
attr_reader :response_body, :auth_method, :formats
|
6
|
-
|
7
|
-
def initialize
|
8
|
-
@response_body = nil
|
9
|
-
@auth_method = nil
|
10
|
-
@formats = []
|
11
|
-
end
|
12
|
-
|
13
|
-
def response(&block)
|
14
|
-
@response_body = block
|
15
|
-
end
|
16
|
-
|
17
|
-
def has_response?
|
18
|
-
!!response_body
|
19
|
-
end
|
20
|
-
|
21
|
-
def desc(description)
|
22
|
-
# noop
|
23
|
-
end
|
24
|
-
|
25
|
-
def format(*mime_types)
|
26
|
-
@formats.concat mime_types
|
27
|
-
end
|
28
|
-
|
29
|
-
def enable_auth(method = Configuration.default_auth_method)
|
30
|
-
return if Configuration.enable_auth_by_default
|
31
|
-
@auth_method = method
|
32
|
-
end
|
33
|
-
|
34
|
-
def skip_auth(method = Configuration.default_auth_method)
|
35
|
-
return unless Configuration.enable_auth_by_default
|
36
|
-
@auth_method = method
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
data/lib/raisin/exposable.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
module Raisin
|
2
|
-
module Exposable
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
included do
|
6
|
-
attr_reader :exposures
|
7
|
-
end
|
8
|
-
|
9
|
-
def initialize(*args)
|
10
|
-
@exposures = []
|
11
|
-
end
|
12
|
-
|
13
|
-
def expose(name, &block)
|
14
|
-
@exposures << [name, block]
|
15
|
-
end
|
16
|
-
|
17
|
-
def expose?
|
18
|
-
!exposures.empty?
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
data/lib/raisin/mixin.rb
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
module Raisin
|
2
|
-
module Mixin
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
def call
|
6
|
-
end
|
7
|
-
|
8
|
-
module ClassMethods
|
9
|
-
def response(&block)
|
10
|
-
define_method(:call, &block) if block_given?
|
11
|
-
end
|
12
|
-
|
13
|
-
def desc(description)
|
14
|
-
# noop
|
15
|
-
end
|
16
|
-
alias_method :description, :desc
|
17
|
-
|
18
|
-
def format(*args)
|
19
|
-
self.class_eval <<-EOF, __FILE__, __LINE__ + 1
|
20
|
-
respond_to(*#{args})
|
21
|
-
EOF
|
22
|
-
end
|
23
|
-
|
24
|
-
def enable_auth(method = nil)
|
25
|
-
method ||= Configuration.default_auth_method
|
26
|
-
send(:before_filter, method) unless Configuration.enable_auth_by_default
|
27
|
-
end
|
28
|
-
|
29
|
-
def disable_auth(method = nil)
|
30
|
-
method ||= Configuration.default_auth_method
|
31
|
-
send(:skip_before_filter, method) if Configuration.enable_auth_by_default
|
32
|
-
end
|
33
|
-
|
34
|
-
def expose(name, &block)
|
35
|
-
if block_given?
|
36
|
-
define_method(name) do |*args|
|
37
|
-
ivar = "@#{name}"
|
38
|
-
|
39
|
-
if instance_variable_defined?(ivar)
|
40
|
-
instance_variable_get(ivar)
|
41
|
-
else
|
42
|
-
instance_variable_set(ivar, instance_exec(block, *args, &block))
|
43
|
-
end
|
44
|
-
end
|
45
|
-
else
|
46
|
-
attr_reader name
|
47
|
-
end
|
48
|
-
|
49
|
-
helper_method name
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
data/lib/raisin/namespace.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
module Raisin
|
2
|
-
class Namespace
|
3
|
-
include Exposable
|
4
|
-
|
5
|
-
attr_reader :path, :methods, :filters
|
6
|
-
|
7
|
-
def initialize(path)
|
8
|
-
super
|
9
|
-
|
10
|
-
@path = path
|
11
|
-
@methods = []
|
12
|
-
@filters = {
|
13
|
-
before: [],
|
14
|
-
after: [],
|
15
|
-
around: []
|
16
|
-
}
|
17
|
-
end
|
18
|
-
|
19
|
-
def add(method)
|
20
|
-
@methods << method
|
21
|
-
end
|
22
|
-
|
23
|
-
def filter(type, *args, &block)
|
24
|
-
@filters[type] << [args.first, block]
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
data/lib/raisin/router.rb
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
module Raisin
|
2
|
-
class Router
|
3
|
-
ALL_VERSIONS = :all
|
4
|
-
|
5
|
-
#
|
6
|
-
# Reset class variables on the subclass when inherited
|
7
|
-
#
|
8
|
-
def self.inherited(subclass)
|
9
|
-
subclass.reset
|
10
|
-
end
|
11
|
-
|
12
|
-
def self.reset
|
13
|
-
@_current_version = nil
|
14
|
-
@_versions = { ALL_VERSIONS => [] }
|
15
|
-
end
|
16
|
-
|
17
|
-
class << self
|
18
|
-
attr_internal_accessor :versions, :current_version
|
19
|
-
|
20
|
-
def mount(api)
|
21
|
-
if version?(:header)
|
22
|
-
@_versions[self.current_version].concat(api.routes)
|
23
|
-
else
|
24
|
-
@_versions[ALL_VERSIONS].concat(version?(:path) ? pathed_routes(api.routes) : api.routes)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
#
|
29
|
-
# Set version for current block
|
30
|
-
#
|
31
|
-
def version(version, options = {}, &block)
|
32
|
-
version = version.to_s
|
33
|
-
self.current_version = version
|
34
|
-
@_versions[version] = [] if version?(:header)
|
35
|
-
yield
|
36
|
-
self.current_version = nil
|
37
|
-
end
|
38
|
-
|
39
|
-
private
|
40
|
-
|
41
|
-
def version?(type)
|
42
|
-
self.current_version && Configuration.version.using == type
|
43
|
-
end
|
44
|
-
|
45
|
-
def pathed_routes(routes)
|
46
|
-
self.routes.map! { |via, path, opts|
|
47
|
-
path.append('/') unless path.start_with?('/')
|
48
|
-
path.append(current_version.version)
|
49
|
-
[via, path, opts]
|
50
|
-
}
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
end
|
55
|
-
end
|
@@ -1,97 +0,0 @@
|
|
1
|
-
require 'raisin/testing/rspec/unit_helper'
|
2
|
-
require 'raisin/testing/rspec/api_helper'
|
3
|
-
|
4
|
-
module Raisin
|
5
|
-
module Testing
|
6
|
-
module RSpec
|
7
|
-
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
module HttpMethods
|
12
|
-
%w(get head post put delete).each do |verb|
|
13
|
-
class_eval <<-EOF, __FILE__, __LINE__ + 1
|
14
|
-
def #{verb}(*args)
|
15
|
-
method = self.example.metadata[:current_method].to_s.downcase
|
16
|
-
super(method, *args)
|
17
|
-
end
|
18
|
-
EOF
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
module DSL
|
26
|
-
%w(get head post put delete).each do |verb|
|
27
|
-
class_eval <<-EOF, __FILE__, __LINE__ + 1
|
28
|
-
def #{verb}(path_or_klass, &block)
|
29
|
-
self.metadata[:current_klass] = _klass_from_path(path_or_klass, :#{verb})
|
30
|
-
self.instance_eval(&block)
|
31
|
-
self.metadata.delete(:current_klass)
|
32
|
-
end
|
33
|
-
EOF
|
34
|
-
end
|
35
|
-
|
36
|
-
def unit(&block)
|
37
|
-
_describe_controller(self.metadata[:current_klass], self, &block).tap do |klass|
|
38
|
-
klass.send(:include, Raisin::RSpecUnitHelper)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def response(format = :json, &block)
|
43
|
-
self.metadata[:type] = :api
|
44
|
-
_describe_controller(self.metadata[:current_klass], self, &block).tap do |klass|
|
45
|
-
klass.send(:include, Raisin::RSpecApiHelper)
|
46
|
-
klass.send(:include, HttpMethods)
|
47
|
-
klass.send(:render_views) if ::Raisin::Configuration.testing_render_views
|
48
|
-
klass.send(:before) {
|
49
|
-
request.accept = format.is_a?(Symbol) ? "application/#{format}" : format
|
50
|
-
}
|
51
|
-
self.metadata.delete(:type)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
protected
|
56
|
-
|
57
|
-
def _klass_from_path(path_or_klass, via)
|
58
|
-
api_klass = _find_first_api_klass
|
59
|
-
|
60
|
-
case path_or_klass
|
61
|
-
when String
|
62
|
-
method_name = api_klass.send(:extract_method_name, api_klass.send(:normalize_path, path_or_klass), via)
|
63
|
-
self.metadata[:current_method] = method_name
|
64
|
-
api_klass.const_get(method_name.camelize)
|
65
|
-
when Symbol
|
66
|
-
self.metadata[:current_method] = path_or_klass
|
67
|
-
api_klass.const_get(path_or_klass.to_s.camelize)
|
68
|
-
else
|
69
|
-
path_or_klass
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
def _find_first_api_klass
|
74
|
-
metadata = self.metadata[:example_group]
|
75
|
-
klass = nil
|
76
|
-
|
77
|
-
until metadata.nil? || klass.respond_to?(:new)
|
78
|
-
klass = metadata[:description_args].first
|
79
|
-
metadata = metadata[:example_group]
|
80
|
-
end
|
81
|
-
|
82
|
-
klass
|
83
|
-
end
|
84
|
-
|
85
|
-
def _describe_controller(klass, parent, &block)
|
86
|
-
result = parent.describe(klass, &block)
|
87
|
-
result.instance_eval <<-EOF
|
88
|
-
def controller_class
|
89
|
-
#{klass}
|
90
|
-
end
|
91
|
-
EOF
|
92
|
-
result
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
require 'raisin/testing/rspec/test_request'
|
2
|
-
|
3
|
-
module Raisin
|
4
|
-
module FunctionalTest
|
5
|
-
def self.append_features(base)
|
6
|
-
base.class_eval do
|
7
|
-
include RSpec::Rails::ControllerExampleGroup
|
8
|
-
extend ClassMethods
|
9
|
-
end
|
10
|
-
|
11
|
-
super
|
12
|
-
end
|
13
|
-
|
14
|
-
module ClassMethods
|
15
|
-
def controller_class
|
16
|
-
metadata = self.metadata[:example_group]
|
17
|
-
klass = nil
|
18
|
-
|
19
|
-
until metadata.nil? || klass.respond_to?(:new)
|
20
|
-
klass = metadata[:description_args].first
|
21
|
-
metadata = metadata[:example_group]
|
22
|
-
end
|
23
|
-
|
24
|
-
klass.respond_to?(:new) ? klass : super
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module ActionController
|
2
|
-
class TestRequest < ActionDispatch::TestRequest
|
3
|
-
|
4
|
-
#
|
5
|
-
# Transforms Raisin controller's path as a standart Rails path
|
6
|
-
# before assigning parameters to the request
|
7
|
-
#
|
8
|
-
# Example:
|
9
|
-
# controller_path: 'users_api#index', action: 'index'
|
10
|
-
# becomes
|
11
|
-
# controller_path: 'users', action: 'index'
|
12
|
-
#
|
13
|
-
def assign_parameters_with_api(routes, controller_path, action, parameters = {})
|
14
|
-
controller_path.sub!(/_api/, '')
|
15
|
-
controller_path.sub!(/\/?#{action}/, '')
|
16
|
-
assign_parameters_without_api(routes, controller_path, action, parameters)
|
17
|
-
end
|
18
|
-
|
19
|
-
alias_method_chain :assign_parameters, :api
|
20
|
-
end
|
21
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
module Raisin
|
2
|
-
module UnitHelper
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
module ClassMethods
|
6
|
-
def controller_class
|
7
|
-
metadata = self.metadata[:example_group]
|
8
|
-
klass = nil
|
9
|
-
|
10
|
-
until metadata.nil? || klass.respond_to?(:new)
|
11
|
-
klass = metadata[:description_args].first
|
12
|
-
metadata = metadata[:example_group]
|
13
|
-
end
|
14
|
-
|
15
|
-
klass.respond_to?(:new) ? klass : super
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def controller
|
20
|
-
self.class.controller_class.new.tap do |c|
|
21
|
-
c.request = request
|
22
|
-
c.response = response
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def request
|
27
|
-
@request ||= ActionDispatch::TestRequest.new
|
28
|
-
end
|
29
|
-
|
30
|
-
def response
|
31
|
-
@response ||= ActionDispatch::TestResponse.new
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
require 'raisin/testing/rspec/unit_helper'
|
2
|
-
|
3
|
-
module Raisin
|
4
|
-
module UnitTest
|
5
|
-
if defined?(RSpec::Rails)
|
6
|
-
include RSpec::Rails::RailsExampleGroup
|
7
|
-
include RSpec::Rails::Matchers::RedirectTo
|
8
|
-
include RSpec::Rails::Matchers::RenderTemplate
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.append_features(base)
|
12
|
-
base.class_eval do
|
13
|
-
include Raisin::UnitHelper
|
14
|
-
subject { controller }
|
15
|
-
end
|
16
|
-
|
17
|
-
super
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
|
-
end
|
data/lib/raisin/testing/rspec.rb
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
# require 'rspec'
|
2
|
-
# require 'raisin/testing/rspec/dsl'
|
3
|
-
require 'raisin/testing/rspec/unit_test'
|
4
|
-
require 'raisin/testing/rspec/functional_test'
|
5
|
-
|
6
|
-
# require 'raisin/testing/rspec/helper'
|
7
|
-
|
8
|
-
# RSpec.configure do |c|
|
9
|
-
# c.extend(Raisin::Testing::RSpec::DSL)
|
10
|
-
# end
|