Class: SmartAdapters::Delegator

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_adapters/delegator.rb

Constant Summary collapse

FORMATS =

List of formats supported by SmartAdapters

%i[json html js xml text csv]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manager, request) ⇒ SmartAdapters::Adapter::Delegator

Initialise SmartAdapters delegator

Parameters:

  • (ActionController)
  • (ActionDispatch::Request)


12
13
14
15
# File 'lib/smart_adapters/delegator.rb', line 12

def initialize(manager, request)
  @manager = manager
  @request = request
end

Instance Attribute Details

#managerstring

Return the controller requested.

Returns:

  • (string)


39
40
41
# File 'lib/smart_adapters/delegator.rb', line 39

def manager
  request_manager
end

Instance Method Details

#actionstring

Return the action requested by the manager.

Returns:

  • (string)


33
34
35
# File 'lib/smart_adapters/delegator.rb', line 33

def action
  request_manager.action_name
end

#api?Boolean

Check if it is an API request

Returns:

  • (Boolean)


59
60
61
# File 'lib/smart_adapters/delegator.rb', line 59

def api?
  json?
end

#loadSmartAdapters::<Controller>::<Action>::<Format>Adapter

Fetch Adatapter base on request



21
22
23
24
25
26
27
28
29
# File 'lib/smart_adapters/delegator.rb', line 21

def load
  unless valid_params?
    raise SmartAdapters::Exceptions::InvalidRequestParamsException
  end
  unless valid_format?
    raise SmartAdapters::Exceptions::InvalidRequestFormatException
  end
  adapter_finder.new(request_manager)
end

#request_paramsHash

Fetch request params

Returns:

  • (Hash)


77
78
79
# File 'lib/smart_adapters/delegator.rb', line 77

def request_params
  @params ||= @request.params
end

#session?Boolean

Check if it is a session request

Returns:

  • (Boolean)


65
66
67
# File 'lib/smart_adapters/delegator.rb', line 65

def session?
  html?
end

#valid_format?Boolean

Check if the request has a valid format.

Returns:

  • (Boolean)


53
54
55
# File 'lib/smart_adapters/delegator.rb', line 53

def valid_format?
  FORMATS.map { |format| send("#{format}?") }.any?
end

#valid_params?Boolean

Check if the request has a valid params.

Returns:

  • (Boolean)


71
72
73
# File 'lib/smart_adapters/delegator.rb', line 71

def valid_params?
  request_params.present?
end