Class: Sapp::Path::Request
Instance Attribute Summary collapse
-
#handler ⇒ Object
readonly
Returns the value of attribute handler.
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
-
#verb ⇒ Object
readonly
Returns the value of attribute verb.
Attributes inherited from Base
#controller, #options, #original, #stream
Instance Method Summary collapse
- #check_methods?(p, hash) ⇒ Boolean
- #check_stream?(p, hash) ⇒ Boolean
- #extract_key(k) ⇒ Object
-
#extract_keys ⇒ Object
Remove the first key:value(controller) return a hash with keys from path.
- #find_controller ⇒ Object
-
#find_path ⇒ Object
Matches by method names and stream count, returns best match.
-
#initialize(original, verb, routes) ⇒ Request
constructor
A new instance of Request.
- #key?(k) ⇒ Boolean
-
#match_by_methods_and_stream ⇒ Object
Returns hash of matched paths by stream count and method name.
-
#match_by_stream_count ⇒ Object
Returns hash of matched paths by stream count.
-
#parse ⇒ Object
Sets controller, path keys and handler.
-
#path? ⇒ Boolean
Returns boolean, is handler defined?.
-
#paths ⇒ Object
Return paths from request controller.
-
#set_controller ⇒ Object
Sets the controller based off the first key:value pair.
-
#sort_path ⇒ Object
Parses path by ‘/’ and creates a numbered dictionary EX: { 0 => ‘user’ }.
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
#handler ⇒ Object (readonly)
Returns the value of attribute handler.
7 8 9 |
# File 'lib/sapp/path/request.rb', line 7 def handler @handler end |
#keys ⇒ Object (readonly)
Returns the value of attribute keys.
7 8 9 |
# File 'lib/sapp/path/request.rb', line 7 def keys @keys end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/sapp/path/request.rb', line 7 def path @path end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
7 8 9 |
# File 'lib/sapp/path/request.rb', line 7 def routes @routes end |
#verb ⇒ Object (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
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
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_keys ⇒ Object
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_controller ⇒ Object
83 84 85 |
# File 'lib/sapp/path/request.rb', line 83 def find_controller routes[verb][controller] end |
#find_path ⇒ Object
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
74 75 76 |
# File 'lib/sapp/path/request.rb', line 74 def key? k k > 0 end |
#match_by_methods_and_stream ⇒ Object
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_count ⇒ Object
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 |
#parse ⇒ Object
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?
101 102 103 |
# File 'lib/sapp/path/request.rb', line 101 def path? find_controller && find_path.any? end |
#paths ⇒ Object
Return paths from request controller
44 45 46 |
# File 'lib/sapp/path/request.rb', line 44 def paths find_controller[:paths] end |
#set_controller ⇒ Object
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_path ⇒ Object
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 |