Class: Sapp::Path::Request

Inherits:
Base
  • Object
show all
Defined in:
lib/sapp/path/request.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#controller, #options, #original, #stream

Instance Method Summary collapse

Methods inherited from Base

#concat_namespace_and_path, #counter, #create_path, #extract_keys_and_paths, #extract_paths, #namespace_to_path, #namespaces, #namespaces?, #nested?, #nested_deeply?, #options?, #set_original, #setup_extraction

Constructor Details

#initialize(original, verb, routes) ⇒ Request

Returns a new instance of Request.



9
10
11
12
13
14
15
# File 'lib/sapp/path/request.rb', line 9

def initialize original, verb, routes
  @original = original
  @verb = verb
  @routes = routes
  @keys = Hash.new
  @handler = nil
end

Instance Attribute Details

#handlerObject (readonly)

Returns the value of attribute handler.



7
8
9
# File 'lib/sapp/path/request.rb', line 7

def handler
  @handler
end

#keysObject (readonly)

Returns the value of attribute keys.



7
8
9
# File 'lib/sapp/path/request.rb', line 7

def keys
  @keys
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/sapp/path/request.rb', line 7

def path
  @path
end

#routesObject (readonly)

Returns the value of attribute routes.



7
8
9
# File 'lib/sapp/path/request.rb', line 7

def routes
  @routes
end

#verbObject (readonly)

Returns the value of attribute verb.



7
8
9
# File 'lib/sapp/path/request.rb', line 7

def verb
  @verb
end

Instance Method Details

#check_methods?(p, hash) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/sapp/path/request.rb', line 91

def check_methods? p, hash
  (p[:methods].values - hash.values).empty?
end

#check_stream?(p, hash) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/sapp/path/request.rb', line 87

def check_stream? p, hash
  p[:stream].count == hash.values.count
end

#extract_key(k) ⇒ Object



78
79
80
81
# File 'lib/sapp/path/request.rb', line 78

def extract_key k
  key = path[:keys][k]
  key.tr(':', '').to_sym
end

#extract_keysObject

Remove the first key:value(controller) return a hash with keys from path



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sapp/path/request.rb', line 62

def extract_keys
  hash = Hash.new

  sort_path.each do |k, v|
    if path[:keys].include?(k) && k > 0
      hash[extract_key(k)] = v
    end
  end

  hash
end

#find_controllerObject



83
84
85
# File 'lib/sapp/path/request.rb', line 83

def find_controller
  routes[verb][controller]
end

#find_pathObject

Matches by method names and stream count, returns best match



39
40
41
# File 'lib/sapp/path/request.rb', line 39

def find_path
  match_by_methods_and_stream
end

#key?(k) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/sapp/path/request.rb', line 74

def key? k
  k > 0
end

#match_by_methods_and_streamObject

Returns hash of matched paths by stream count and method name



34
35
36
# File 'lib/sapp/path/request.rb', line 34

def match_by_methods_and_stream
  match_by_stream_count.select { |p| check_methods? p, sort_path }
end

#match_by_stream_countObject

Returns hash of matched paths by stream count



29
30
31
# File 'lib/sapp/path/request.rb', line 29

def match_by_stream_count
  paths.select { |p| check_stream? p, sort_path }
end

#parseObject

Sets controller, path keys and handler



18
19
20
21
22
23
24
25
26
# File 'lib/sapp/path/request.rb', line 18

def parse
  set_controller

  if path?
    @path  = find_path.uniq.first
    @keys  = extract_keys
    @handler = @path[:handler]
  end
end

#path?Boolean

Returns boolean, is handler defined?

Returns:

  • (Boolean)


101
102
103
# File 'lib/sapp/path/request.rb', line 101

def path?
  find_controller && find_path.any?
end

#pathsObject

Return paths from request controller



44
45
46
# File 'lib/sapp/path/request.rb', line 44

def paths
  find_controller[:paths]
end

#set_controllerObject

Sets the controller based off the first key:value pair



96
97
98
# File 'lib/sapp/path/request.rb', line 96

def set_controller 
  @controller = sort_path[0]
end

#sort_pathObject

Parses path by ‘/’ and creates a numbered dictionary EX: { 0 => ‘user’ }



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sapp/path/request.rb', line 49

def sort_path
  reset_counter
  array = Array.new

  setup_extraction.each do |v|
    array << [counter, v]
    count
  end

  @sort_path ||= Hash[array]
end