Module: Camille::Controller

Defined in:
lib/camille/controller.rb

Defined Under Namespace

Classes: ArgumentError, MissingRenderError, ParamsTypeError, ResponseTypeError, TypeError

Instance Method Summary collapse

Instance Method Details

#camille_endpointObject



13
14
15
# File 'lib/camille/controller.rb', line 13

def camille_endpoint
  camille_schema && camille_schema.endpoints[action_name.to_sym]
end

#camille_schemaObject



9
10
11
# File 'lib/camille/controller.rb', line 9

def camille_schema
  @camille_schema ||= Camille::Loader.controller_name_to_schema_map[self.class.name]
end

#process_actionObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/camille/controller.rb', line 44

def process_action(*)
  Camille::Loader.check_and_raise_exception
  if endpoint = camille_endpoint
    begin
      if Camille::Configuration.check_params
        # New way: check params against the endpoint's params_type if it exists
        if endpoint.params_type
          params_to_check = params.to_unsafe_h
          check_result = endpoint.params_type.check_params(params_to_check)

          if check_result.type_error?
            string_io = StringIO.new
            Camille::TypeErrorPrinter.new(check_result).print(string_io)
            raise ParamsTypeError.new("\nType check failed for params.\n#{string_io.string}")
          else
            # Use the checked value which already has snake_case keys
            self.params = ActionController::Parameters.new(check_result.value)
          end
        end
      else
        # Old way: just transform keys
        params.deep_transform_keys!{|key| Camille::Configuration.params_key_converter.call(key.to_s)}
      end

      result = super
      # When there's no `render` call, Rails will return status 204
      if response.status == 204
        raise_missing_render_error
      end
      result
    rescue ActionController::MissingExactTemplate
      raise_missing_render_error
    end
  else
    super
  end
end

#render(*args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/camille/controller.rb', line 17

def render *args
  if endpoint = camille_endpoint
    render_options = args.last
    intended_status = render_options[:status] || 200
    if intended_status == 200 || intended_status == :ok
      if render_options.has_key? :json
        value = render_options[:json]
        result = endpoint.response_type.check(value)
        if result.type_error?
          string_io = StringIO.new
          Camille::TypeErrorPrinter.new(result).print(string_io)
          raise ResponseTypeError.new("\nType check failed for response.\n#{string_io.string}")
        else
          rendered = result.render.json
          super(json: rendered)
        end
      else
        raise ArgumentError.new("Expected key :json for `render` call.")
      end
    else
      super
    end
  else
    super
  end
end