Class: Moonrope::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/moonrope/action.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, name) { ... } ⇒ Action

Initialize a new action

Parameters:

  • controller (Moonrope::Controller)

    the controller this action belongs to

  • name (Symbol)

    the name of the action

Yields:

  • allows the action to be configured via Moonrope::DSL::ActionDSL



32
33
34
35
36
37
38
# File 'lib/moonrope/action.rb', line 32

def initialize(controller, name, &block)
  @controller = controller
  @name = name
  @params = {}
  @dsl = Moonrope::DSL::ActionDSL.new(self)
  @dsl.instance_eval(&block) if block_given?
end

Instance Attribute Details

#accessProc

Returns the access check condition for the action.

Returns:

  • (Proc)

    the access check condition for the action



20
21
22
# File 'lib/moonrope/action.rb', line 20

def access
  @access
end

#actionProc

Returns the action for the action.

Returns:

  • (Proc)

    the action for the action



23
24
25
# File 'lib/moonrope/action.rb', line 23

def action
  @action
end

#controllerMoonrope::Controller (readonly)

Returns the associated controller.

Returns:



5
6
7
# File 'lib/moonrope/action.rb', line 5

def controller
  @controller
end

#descriptionString

Returns the description of the action.

Returns:

  • (String)

    the description of the action



17
18
19
# File 'lib/moonrope/action.rb', line 17

def description
  @description
end

#dslMoonrope::DSL::Action (readonly)

Returns the action’s DSL.

Returns:

  • (Moonrope::DSL::Action)

    the action’s DSL



11
12
13
# File 'lib/moonrope/action.rb', line 11

def dsl
  @dsl
end

#nameSymbol (readonly)

Returns the name of the action.

Returns:

  • (Symbol)

    the name of the action



8
9
10
# File 'lib/moonrope/action.rb', line 8

def name
  @name
end

#paramsHash (readonly)

Returns the params available for the action.

Returns:

  • (Hash)

    the params available for the action



14
15
16
# File 'lib/moonrope/action.rb', line 14

def params
  @params
end

Instance Method Details

#check_access(request = nil) ⇒ Boolean

Check whether the authenticated user has access to this request. Accepts a Request or an EvalEnvironment.

Parameters:

Returns:

  • (Boolean)


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/moonrope/action.rb', line 143

def check_access(request = nil)
  if request.is_a?(EvalEnvironment)
    eval_environment = request
  else
    eval_environment = EvalEnvironment.new(@controller.base, request)
  end
  
  access_condition = self.access || @controller.access || @controller.base.default_access
  
  if eval_environment.auth
    # If there's no authentication object, access is permitted otherwise
    # we'll do the normal testing.
    if access_condition.is_a?(Proc)
      !!eval_environment.instance_exec(self, &access_condition)
    elsif access_condition.is_a?(Symbol)
      !!(eval_environment.auth.respond_to?(access_condition) && eval_environment.auth.send(access_condition))
    elsif access_condition.is_a?(Hash) && access_condition[:must_be] && access_condition[:with]
      !!(eval_environment.auth.is_a?(access_condition[:must_be]) &&
          eval_environment.auth.respond_to?(access_condition[:with]) &&
          eval_environment.auth.send(access_condition[:with])
        )
    elsif access_condition.is_a?(Hash) && access_condition[:must_be]
      !!(eval_environment.auth.is_a?(access_condition[:must_be]))
    elsif access_condition == true
      true
    else
      false
    end
  else
    # No authentication object is available to test with. The result here
    # depends on whether or not an access condition has been defined or not.
    !access_condition
  end
end

#convert_errors_to_action_result(&block) ⇒ Object

Execute a block of code and catch approprite Moonrope errors and return a result.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/moonrope/action.rb', line 56

def convert_errors_to_action_result(&block)
  begin
    yield block
  rescue => exception
    case exception
    when Moonrope::Errors::RequestError
      result = ActionResult.new(self)
      result.status = exception.status
      result.data = exception.data
      result
    else
      if error_block = @controller.base.external_errors[exception.class]
        result = ActionResult.new(self)
        error_block.call(exception, result)
        result
      else
        raise
      end
    end
  end
end

#default_paramsHash

Return a hash of all params for this action which are

Returns:

  • (Hash)

    hash with field names as keys with default values



45
46
47
48
49
50
# File 'lib/moonrope/action.rb', line 45

def default_params
  @params.inject({}) do |h,(k,v)|
    h[k.to_s] = v[:default] if v[:default]
    h
  end
end

#execute(request = nil) ⇒ Moonrope::ActionResult

Executes the action and returns a ActionResult object with the result of the action.

Parameters:

Returns:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/moonrope/action.rb', line 85

def execute(request = nil)
  if request.is_a?(EvalEnvironment)
    eval_environment = request
  else
    eval_environment = EvalEnvironment.new(@controller.base, request)
  end
  
  #
  # Set this actions default parameters in the eval environment so that
  # it has access to them.
  #
  eval_environment.default_params = self.default_params
  
  #
  # Set the current action to the eval environment so it knows what action
  # invoked this.
  #
  eval_environment.action = self
  
  convert_errors_to_action_result do
    #
    # Validate the parameters
    #
    self.validate_parameters(eval_environment.params)
    
    start_time = Time.now
    
    # Run before filters
    controller.before_actions_for(name).each do |action|
      eval_environment.instance_eval(&action.block)
    end
    
    # Run the actual action
    response = eval_environment.instance_eval(&action)
    
    # Calculate the length of time this request takes
    time_to_run = Time.now - start_time
    
    # Prepare a action result
    result = ActionResult.new(self)
    result.data     = response
    result.status   = 'success'
    result.time     = time_to_run.round(2)
    result.flags    = eval_environment.flags
    result.headers  = eval_environment.headers
    
    # Return the result object
    result
  end
end

#validate_parameters(param_set) ⇒ Boolean

Return whether or not the passed ParamSet is valid for this action

Parameters:

Returns:

  • (Boolean)


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/moonrope/action.rb', line 184

def validate_parameters(param_set)
  @params.each do |name, value|
    if value[:required] && param_set[name].nil?
      raise Moonrope::Errors::ParameterError, "`#{name}` parameter is required but is missing"
    end
    
    if value[:regex] && param_set[name] && !(param_set[name].to_s =~ value[:regex])
      raise Moonrope::Errors::ParameterError, "`#{name}` parameter is invalid"
    end
    
    if value[:type] && param_set[name] && !param_set[name].is_a?(value[:type])
      raise Moonrope::Errors::ParameterError, "`#{name}` should be a `#{value[:type]}` but is a `#{param_set[name].class}`"
    end
  end
  true
end