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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b3dec6b64d723aeb2e166a548bd1b0979e6f8639
4
- data.tar.gz: 1578b8fcea65a8fa2a8021cf2e8d1ddb3b5db665
3
+ metadata.gz: 1be661422027acf61678bd7b6a73932376b53ec3
4
+ data.tar.gz: 3ea06c9d8c46916c81b833d6c0bd7328e7886580
5
5
  SHA512:
6
- metadata.gz: e78a8573273c0bc8a0f4f892bbef61142c0b391fd01909130952c606cfd11888974569751659ba4d016763e7f143de9f199f22b6a5f01941c4fd4f4816665c5b
7
- data.tar.gz: 5829ec72fbbbcce330cf9666927a270b4351a4c9bf26c2bba5edaecbb0301d3b3d7a452af049cba0a1523593a97cd29655f99f47f43936d7fabdaee5a6903cbf
6
+ metadata.gz: adf31e337346e9f3013a1d89b6583ef1dc87a0fd771c21aaf4c871460e31abdc6f95c839922df56cc8eaaac91dec05b60fe15f3ad99ab0eff86cde9b171155a7
7
+ data.tar.gz: 3256a19d2938b43720bdf9ea95ffac7f5f86704c9acffce1977c7f16591ea0eacfbc1de49b96fa3b5a0782fba49955e52a6407c75da60efbbb71781fdb219791
@@ -1,27 +1,27 @@
1
1
  module Moonrope
2
2
  class Action
3
-
3
+
4
4
  # @return [Moonrope::Controller] the associated controller
5
5
  attr_reader :controller
6
-
6
+
7
7
  # @return [Symbol] the name of the action
8
8
  attr_reader :name
9
-
9
+
10
10
  # @return [Moonrope::DSL::Action] the action's DSL
11
11
  attr_reader :dsl
12
-
12
+
13
13
  # @return [Hash] the params available for the action
14
14
  attr_reader :params
15
-
15
+
16
16
  # @return [String] the description of the action
17
17
  attr_accessor :description
18
-
18
+
19
19
  # @return [Proc] the access check condition for the action
20
20
  attr_accessor :access
21
-
21
+
22
22
  # @return [Proc] the action for the action
23
23
  attr_accessor :action
24
-
24
+
25
25
  #
26
26
  # Initialize a new action
27
27
  #
@@ -36,9 +36,9 @@ module Moonrope
36
36
  @dsl = Moonrope::DSL::ActionDSL.new(self)
37
37
  @dsl.instance_eval(&block) if block_given?
38
38
  end
39
-
39
+
40
40
  #
41
- # Return a hash of all params for this action which are
41
+ # Return a hash of all params for this action which are
42
42
  #
43
43
  # @return [Hash] hash with field names as keys with default values
44
44
  #
@@ -48,7 +48,7 @@ module Moonrope
48
48
  h
49
49
  end
50
50
  end
51
-
51
+
52
52
  #
53
53
  # Execute a block of code and catch approprite Moonrope errors and return
54
54
  # a result.
@@ -74,12 +74,12 @@ module Moonrope
74
74
  end
75
75
  end
76
76
  end
77
-
77
+
78
78
  #
79
79
  # Executes the action and returns a ActionResult object with the result
80
80
  # of the action.
81
81
  #
82
- # @param request [Moonrope::Request or Moonrope::EvalEnvironment]
82
+ # @param request [Moonrope::Request or Moonrope::EvalEnvironment]
83
83
  # @return [Moonrope::ActionResult]
84
84
  #
85
85
  def execute(request = nil)
@@ -88,38 +88,38 @@ module Moonrope
88
88
  else
89
89
  eval_environment = EvalEnvironment.new(@controller.base, request)
90
90
  end
91
-
91
+
92
92
  #
93
93
  # Set this actions default parameters in the eval environment so that
94
94
  # it has access to them.
95
95
  #
96
96
  eval_environment.default_params = self.default_params
97
-
97
+
98
98
  #
99
99
  # Set the current action to the eval environment so it knows what action
100
100
  # invoked this.
101
101
  #
102
102
  eval_environment.action = self
103
-
103
+
104
104
  convert_errors_to_action_result do
105
105
  #
106
106
  # Validate the parameters
