class TomogramRouting::Tomogram

Public Class Methods

craft(docs) click to toggle source
# File lib/tomogram_routing/tomogram.rb, line 5
def self.craft(docs)
  new(MultiJson.load(docs).inject([]) do |res, doc|
    res.push(Request.new.merge(doc))
  end)
end
new(array) click to toggle source
Calls superclass method
# File lib/tomogram_routing/tomogram.rb, line 20
def initialize(array)
  super
  compile_path_patterns
end

Public Instance Methods

find_request(method:, path:) click to toggle source
# File lib/tomogram_routing/tomogram.rb, line 11
def find_request(method:, path:)
  path = find_request_path(method: method, path: path)
  find do |doc|
    doc['path'] == path && doc['method'] == method
  end
end

Private Instance Methods

actions_with_same_method(documentation, method) click to toggle source
# File lib/tomogram_routing/tomogram.rb, line 84
def actions_with_same_method(documentation, method)
  documentation.find_all do |doc|
    doc['method'] == method
  end
end
compile_path_pattern(path) click to toggle source
# File lib/tomogram_routing/tomogram.rb, line 34
def compile_path_pattern(path)
  str = Regexp.escape(path)
  str = str.gsub(/\\{\w+\\}/, '[^&=\/]+')
  str = "\\A#{ str }\\z"
  Regexp.new(str)
end
compile_path_patterns() click to toggle source
# File lib/tomogram_routing/tomogram.rb, line 25
def compile_path_patterns
  each do |action|
    next unless (path = action['path'])

    regexp = compile_path_pattern(path)
    action['path_regexp'] = regexp
  end
end
cut_off_query_params(path) click to toggle source
# File lib/tomogram_routing/tomogram.rb, line 65
def cut_off_query_params(path)
  path.gsub(/\?.*\z/, '')
end
find_request_path(method:, path:) click to toggle source
# File lib/tomogram_routing/tomogram.rb, line 41
def find_request_path(method:, path:)
  return '' unless path && path.size > 0

  path = normalize_path(path)

  action = search_for_an_exact_match(method, path, self)
  return action['path'] if action

  action = search_with_parameter(method, path, self)
  return action['path'] if action

  ''
end
normalize_path(path) click to toggle source
# File lib/tomogram_routing/tomogram.rb, line 55
def normalize_path(path)
  path = cut_off_query_params(path)
  remove_the_slash_at_the_end2(path)
end
remove_the_slash_at_the_end2(path) click to toggle source
# File lib/tomogram_routing/tomogram.rb, line 60
def remove_the_slash_at_the_end2(path)
  return path[0..-2] if path[-1] == '/'
  path
end
search_for_an_exact_match(method, path, documentation) click to toggle source
# File lib/tomogram_routing/tomogram.rb, line 69
def search_for_an_exact_match(method, path, documentation)
  documentation.find do |action|
    action['path'] == path && action['method'] == method
  end
end
search_with_parameter(method, path, documentation) click to toggle source
# File lib/tomogram_routing/tomogram.rb, line 75
def search_with_parameter(method, path, documentation)
  documentation = actions_with_same_method(documentation, method)

  documentation.find do |action|
    next unless regexp = action['path_regexp']
    regexp =~ path
  end
end