Module: Camille::Controller

Defined in:
lib/camille/controller.rb

Defined Under Namespace

Classes: ArgumentError, MissingRenderError, TypeError

Instance Method Summary collapse

Instance Method Details

#camille_endpointObject



11
12
13
# File 'lib/camille/controller.rb', line 11

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

#camille_schemaObject



7
8
9
# File 'lib/camille/controller.rb', line 7

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

#process_actionObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/camille/controller.rb', line 42

def process_action(*)
  Camille::Loader.check_and_raise_exception
  if endpoint = camille_endpoint
    begin
      params.deep_transform_keys!{|key| Camille::Configuration.params_key_converter.call(key.to_s)}
      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



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

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 TypeError.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