107
107
  #
108
108
  self.validate_parameters(eval_environment.params)
109
-
109
+
110
110
  start_time = Time.now
111
-
111
+
112
112
  # Run before filters
113
113
  controller.before_actions_for(name).each do |action|
114
114
  eval_environment.instance_eval(&action.block)
115
115
  end
116
-
116
+
117
117
  # Run the actual action
118
118
  response = eval_environment.instance_eval(&action)
119
-
119
+
120
120
  # Calculate the length of time this request takes
121
121
  time_to_run = Time.now - start_time
122
-
122
+
123
123
  # Prepare a action result
124
124
  result = ActionResult.new(self)
125
125
  result.data = response
@@ -127,12 +127,12 @@ module Moonrope
127
127
  result.time = time_to_run.round(2)
128
128
  result.flags = eval_environment.flags
129
129
  result.headers = eval_environment.headers
130
-
130
+
131
131
  # Return the result object
132
132
  result
133
133
  end
134
134
  end
135
-
135
+
136
136
  #
137
137
  # Check whether the authenticated user has access to this request.
138
138
  # Accepts a Request or an EvalEnvironment.
@@ -146,9 +146,9 @@ module Moonrope
146
146
  else
147
147
  eval_environment = EvalEnvironment.new(@controller.base, request)
148
148
  end
149
-
149
+
150
150
  access_condition = self.access || @controller.access || @controller.base.default_access
151
-
151
+
152
152
  if eval_environment.auth
153
153
  # If there's no authentication object, access is permitted otherwise
154
154
  # we'll do the normal testing.
@@ -174,7 +174,7 @@ module Moonrope
174
174
  !access_condition
175
175
  end
176
176
  end
177
-
177
+
178
178
  #
179
179
  # Return whether or not the passed ParamSet is valid for this action
180
180
  #
@@ -186,17 +186,17 @@ module Moonrope
186
186
  if value[:required] && param_set[name].nil?
187
187
  raise Moonrope::Errors::ParameterError, "`#{name}` parameter is required but is missing"
188
188
  end
189
-
189
+
190
190
  if value[:regex] && param_set[name] && !(param_set[name].to_s =~ value[:regex])
191
191
  raise Moonrope::Errors::ParameterError, "`#{name}` parameter is invalid"
192
192
  end
193
-
193
+
194
194
  if value[:type] && param_set[name] && !param_set[name].is_a?(value[:type])
195
195
  raise Moonrope::Errors::ParameterError, "`#{name}` should be a `#{value[:type]}` but is a `#{param_set[name].class}`"
196
196
  end
197
197
  end
198
198
  true
199
199
  end
200
-
200
+
201
201
  end
202
202
  end
@@ -1,6 +1,6 @@
1
1
  module Moonrope
2
2
  class ActionResult
3
-
3
+
4
4
  #
5
5
  # Initialize a new result from a Moonrope::Action.
6
6
  #
@@ -12,22 +12,22 @@ module Moonrope
12
12
  @time = nil
13
13
  @flags = {}
14
14
  end
15
-
15
+
16
16
  # @return [Object] the return value from the action
17
17
  attr_accessor :data
18
-
18
+
19
19
  # @return [String] the status of the request
20
20
  attr_accessor :status
21
-
21
+
22
22
  # @return [Hash] headers which have been set in the action
23
23
  attr_accessor :headers
24
-
24
+
25
25
  # @return [Float] the length of time to process the action
26
26
  attr_accessor :time
27
-
27
+
28
28
  # @return [Hash] flags which have been set in the action
29
29
  attr_accessor :flags
30
-
30
+
31
31
  #
32
32
  # Return a Hash representation of this ActionResult without the
33
33
  # headers.
@@ -49,7 +49,7 @@ module Moonrope
49
49
  :data => self.data
50
50
  }
51
51
  end
52
-
52
+
53
53
  #
54
54
  # Return the ActionResult's hash with a JSON.
55
55
  #
@@ -58,6 +58,6 @@ module Moonrope
58
58
  def to_json
59
59
  to_hash.to_json
60
60
  end
61
-
61
+
62
62
  end
63
63
  end
data/lib/moonrope/base.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  module Moonrope
2
2
  class Base
