Class: Culpa::PathParser

Inherits:
Object
  • Object
show all
Defined in:
lib/culpa/path_parser.rb

Class Method Summary collapse

Class Method Details

.compose_regex(url) ⇒ Object

Compose a regex from a human-readable http path pattern



59
60
61
62
# File 'lib/culpa/path_parser.rb', line 59

def self.compose_regex(url)
  regex = url.split('/').map { |dir| dir_to_regex(dir) }.join('/')
  Regexp.new("^#{regex}$")
end

.create_route_cache(brickchains, global_prefix) ⇒ Object

Use the brickchains to build the cache of route



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/culpa/path_parser.rb', line 17

def self.create_route_cache(brickchains, global_prefix)
  custom_routes = []
  brickchains.each do |name, data|
    next unless data.key?(:options) && data[:options] && data[:options].key?(:url)
    item = {
      regex: compose_regex(global_prefix + data[:options][:url]),
      router_method_name: name
    }
    item[:verb] = data[:options][:verb] if data[:options].key? :verb
    custom_routes << item
  end
  fixed_routes = [
    {
      infer: {
        get: 'list',
        post: 'create'
      },
      regex: compose_regex(global_prefix + '/:res_name')
    }, {
      infer: {
        get: 'get',
        put: 'update',
        patch: 'update',
        delete: 'delete'
      },
      regex: compose_regex(global_prefix + '/:res_name/:id')
    }, {
      regex: compose_regex(global_prefix + '/:res_name/:id/:sub_call')
    }
  ]
  @@route_patterns = custom_routes + fixed_routes
end

.dir_to_regex(dir) ⇒ Object

Transform a string to its equivalent for the compose_regex



52
53
54
55
# File 'lib/culpa/path_parser.rb', line 52

def self.dir_to_regex(dir)
  return dir unless dir.start_with? ':'
  "(?<#{dir[1..dir.length - 1]}>\\w+)"
end

.extract_vars(path, call_options) ⇒ Object

Search for the routes, extracts informations and returns the router method name and the request informations



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/culpa/path_parser.rb', line 67

def self.extract_vars(path, call_options)
  @@route_patterns.each do |data|
    next unless (params = parse data, path)
    call_options[:params].merge!(params)
    if data.key?(:router_method_name)
      raise RouteNotFoundError if data.key?(:verb) && call_options[:verb] != data[:verb]
      return data[:router_method_name], call_options
    else
      return infer_method_name(data, call_options[:params], call_options[:verb]), call_options
    end
  end
end

.infer_method_name(pattern, params, verb) ⇒ Object

Try to return the router method name or infer it automatically



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/culpa/path_parser.rb', line 82

def self.infer_method_name(pattern, params, verb)
  if params.key?('sub_call')
    return "#{params['sub_call']}_#{params['res_name']}".to_sym
  end

  unless pattern[:infer].is_a?(Hash) && pattern[:infer].key?(verb)
    raise UnpredictableSubCallError
  end

  "#{pattern[:infer][verb]}_#{params['res_name']}".to_sym
end

.parse(pattern, path) ⇒ Object

Parse a path and return the results of the parse



9
10
11
12
13
# File 'lib/culpa/path_parser.rb', line 9

def self.parse(pattern, path)
  result = path.match(pattern[:regex])
  return unless result
  Hash[result.names.map { |name| name }.zip(result.captures)]
end

.route_patternsObject



3
4
5
# File 'lib/culpa/path_parser.rb', line 3

def self.route_patterns
  @@route_patterns
end