Class: Boar::Services::Generic

Inherits:
Object
  • Object
show all
Includes:
Utils::Basic
Defined in:
app/models/boar/services/generic.rb

Direct Known Subclasses

Downloads, Pages

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Basic

#ensure_hash, #get_option, #interpolate

Constructor Details

#initialize(controller) ⇒ Generic

Returns a new instance of Generic.



17
18
19
20
21
22
23
24
25
# File 'app/models/boar/services/generic.rb', line 17

def initialize(controller)
  @controller = controller
  @configuration = Rails.application.config.boar
  @options = @controller.boar_options
  @params = @controller.params

  skip_cache_param = get_option(@options, :skip_cache_param, @configuration.skip_cache_param)
  @skip_cache = skip_cache_param ? @controller.request.params[skip_cache_param].to_boolean : false
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



11
12
13
# File 'app/models/boar/services/generic.rb', line 11

def configuration
  @configuration
end

#controllerObject

Returns the value of attribute controller.



10
11
12
# File 'app/models/boar/services/generic.rb', line 10

def controller
  @controller
end

#optionsObject

Returns the value of attribute options.



12
13
14
# File 'app/models/boar/services/generic.rb', line 12

def options
  @options
end

#paramsObject

Returns the value of attribute params.



13
14
15
# File 'app/models/boar/services/generic.rb', line 13

def params
  @params
end

Instance Method Details

#handle_authentication(path, options = nil) ⇒ Object



58
59
60
# File 'app/models/boar/services/generic.rb', line 58

def handle_authentication(path, options = nil)
  self.handler_for(:authentication, options).call(path)
end

#handle_error(exception) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/boar/services/generic.rb', line 36

def handle_error(exception)
  if exception.is_a?(Lazier::Exceptions::Dump) then
    raise exception
  else
    handler = self.handler_for(:views, @options)

    # Get HTTP code
    code = exception.is_a?(Boar::Exceptions::Error) ? exception.code : 500

    # Basing on the code
    view = case code
      when 403 then handler.call(:error, code: 403)
      when 404 then handler.call(:error, code: 404)
      else
        raise exception if !Boar::Exceptions::Error.must_raise?(exception)
        handler.call(:error, code: 500, exception: exception)
    end

    @controller.render(file: view, status: code, formats: [:html]) if !@controller.performed?
  end
end

#handler_for(scope, options = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/boar/services/generic.rb', line 62

def handler_for(scope, options = nil)
  options = ensure_hash(options)
  scope = scope.to_sym

  # Get the handler specified by user
  handlers_options = get_option(@options.merge(options), :handlers, {})

  # Use either the user handler or the system one
  handlers = @configuration.handlers.merge(handlers_options)

  # Instantiate the handler
  @boar_handlers ||= {}
  @boar_handlers[scope] ||= ::Lazier.find_class(handlers[scope]).new(self, options)
end

#run(method) ⇒ Object



27
28
29
30
31
32
33
34
# File 'app/models/boar/services/generic.rb', line 27

def run(method)
  begin
    self.send(method)
  rescue => e
    e = Boar::Exceptions::UnImplemented.new if !self.respond_to?(method)
    self.handle_error(e)
  end
end