3
-
3
+
4
4
  #
5
- # Load a set of Moonrope configuration files from a given
5
+ # Load a set of Moonrope configuration files from a given
6
6
  # directory.
7
7
  #
8
8
  # @param path [String] the path to a directory containing Moonrope files
@@ -13,34 +13,34 @@ module Moonrope
13
13
  api.load(path)
14
14
  api
15
15
  end
16
-
16
+
17
17
  # @return [Array] the array of defined structures
18
18
  attr_reader :structures
19
-
19
+
20
20
  # @return [Array] the array of defined controllers
21
21
  attr_accessor :controllers
22
-
22
+
23
23
  # @return [Array] the array of defined helpers
24
24
  attr_accessor :helpers
25
-
25
+
26
26
  # @return [Moonrope::DSL::BaseDSL] the base DSL
27
27
  attr_accessor :dsl
28
-
28
+
29
29
  # @return [Proc] the authentictor
30
30
  attr_accessor :authenticator
31
-
31
+
32
32
  # @return [Proc] the default access condition
33
33
  attr_accessor :default_access
34
-
34
+
35
35
  # @return [Array] the array of directories to load from (if relevant)
36
36
  attr_accessor :load_directories
37
-
37
+
38
38
  # @return [String] the moonrope environment
39
39
  attr_accessor :environment
40
-
40
+
41
41
  # @return [Proc] a proc to execute before every request
42
42
  attr_accessor :on_request
43
-
43
+
44
44
  #
45
45
  # Initialize a new instance of the Moonrope::Base
46
46
  #
@@ -53,7 +53,7 @@ module Moonrope
53
53
  @dsl = Moonrope::DSL::BaseDSL.new(self)
54
54
  @dsl.instance_eval(&block) if block_given?
55
55
  end
56
-
56
+
57
57
  #
58
58
  # Reset the whole base to contain no data.
59
59
  #
@@ -64,7 +64,7 @@ module Moonrope
64
64
  @authenticator = nil
65
65
  @default_access = nil
66
66
  end
67
-
67
+
68
68
  #
69
69
  # Reload this whole base API from the path
70
70
  #
@@ -84,9 +84,9 @@ module Moonrope
84
84
  raise Moonrope::Errors::Error, "Can't reload Moonrope::Base as it wasn't required from a directory"
85
85
  end
86
86
  end
87
-
87
+
88
88
  alias_method :reload, :load
89
-
89
+
90
90
  #
91
91
  # Load from a given directory
92
92
  #
@@ -100,7 +100,7 @@ module Moonrope
100
100
  false
101
101
  end
102
102
  end
103
-
103
+
104
104
  #
105
105
  # Add a dirctory to the directories to load
106
106
  #
@@ -112,7 +112,7 @@ module Moonrope
112
112
  false
113
113
  end
114
114
  end
115
-
115
+
116
116
  #
117
117
  # Return a structure of the given name
118
118
  #
@@ -122,9 +122,9 @@ module Moonrope
122
122
  def structure(name)
123
123
  structures.select { |s| s.name == name }.first
124
124
  end
125
-
125
+
126
126
  alias_method :[], :structure
127
-
127
+
128
128
  #
129
129
  # Return a controller of the given name
130
130
  #
@@ -134,9 +134,9 @@ module Moonrope
134
134
  def controller(name)
135
135
  controllers.select { |a| a.name == name }.first
136
136
  end
137
-
137
+
138
138
  alias_method :/, :controller
139
-
139
+
140
140
  #
141
141
  # Create a new rack request for this API.
142
142
  #
@@ -145,7 +145,7 @@ module Moonrope
145
145
  def request(*args)
146
146
  Moonrope::Request.new(self, *args)
147
147
  end
148
-
148
+
149
149
  #
150
150
  # Return a helper for the given name and, potentially controller
151
151
  #
@@ -162,7 +162,7 @@ module Moonrope
162
162
  end
163
163
  matched_helpers.first
164
164
  end
165
-
165
+
166
166
  #
167
167
  # Return all the external errors which are registered for this base
168
168
  #
@@ -171,15 +171,15 @@ module Moonrope
171
171
  def external_errors
172
172
  @external_errors ||= {}
173
173
  end
174
-
174
+
175
175
  #
