Class: Stylish::Developer::Route
- Inherits:
-
Object
- Object
- Stylish::Developer::Route
- Defined in:
- lib/stylish/developer/route.rb
Overview
The Route class is responsible for routing requests to the API to their appropriate handler.
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
Class Method Summary collapse
Instance Method Summary collapse
- #actual_path ⇒ Object
- #info_handler_rack_response ⇒ Object
-
#initialize(env) ⇒ Route
constructor
A new instance of Route.
- #listing_handler ⇒ Object
- #models_handler ⇒ Object
- #modification_handler ⇒ Object
- #path_handler ⇒ Object
- #prefix ⇒ Object
- #request ⇒ Object
- #request_type ⇒ Object
- #respond ⇒ Object
Constructor Details
#initialize(env) ⇒ Route
Returns a new instance of Route.
12 13 14 |
# File 'lib/stylish/developer/route.rb', line 12 def initialize(env) @env = env end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
10 11 12 |
# File 'lib/stylish/developer/route.rb', line 10 def env @env end |
Class Method Details
.request(env) ⇒ Object
6 7 8 |
# File 'lib/stylish/developer/route.rb', line 6 def self.request(env) new(env) end |
Instance Method Details
#actual_path ⇒ Object
87 88 89 |
# File 'lib/stylish/developer/route.rb', line 87 def actual_path request.path[/^#{ prefix }\/(\w+)\/(.+)$/, 2] end |
#info_handler_rack_response ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/stylish/developer/route.rb', line 51 def info_handler_rack_response body = { root: Stylish::Developer.server.root.to_s, sprockets_paths: Stylish::Developer.server.sprockets.paths, library: Stylish::Developer.config.library.as_json }.to_json headers = { "Content-Type" => "application/json" } [200, headers, [body]] end |
#listing_handler ⇒ Object
79 80 81 |
# File 'lib/stylish/developer/route.rb', line 79 def listing_handler @listing_handler ||= Stylish::Developer::Listing.new(actual_path, request_type) end |
#models_handler ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/stylish/developer/route.rb', line 65 def models_handler path_args = actual_path.split("/") parts = path_args.dup action = parts.shift prefix = parts.shift path = parts.join("/") @models_handler ||= Stylish::Developer::ModelDelegator.new(path, action, prefix, request: request, path_args: path_args) end |
#modification_handler ⇒ Object
75 76 77 |
# File 'lib/stylish/developer/route.rb', line 75 def modification_handler @modification_handler ||= Stylish::Developer::Modification.new(actual_path, request_type, request) end |
#path_handler ⇒ Object
83 84 85 |
# File 'lib/stylish/developer/route.rb', line 83 def path_handler @path_handler ||= Stylish::Developer::Path.new(actual_path, request_type) end |
#prefix ⇒ Object
20 21 22 |
# File 'lib/stylish/developer/route.rb', line 20 def prefix Stylish::Developer.config.base_url end |
#request ⇒ Object
16 17 18 |
# File 'lib/stylish/developer/route.rb', line 16 def request @request ||= Rack::Request.new(env) end |
#request_type ⇒ Object
91 92 93 94 |
# File 'lib/stylish/developer/route.rb', line 91 def request_type return "info" if request.path.match(/^#{prefix}\/info$/) request.path[/^#{ prefix }\/(\w+)\/(.+)$/, 1] end |
#respond ⇒ Object
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 49 |
# File 'lib/stylish/developer/route.rb', line 24 def respond data = case when %w{meta content compiled}.include?(request_type) path_handler.to_rack_response() when request_type == "list" listing_handler.to_rack_response() when request_type == "models" models_handler.to_rack_response() when request_type == "info" info_handler_rack_response when %w(create update delete).include?(request_type) modification_handler.to_rack_response() else [404, {}, ['Not found']] end status, headers, body = data [status, headers, body] end |