Class: Cathode::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/cathode/request.rb

Overview

A ‘Request` object is created when a Rails request is intercepted by Cathode. This object is responsible for enforcing version headers as well as token authorization headers. After header enforcement, the object figures out which version and resource/action to use to process the request. Finally, the request’s ‘_body` (HTTP response body) and `_status` (HTTP status code) are set and rendered from the controller that initiated the request.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Request

Initializes the request

Parameters:

  • context (ActionController)

    The controller responding to the request



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cathode/request.rb', line 48

def initialize(context)
  @context = context

  version_number = context.request.headers['HTTP_ACCEPT_VERSION']

  if version_number.nil?
    @_status, @_body = :bad_request, 'A version number must be passed in the Accept-Version header'
    return self
  end

  version = Version.find(version_number)
  unless version.present?
    @_status, @_body = :bad_request, "Unknown API version: #{version_number}"
    return self
  end

  action_name = params[:action]
  if action_name == 'custom'
    action_name = context.request.path.split('/').last
  end

  params[:controller].slice! 'cathode/'
  resources = params[:controller].split('_').map(&:to_sym)
  resource = version._resources.find(resources.first)
  @resource_tree = [resource]
  subresources = resources.drop(1).collect do |r|
    resource = resource._resources.find(r)
  end
  @resource_tree += subresources
  resource = @resource_tree.last

  @resource = resource

  if @resource_tree.size > 1
    @action = resource.actions.find(action_name.to_sym)
  else
    unless version.action?(resource.try(:name) || '', action_name)
      @_status = :not_found
      return self
    end
    @action = version._resources.find(resource.name).actions.find(action_name.to_sym)
  end

  @strong_params = @action.strong_params
  @_status = :ok

  if action.override_block
    context.instance_eval(&action.override_block)
  else
    action_block = action.action_block
    if action_block.nil? && respond_to?(:default_action_block)
      action_block = default_action_block
    end

    instance_eval(&action_block)
  end

  body if @_body.nil?
end

Instance Attribute Details

#_bodyObject (readonly)

Returns the value of attribute _body.



10
11
12
# File 'lib/cathode/request.rb', line 10

def _body
  @_body
end

#_statusObject (readonly)

Returns the value of attribute _status.



10
11
12
# File 'lib/cathode/request.rb', line 10

def _status
  @_status
end

#actionObject (readonly)

Returns the value of attribute action.



10
11
12
# File 'lib/cathode/request.rb', line 10

def action
  @action
end

#contextObject (readonly)

Returns the value of attribute context.



10
11
12
# File 'lib/cathode/request.rb', line 10

def context
  @context
end

#custom_logicObject (readonly)

Returns the value of attribute custom_logic.



10
11
12
# File 'lib/cathode/request.rb', line 10

def custom_logic
  @custom_logic
end

#resourceObject (readonly)

Returns the value of attribute resource.



10
11
12
# File 'lib/cathode/request.rb', line 10

def resource
  @resource
end

Class Method Details

.create(context) ⇒ IndexRequest, ...

Creates a request by initializing the appropriate subclass.

Parameters:

  • context (ActionController)

    The controller responding to the request

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cathode/request.rb', line 27

def create(context)
  klass = case context.params[:action].to_sym
          when :index
            IndexRequest
          when :show
            ShowRequest
          when :create
            CreateRequest
          when :update
            UpdateRequest
          when :destroy
            DestroyRequest
          else
            CustomRequest
          end
  klass.new(context)
end