176
176
  # Register a new external error
177
177
  #
178
- # @param error_class [Class] a class which should be caught
178
+ # @param error_class [Class] a class which should be caught
179
179
  #
180
180
  def register_external_error(error_class, &block)
181
181
  self.external_errors[error_class] = block
182
182
  end
183
-
183
+
184
184
  end
185
185
  end
@@ -1,15 +1,15 @@
1
1
  module Moonrope
2
2
  class BeforeAction
3
-
3
+
4
4
  # @return [Array] the names of actions to execute this request on
5
5
  attr_accessor :actions
6
-
6
+
7
7
  # @return [Proc] the block to execute in this action
8
8
  attr_accessor :block
9
-
9
+
10
10
  # @return [Moonrope::Controller] the associated controller
11
11
  attr_reader :controller
12
-
12
+
13
13
  #
14
14
  # Initilize a new BeforeAction
15
15
  #
@@ -19,6 +19,6 @@ module Moonrope
19
19
  @controller = controller
20
20
  @actions = []
21
21
  end
22
-
22
+
23
23
  end
24
24
  end
@@ -1,9 +1,9 @@
1
1
  module Moonrope
2
2
  class Controller
3
-
3
+
4
4
  attr_accessor :name, :actions, :access, :befores
5
5
  attr_reader :base, :dsl
6
-
6
+
7
7
  #
8
8
  # Initalize a new Moonrope::Controller
9
9
  #
@@ -20,7 +20,7 @@ module Moonrope
20
20
  @dsl = Moonrope::DSL::ControllerDSL.new(self)
21
21
  @dsl.instance_eval(&block) if block_given?
22
22
  end
23
-
23
+
24
24
  #
25
25
  # Return an array of before actions which must be executed for
26
26
  # the given action.
@@ -33,7 +33,7 @@ module Moonrope
33
33
  b.actions.empty? || b.actions.include?(action)
34
34
  end
35
35
  end
36
-
36
+
37
37
  #
38
38
  # Lookup and return an action in this controller by name.
39
39
  #
@@ -43,8 +43,8 @@ module Moonrope
43
43
  def action(action)
44
44
  actions[action.to_sym]
45
45
  end
46
-
46
+
47
47
  alias_method :/, :action
48
-
48
+
49
49
  end
50
50
  end
@@ -1,8 +1,8 @@
1
1
  module Moonrope
2
2
  module DSL
3
3
  class ActionDSL
4
-
5
- #
4
+
5
+ #
6
6
  # Initialize a new ActionDSL
7
7
  #
8
8
  # @param action [Moonrope::Action]
@@ -10,8 +10,8 @@ module Moonrope
10
10
  def initialize(action)
11
11
  @action = action
12
12
  end
13
-
14
- #
13
+
14
+ #
15
15
  # Set the description for the action
16
16
  #
17
17
  # description "Returns all users which are configured"
@@ -22,21 +22,26 @@ module Moonrope
22
22
  def description(value)
23
23
  @action.description = value
24
24
  end
25
-
25
+
26
26
  #
27
27
  # Add a new param to the action's param set.
28
28
  #
29
29
  # param :page, "The page number", :default => 2
30
30
  #
31
31
  # @param name [Symbol] the name of the param
32
- # @param description [String] a description of the action
33
- # @param options [Hash] a hash of additional options
32
+ # @param description_or_options [String/Hash] a description of the action or options
33
+ # @param options_if_description [Hash] a hash of additional options if a description was provided
34
34
  # @return [void]
35
35
  #
36
- def param(name, description, options = {})
37
- @action.params[name] = options.merge(:description => description)
36
+ def param(name, description_or_options = {}, options_if_description = {})
37
+ if description_or_options.is_a?(String)
38
+ options = options_if_description.merge(:description => description_or_options)
39
+ else
40
+ options = description_or_options
41
+ end
42
+ @action.params[name] = options
38
43
  end
39
-
44
+
40
45
  #
41
46
  # Set the access condition for the action.
42
47
  #
@@ -50,7 +55,7 @@ module Moonrope
50
55
  def access(value = nil, &block)
51
56
  @action.access = block_given? ? block : value
52
57
  end
53
-
58
+
54
59
  #
