Class: Sekisyo::WhitelistDetails::Path

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/sekisyo/whitelist_details/path.rb

Overview

Sekisyo WhitelistDetails Path is a definition object for each path in the whitelist.

Constant Summary collapse

VALID_METHODS =
{
  GET: 'get',
  POST: 'post',
  PATCH: 'patch',
  PUT: 'put',
  DELETE: 'delete'
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(path, object) ⇒ Path

e.g.

object = {
           'get' => {
             'required' => ['category_id', 'id'],
             'properties' => {
               'category_id' => { 'type' => 'integer', 'presence' => true }
               'id' => { 'type' => 'integer', 'presence' => true }
             }
           },
           'post' => { ... }
         }

Parameters:

  • path (String)

    Dynamic URL string. Example “/categories/category_id/pets/id”.

  • object (hash)

    Hash object with HTTP method keys.



40
41
42
43
44
45
46
# File 'lib/sekisyo/whitelist_details/path.rb', line 40

def initialize(path, object)
  @path = path
  @path_pattern = /^#{path.gsub(/{(.+?)}/, '(?<\\1>[^/]+)')}(\..*)?$/
  @methods = object.slice(*VALID_METHODS.values).transform_values do |value|
    Sekisyo::WhitelistDetails::Method.new(value)
  end
end

Instance Method Details

#[](method) ⇒ Object



69
70
71
72
73
# File 'lib/sekisyo/whitelist_details/path.rb', line 69

def [](method)
  raise 'Failed to determine HTTP method.' if VALID_METHODS[method.to_sym].nil?

  @methods[VALID_METHODS[method.to_sym]]
end

#path_params(url) ⇒ Hash

Obtain the Path parameter from the dynamic URL according to the definition of the dynamic URL in the whitelist.

Example

@path = "/categories/{category_id}/pets/{id}"
url = "/categories/1/pets/2"
path_params(url) #=> { 'category_id' => '1', 'id' => '2' }

Parameters:

  • url (String)

    URL string.

Returns:

  • (Hash)

    Path parameters.



61
62
63
64
65
66
67
# File 'lib/sekisyo/whitelist_details/path.rb', line 61

def path_params(url)
  param_keys = @path.scan(/(?<=\{).*?(?=\})/)
  @path_pattern =~ url
  param_keys.to_h do |key|
    [key, Regexp.last_match(key)]
  end
end