moonrope 1.2.5 → 1.3.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 +4 -4
- data/lib/moonrope/action.rb +29 -29
- data/lib/moonrope/action_result.rb +9 -9
- data/lib/moonrope/base.rb +27 -27
- data/lib/moonrope/before_action.rb +5 -5
- data/lib/moonrope/controller.rb +6 -6
- data/lib/moonrope/dsl/action_dsl.rb +18 -13
- data/lib/moonrope/dsl/base_dsl.rb +7 -7
- data/lib/moonrope/dsl/controller_dsl.rb +8 -8
- data/lib/moonrope/dsl/structure_dsl.rb +23 -14
- data/lib/moonrope/errors.rb +12 -12
- data/lib/moonrope/eval_environment.rb +22 -22
- data/lib/moonrope/eval_helpers.rb +3 -3
- data/lib/moonrope/helper.rb +4 -4
- data/lib/moonrope/param_set.rb +6 -6
- data/lib/moonrope/rack_middleware.rb +16 -16
- data/lib/moonrope/railtie.rb +10 -10
- data/lib/moonrope/request.rb +20 -20
- data/lib/moonrope/structure.rb +28 -28
- data/lib/moonrope/structure_attribute.rb +5 -6
- data/lib/moonrope/version.rb +2 -2
- metadata +2 -2
data/lib/moonrope/request.rb
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
module Moonrope
|
|
2
2
|
class Request
|
|
3
|
-
|
|
3
|
+
|
|
4
4
|
class << self
|
|
5
5
|
attr_accessor :path_regex
|
|
6
|
-
|
|
6
|
+
|
|
7
7
|
# @return [Regex] the regex which should be matched for all API requests
|
|
8
8
|
def path_regex
|
|
9
9
|
@path_regex ||= /\A\/api\/([\w\/\-\.]+)?/
|
|
10
10
|
end
|
|
11
11
|
end
|
|
12
|
-
|
|
12
|
+
|
|
13
13
|
# @return [Hash] the rack environment
|
|
14
14
|
attr_reader :env
|
|
15
15
|
# @return [String] the name of the controller which was requested
|
|
@@ -18,7 +18,7 @@ module Moonrope
|
|
|
18
18
|
attr_reader :action_name
|
|
19
19
|
# @return [Object] the authenticated user
|
|
20
20
|
attr_reader :authenticated_user
|
|
21
|
-
|
|
21
|
+
|
|
22
22
|
#
|
|
23
23
|
# Initialize a new Moonrope::Request
|
|
24
24
|
#
|
|
@@ -34,7 +34,7 @@ module Moonrope
|
|
|
34
34
|
end
|
|
35
35
|
@version, @controller_name, @action_name = path ? path.split("/") : [nil, nil, nil]
|
|
36
36
|
end
|
|
37
|
-
|
|
37
|
+
|
|
38
38
|
#
|
|
39
39
|
# Return the requested API version from the request
|
|
40
40
|
#
|
|
@@ -45,7 +45,7 @@ module Moonrope
|
|
|
45
45
|
version = 1 if version == 0
|
|
46
46
|
version
|
|
47
47
|
end
|
|
48
|
-
|
|
48
|
+
|
|
49
49
|
#
|
|
50
50
|
# Return whether or not this request is valid and can continue?
|
|
51
51
|
#
|
|
@@ -54,16 +54,16 @@ module Moonrope
|
|
|
54
54
|
def valid?
|
|
55
55
|
!!(version > 0 && [controller_name, action_name].all? { |c| c =~ /\A[\w\-\.]+\z/} && controller && action)
|
|
56
56
|
end
|
|
57
|
-
|
|
57
|
+
|
|
58
58
|
#
|
|
59
59
|
# Return the controller object for the request
|
|
60
|
-
#
|
|
60
|
+
#
|
|
61
61
|
# @return [Moonrope::Controller]
|
|
62
62
|
#
|
|
63
63
|
def controller
|
|
64
64
|
@controller ||= @base.controller(controller_name.to_sym)
|
|
65
65
|
end
|
|
66
|
-
|
|
66
|
+
|
|
67
67
|
#
|
|
68
68
|
# Return the action object for the request
|
|
69
69
|
#
|
|
@@ -72,7 +72,7 @@ module Moonrope
|
|
|
72
72
|
def action
|
|
73
73
|
@action ||= controller.actions[action_name.to_sym]
|
|
74
74
|
end
|
|
75
|
-
|
|
75
|
+
|
|
76
76
|
#
|
|
77
77
|
# Execute the appropriate action for the request after running
|
|
78
78
|
# the various authentication checks.
|
|
@@ -84,7 +84,7 @@ module Moonrope
|
|
|
84
84
|
if @base.authenticator
|
|
85
85
|
result = action.convert_errors_to_action_result do
|
|
86
86
|
@authenticated_user = eval_env.instance_eval(&@base.authenticator)
|
|
87
|
-
# If we are authenticated, check whether the action permits access to
|
|
87
|
+
# If we are authenticated, check whether the action permits access to
|
|
88
88
|
# this user, if not raise an error.
|
|
89
89
|
if authenticated?
|
|
90
90
|
unless action.check_access(eval_env) == true
|
|
@@ -92,7 +92,7 @@ module Moonrope
|
|
|
92
92
|
end
|
|
93
93
|
end
|
|
94
94
|
end
|
|
95
|
-
|
|
95
|
+
|
|
96
96
|
if result.is_a?(Moonrope::ActionResult)
|
|
97
97
|
# If we already have a result, we should return it and no longer execute
|
|
98
98
|
# this request.
|
|
@@ -101,7 +101,7 @@ module Moonrope
|
|
|
101
101
|
end
|
|
102
102
|
action.execute(eval_env)
|
|
103
103
|
end
|
|
104
|
-
|
|
104
|
+
|
|
105
105
|
#
|
|
106
106
|
# Return all user supplier parameters
|
|
107
107
|
#
|
|
@@ -110,7 +110,7 @@ module Moonrope
|
|
|
110
110
|
def params
|
|
111
111
|
@params ||= Moonrope::ParamSet.new(rack_request.params['params'])
|
|
112
112
|
end
|
|
113
|
-
|
|
113
|
+
|
|
114
114
|
#
|
|
115
115
|
# Return all HTTP headers from the request
|
|
116
116
|
#
|
|
@@ -119,7 +119,7 @@ module Moonrope
|
|
|
119
119
|
def headers
|
|
120
120
|
@headers ||= self.class.extract_http_request_headers(@env)
|
|
121
121
|
end
|
|
122
|
-
|
|
122
|
+
|
|
123
123
|
#
|
|
124
124
|
# Is this request to the API anonymous?
|
|
125
125
|
#
|
|
@@ -128,7 +128,7 @@ module Moonrope
|
|
|
128
128
|
def anonymous?
|
|
129
129
|
authenticated_user.nil?
|
|
130
130
|
end
|
|
131
|
-
|
|
131
|
+
|
|
132
132
|
#
|
|
133
133
|
# Is this request to the API authenticated?
|
|
134
134
|
#
|
|
@@ -137,9 +137,9 @@ module Moonrope
|
|
|
137
137
|
def authenticated?
|
|
138
138
|
!(authenticated_user.nil? || authenticated_user == false)
|
|
139
139
|
end
|
|
140
|
-
|
|
140
|
+
|
|
141
141
|
private
|
|
142
|
-
|
|
142
|
+
|
|
143
143
|
#
|
|
144
144
|
# Return/create a rack request object for use internally
|
|
145
145
|
#
|
|
@@ -148,7 +148,7 @@ module Moonrope
|
|
|
148
148
|
def rack_request
|
|
149
149
|
@rack_request ||= ::Rack::Request.new(@env)
|
|
150
150
|
end
|
|
151
|
-
|
|
151
|
+
|
|
152
152
|
#
|
|
153
153
|
# Extract headers from the rack env
|
|
154
154
|
#
|
|
@@ -165,6 +165,6 @@ module Moonrope
|
|
|
165
165
|
hash
|
|
166
166
|
end
|
|
167
167
|
end
|
|
168
|
-
|
|
168
|
+
|
|
169
169
|
end
|
|
170
170
|
end
|
data/lib/moonrope/structure.rb
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
module Moonrope
|
|
2
2
|
class Structure
|
|
3
|
-
|
|
3
|
+
|
|
4
4
|
# @return [Symbol] the name of the structure
|
|
5
5
|
attr_accessor :name
|
|
6
|
-
|
|
6
|
+
|
|
7
7
|
# @return [Proc] the basic data block
|
|
8
8
|
attr_accessor :basic
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
# @return [Proc] the full data block
|
|
11
11
|
attr_accessor :full
|
|
12
|
-
|
|
12
|
+
|
|
13
13
|
# @return [Moonrope::DSL::StructureDSL] the DSL
|
|
14
14
|
attr_reader :dsl
|
|
15
|
-
|
|
15
|
+
|
|
16
16
|
# @return [Hash] all expansions for the structure
|
|
17
17
|
attr_reader :expansions
|
|
18
|
-
|
|
18
|
+
|
|
19
19
|
# @return [Moonrope::Base] the base API
|
|
20
20
|
attr_reader :base
|
|
21
|
-
|
|
21
|
+
|
|
22
22
|
# @return [Hash] attributes which should be included in this structure
|
|
23
23
|
attr_reader :attributes
|
|
24
|
-
|
|
24
|
+
|
|
25
25
|
#
|
|
26
26
|
# Initialize a new structure
|
|
27
27
|
#
|
|
@@ -36,7 +36,7 @@ module Moonrope
|
|
|
36
36
|
@dsl = Moonrope::DSL::StructureDSL.new(self)
|
|
37
37
|
@dsl.instance_eval(&block) if block_given?
|
|
38
38
|
end
|
|
39
|
-
|
|
39
|
+
|
|
40
40
|
#
|
|
41
41
|
# Return a hash for this struture
|
|
42
42
|
#
|
|
@@ -47,52 +47,52 @@ module Moonrope
|
|
|
47
47
|
def hash(object, options = {})
|
|
48
48
|
# Set up an environment
|
|
49
49
|
environment = EvalEnvironment.new(base, options[:request], :o => object)
|
|
50
|
-
|
|
50
|
+
|
|
51
51
|
# Set a new hash
|
|
52
52
|
hash = Hash.new
|
|
53
|
-
|
|
53
|
+
|
|
54
54
|
# Add the 'basic' structured fields
|
|
55
55
|
DeepMerge.deep_merge! hash_for_attributes(@attributes[:basic], object, environment), hash
|
|
56
|
-
|
|
56
|
+
|
|
57
57
|
# Always get a basic hash to work from
|
|
58
58
|
if self.basic.is_a?(Proc)
|
|
59
59
|
DeepMerge.deep_merge! environment.instance_eval(&self.basic), hash
|
|
60
60
|
end
|
|
61
|
-
|
|
61
|
+
|
|
62
62
|
# Enhance with the full hash if requested
|
|
63
63
|
if options[:full]
|
|
64
|
-
|
|
64
|
+
|
|
65
65
|
# Add the 'full' structured fields
|
|
66
66
|
DeepMerge.deep_merge! hash_for_attributes(@attributes[:full], object, environment), hash
|
|
67
|
-
|
|
67
|
+
|
|
68
68
|
if self.full.is_a?(Proc)
|
|
69
69
|
full_hash = environment.instance_eval(&self.full)
|
|
70
70
|
DeepMerge.deep_merge! full_hash,hash
|
|
71
71
|
end
|
|
72
72
|
end
|
|
73
|
-
|
|
73
|
+
|
|
74
74
|
# Add expansions
|
|
75
|
-
if options[:expansions]
|
|
76
|
-
|
|
75
|
+
if options[:expansions]
|
|
76
|
+
|
|
77
77
|
# Add structured expansions
|
|
78
78
|
@attributes[:expansion].each do |attribute|
|
|
79
79
|
next if options[:expansions].is_a?(Array) && !options[:expansions].include?(attribute.name.to_sym)
|
|
80
80
|
DeepMerge.deep_merge! hash_for_attributes([attribute], object, environment), hash
|
|
81
81
|
end
|
|
82
|
-
|
|
82
|
+
|
|
83
83
|
# Add the expansions
|
|
84
84
|
expansions.each do |name, expansion|
|
|
85
85
|
next if options[:expansions].is_a?(Array) && !options[:expansions].include?(name.to_sym)
|
|
86
86
|
DeepMerge.deep_merge!({name.to_sym => environment.instance_eval(&expansion)}, hash)
|
|
87
87
|
end
|
|
88
88
|
end
|
|
89
|
-
|
|
89
|
+
|
|
90
90
|
# Return the hash
|
|
91
91
|
hash
|
|
92
92
|
end
|
|
93
|
-
|
|
93
|
+
|
|
94
94
|
private
|
|
95
|
-
|
|
95
|
+
|
|
96
96
|
#
|
|
97
97
|
# Return a returnable hash for a given set of structured fields.
|
|
98
98
|
#
|
|
@@ -100,7 +100,7 @@ module Moonrope
|
|
|
100
100
|
return {} unless attributes.is_a?(Array)
|
|
101
101
|
Hash.new.tap do |hash|
|
|
102
102
|
attributes.each do |attribute|
|
|
103
|
-
|
|
103
|
+
|
|
104
104
|
unless attribute.conditions.empty?
|
|
105
105
|
matched = false
|
|
106
106
|
attribute.conditions.each do |condition|
|
|
@@ -115,7 +115,7 @@ module Moonrope
|
|
|
115
115
|
next
|
|
116
116
|
end
|
|
117
117
|
end
|
|
118
|
-
|
|
118
|
+
|
|
119
119
|
if attribute.value.is_a?(Proc)
|
|
120
120
|
value = environment.instance_eval(&attribute.value)
|
|
121
121
|
elsif attribute.value
|
|
@@ -123,7 +123,7 @@ module Moonrope
|
|
|
123
123
|
else
|
|
124
124
|
value = value_for_attribute(object, environment, attribute)
|
|
125
125
|
end
|
|
126
|
-
|
|
126
|
+
|
|
127
127
|
if attribute.groups.empty?
|
|
128
128
|
hash[attribute.name] = value
|
|
129
129
|
else
|
|
@@ -136,11 +136,11 @@ module Moonrope
|
|
|
136
136
|
last_hash = last_hash[group]
|
|
137
137
|
end
|
|
138
138
|
end
|
|
139
|
-
|
|
139
|
+
|
|
140
140
|
end
|
|
141
141
|
end
|
|
142
142
|
end
|
|
143
|
-
|
|
143
|
+
|
|
144
144
|
#
|
|
145
145
|
# Return a value for a structured field.
|
|
146
146
|
#
|
|
@@ -164,6 +164,6 @@ module Moonrope
|
|
|
164
164
|
value
|
|
165
165
|
end
|
|
166
166
|
end
|
|
167
|
-
|
|
167
|
+
|
|
168
168
|
end
|
|
169
169
|
end
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
module Moonrope
|
|
2
2
|
class StructureAttribute
|
|
3
|
-
|
|
3
|
+
|
|
4
4
|
attr_accessor :name
|
|
5
5
|
attr_accessor :groups
|
|
6
6
|
attr_accessor :conditions
|
|
@@ -11,18 +11,17 @@ module Moonrope
|
|
|
11
11
|
attr_accessor :structure_opts
|
|
12
12
|
attr_accessor :value
|
|
13
13
|
attr_accessor :example
|
|
14
|
-
|
|
15
|
-
def initialize(type, name
|
|
14
|
+
|
|
15
|
+
def initialize(type, name)
|
|
16
16
|
@type = type
|
|
17
17
|
@name = name
|
|
18
|
-
@description = description
|
|
19
18
|
@groups = []
|
|
20
19
|
@conditions = []
|
|
21
20
|
end
|
|
22
|
-
|
|
21
|
+
|
|
23
22
|
def source_attribute
|
|
24
23
|
@source_attribute || @name
|
|
25
24
|
end
|
|
26
|
-
|
|
25
|
+
|
|
27
26
|
end
|
|
28
27
|
end
|
data/lib/moonrope/version.rb
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
module Moonrope
|
|
2
|
-
VERSION = '1.
|
|
3
|
-
end
|
|
2
|
+
VERSION = '1.3.0'
|
|
3
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: moonrope
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Adam Cooke
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2014-
|
|
11
|
+
date: 2014-10-26 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: json
|