55
60
  # Set the action to execute when this action is invoked.
56
61
  #
@@ -60,11 +65,11 @@ module Moonrope
60
65
  #
61
66
  # @yield the contents of the yield will be saved as the action
62
67
  # @return [void]
63
- #
68
+ #
64
69
  def action(&block)
65
70
  @action.action = block
66
71
  end
67
-
72
+
68
73
  end
69
74
  end
70
75
  end
@@ -1,7 +1,7 @@
1
1
  module Moonrope
2
2
  module DSL
3
3
  class BaseDSL
4
-
4
+
5
5
  #
6
6
  # Initiaize a new BaseDSL
7
7
  #
@@ -10,7 +10,7 @@ module Moonrope
10
10
  def initialize(base)
11
11
  @base = base
12
12
  end
13
-
13
+
14
14
  #
15
15
  # Define a new structure
16
16
  #
@@ -27,7 +27,7 @@ module Moonrope
27
27
  structure.dsl.instance_eval(&block) if block_given?
28
28
  structure
29
29
  end
30
-
30
+
31
31
  #
32
32
  # Define a new controller or append values to an existing
33
33
  # controller if it has already been defined.
@@ -46,7 +46,7 @@ module Moonrope
46
46
  controller.dsl.instance_eval(&block) if block_given?
47
47
  controller
48
48
  end
49
-
49
+
50
50
  #
51
51
  # Set the authenticator for the API.
52
52
  #
@@ -55,7 +55,7 @@ module Moonrope
55
55
  def authenticator(&block)
56
56
  @base.authenticator = block
57
57
  end
58
-
58
+
59
59
  #
60
60
  # Set the default access check block.
61
61
  #
@@ -64,7 +64,7 @@ module Moonrope
64
64
  def default_access(value = nil, &block)
65
65
  @base.default_access = block_given? ? block : value
66
66
  end
67
-
67
+
68
68
  #
69
69
  # Define a new helper in the global namespace
70
70
  #
@@ -80,7 +80,7 @@ module Moonrope
80
80
  @base.helpers << helper_instance
81
81
  helper_instance
82
82
  end
83
-
83
+
84
84
  end
85
85
  end
86
86
  end
@@ -1,7 +1,7 @@
1
1
  module Moonrope
2
2
  module DSL
3
3
  class ControllerDSL
4
-
4
+
5
5
  #
6
6
  # Initialize a new ControllerDSL
7
7
  #
@@ -10,10 +10,10 @@ module Moonrope
10
10
  def initialize(controller)
11
11
  @controller = controller
12
12
  end
13
-
13
+
14
14
  # @return [Moonrope::Controller] the associated controller
15
15
  attr_reader :controller
16
-
16
+
17
17
  #
18
18
  # Defines a new action within the controller.
19
19
  #
@@ -27,12 +27,12 @@ module Moonrope
27
27
  @controller.actions[name] = action
28
28
  action
29
29
  end
30
-
30
+
31
31
  #
32
32
  # Defines a new before action within the controller.
33
33
  #
34
34
  # @param actions [Symbol] the names of the actions to apply to (none for all)
35
- # @yield stores the block as the block to be executed
35
+ # @yield stores the block as the block to be executed
36
36
  # @return [Moonrope::BeforeAction]
37
37
  #
38
38
  def before(*actions, &block)
@@ -42,7 +42,7 @@ module Moonrope
42
42
  @controller.befores << before_action
43
43
  before_action
44
44
  end
45
-
45
+
46
46
  #
47
47
  # Defines the access required for controller methods which do not
48
48
  # define their own access.
@@ -50,7 +50,7 @@ module Moonrope
50
50
  def access(value = nil, &block)
51
51
  @controller.access = block_given? ? block : value
52
52
  end
53
-
53
+
54
54
  #
55
55
  # Defines a new helper for this controller.
56
56
  #
@@ -61,7 +61,7 @@ module Moonrope
61
61
  if @controller.base.helper(name, @controller)
62
62
  raise Moonrope::Errors::HelperAlreadyDefined, "Helper has already been defined with name `#{name}`"
63
63
  end
64
-
64
+
65
65
  @controller.base.helpers << Moonrope::Helper.new(name, @controller, options, &block)
66
66
  end
67
67
  end