Class: Apia::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/apia/route.rb

Constant Summary collapse

REQUEST_METHODS =
[:get, :post, :patch, :put, :delete].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, **options) ⇒ Route

Returns a new instance of Route.



16
17
18
19
20
21
22
23
24
25
# File 'lib/apia/route.rb', line 16

def initialize(path, **options)
  @path = path

  @group = options[:group]

  @controller = options[:controller]
  @endpoint = options[:endpoint]

  @request_method = options[:request_method] || :get
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



11
12
13
# File 'lib/apia/route.rb', line 11

def controller
  @controller
end

#endpointApia::Endpoint

Return the endpoint object for this route

Returns:



30
31
32
33
34
35
36
# File 'lib/apia/route.rb', line 30

def endpoint
  if @endpoint.is_a?(Symbol) && controller
    return controller.definition.endpoints[@endpoint]
  end

  @endpoint
end

#groupObject (readonly)

Returns the value of attribute group.



13
14
15
# File 'lib/apia/route.rb', line 13

def group
  @group
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/apia/route.rb', line 10

def path
  @path
end

#request_methodObject (readonly)

Returns the value of attribute request_method.



12
13
14
# File 'lib/apia/route.rb', line 12

def request_method
  @request_method
end

Instance Method Details

#extract_arguments(given_path) ⇒ Hash

Extract arguments from the given path and return a hash of the arguments based on their naming from the route

Parameters:

  • given_path (String)

Returns:

  • (Hash)


50
51
52
53
54
55
56
57
58
# File 'lib/apia/route.rb', line 50

def extract_arguments(given_path)
  given_path_parts = RouteSet.split_path(given_path)
  path_parts.each_with_index.with_object({}) do |(part, index), hash|
    next unless part =~ /\A:(\w+)/

    value = given_path_parts[index]
    hash[Regexp.last_match[1]] = value
  end
end

#path_partsArray<String>

Return the parts for this route

Returns:

  • (Array<String>)


41
42
43
# File 'lib/apia/route.rb', line 41

def path_parts
  @path_parts ||= RouteSet.split_path(@path)
end