Class: OpenapiBlocks::Routing::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_blocks/routing/operation.rb

Overview

rubocop:disable Style/Documentation

Constant Summary collapse

ACTIONS_MAP =
{
  "index"   => { verbs: ["get"], has_body: false },
  "show"    => { verbs: ["get"],           has_body: false },
  "create"  => { verbs: ["post"],          has_body: true  },
  "update"  => { verbs: %w[put patch], has_body: true },
  "destroy" => { verbs: ["delete"], has_body: false }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(route) ⇒ Operation

Returns a new instance of Operation.



16
17
18
19
20
21
22
# File 'lib/openapi_blocks/routing/operation.rb', line 16

def initialize(route)
  @controller = route[:controller]
  @action     = route[:action]
  @path       = normalize_path(route[:path])
  @verbs      = ACTIONS_MAP.dig(@action, :verbs)
  @has_body   = ACTIONS_MAP.dig(@action, :has_body)
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



14
15
16
# File 'lib/openapi_blocks/routing/operation.rb', line 14

def action
  @action
end

#controllerObject (readonly)

Returns the value of attribute controller.



14
15
16
# File 'lib/openapi_blocks/routing/operation.rb', line 14

def controller
  @controller
end

#has_bodyObject (readonly)

Returns the value of attribute has_body.



14
15
16
# File 'lib/openapi_blocks/routing/operation.rb', line 14

def has_body
  @has_body
end

#pathObject (readonly)

Returns the value of attribute path.



14
15
16
# File 'lib/openapi_blocks/routing/operation.rb', line 14

def path
  @path
end

#verbsObject (readonly)

Returns the value of attribute verbs.



14
15
16
# File 'lib/openapi_blocks/routing/operation.rb', line 14

def verbs
  @verbs
end

Instance Method Details

#path_parametersObject



28
29
30
# File 'lib/openapi_blocks/routing/operation.rb', line 28

def path_parameters
  @path.scan(/\{(\w+)\}/).flatten
end

#schema_nameObject



32
33
34
# File 'lib/openapi_blocks/routing/operation.rb', line 32

def schema_name
  @controller.split("/").last.classify
end

#valid?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/openapi_blocks/routing/operation.rb', line 24

def valid?
  ACTIONS_MAP.key?(@action) && @verbs.present?
end