Class: SinatraServiceController

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Sinatra::Helpers
Defined in:
lib/framework_ext/sinatra_controller.rb

Overview

Base code shared by all service controllers This allows us to share code between controllers more precisely to render templates and in general to use sinatra helpers

See Also:

  • and Sinatra::Helpers

Author:

  • Matt Aimonetti

Defined Under Namespace

Classes: AuthenticationFailed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, service) ⇒ SinatraServiceController

Returns a new instance of SinatraServiceController.

Parameters:

  • app (Sinatra::Application)

    The Sinatra app used as a reference and to access request params

  • service (WSDSL)

    The service served by this controller

Raises:

  • (ParamError, NoParamsDefined, MissingParam, UnexpectedParam, InvalidParamType, InvalidParamValue)

    If the params don’t validate one of the ParamsVerification errors will be raised.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/framework_ext/sinatra_controller.rb', line 53

def initialize(app, service)
  @app      = app
  @env      = app.env
  @request  = app.request
  @response = app.response
  @service  = service
 
  # raises an exception if the params are not valid
  # otherwise update the app params with potentially new params (using default values)   
  # note that if a type if mentioned for a params, the object will be cast to this object type 
  @params = app.params = ParamsVerification.validate!(app.params, service.defined_params)

  # Authentication check
  if service.auth_required
    raise AuthenticationFailed unless logged_in?
  end
end

Instance Attribute Details

#appSinatra::Application (readonly)

Returns:

  • (Sinatra::Application)


28
29
30
# File 'lib/framework_ext/sinatra_controller.rb', line 28

def app
  @app
end

#envHash (readonly)

Returns:

  • (Hash)


32
33
34
# File 'lib/framework_ext/sinatra_controller.rb', line 32

def env
  @env
end

#paramsHash

Returns:

  • (Hash)


46
47
48
# File 'lib/framework_ext/sinatra_controller.rb', line 46

def params
  @params
end

#requestSinatra::Request (readonly)

Returns:

  • (Sinatra::Request)

See Also:



37
38
39
# File 'lib/framework_ext/sinatra_controller.rb', line 37

def request
  @request
end

#responseSinatra::Response (readonly)

Returns:

  • (Sinatra::Response)

See Also:



42
43
44
# File 'lib/framework_ext/sinatra_controller.rb', line 42

def response
  @response
end

#serviceWSDSL (readonly)

Returns The service served by this controller.

Returns:

  • (WSDSL)

    The service served by this controller



24
25
26
# File 'lib/framework_ext/sinatra_controller.rb', line 24

def service
  @service
end

Instance Method Details

#logged_in?Boolean

Returns true or false if the player is logged in.

Returns:

  • (Boolean)


76
77
78
# File 'lib/framework_ext/sinatra_controller.rb', line 76

def logged_in?
  !session[:player_id].nil?
end