Class: Smooth::Resource::Router
Constant Summary collapse
- Verbs =
{ get: :get, show: :get, put: :put, patch: :put, create: :post, delete: :delete, destroy: :destroy, options: :options, post: :post }
Instance Attribute Summary collapse
-
#descriptions ⇒ Object
readonly
Returns the value of attribute descriptions.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
- #apply_to(sinatra) ⇒ Object
- #build_methods_table ⇒ Object
- #counter ⇒ Object
- #define_route(request_method, route_pattern, *args, &block) ⇒ Object
- #desc(description, *_args) ⇒ Object
- #describe_route(request_method, route_pattern) ⇒ Object
- #expand_routes(from_attributes = {}) ⇒ Object
-
#initialize(resource, _options = {}) ⇒ Router
constructor
A new instance of Router.
-
#interface_documentation ⇒ Object
I may be getting this in a convoluted way may be easier to build up naturally.
-
#lookup_handler_for(method, signifier) ⇒ Object
Allows for a configuration syntax like.
- #method_missing(meth, *args, &block) ⇒ Object
- #methods_table ⇒ Object
- #patterns ⇒ Object
- #route_patterns_table ⇒ Object
- #route_table ⇒ Object
- #uri_templates ⇒ Object
Constructor Details
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
Instance Attribute Details
#descriptions ⇒ Object (readonly)
Returns the value of attribute descriptions.
4 5 6 |
# File 'lib/smooth/resource/router.rb', line 4 def descriptions @descriptions end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
4 5 6 |
# File 'lib/smooth/resource/router.rb', line 4 def resource @resource end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
4 5 6 |
# File 'lib/smooth/resource/router.rb', line 4 def rules @rules end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
4 5 6 |
# File 'lib/smooth/resource/router.rb', line 4 def table @table end |
Instance Method Details
#apply_to(sinatra) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/smooth/resource/router.rb', line 72 def apply_to(sinatra) router = self user_finder = resource.api.method(:lookup_current_user).to_proc policy_finder = resource.api.method(:lookup_policy).to_proc router.rules.each do |_| , _ = _ handler = methods_table.method([:name]) sinatra.send [:method], [:pattern] do |*args| begin request = { headers: headers, params: params, user: user_finder.call(params, headers), policy: policy_finder.call(params, headers), args: args } rescue => exception halt 500, {}, { error: exception., backtrace: exception.backtrace, stage: 'request' }.to_json end begin response = handler.call(request.to_mash) body response.body headers response.headers status response.status rescue => exception halt 500, {}, { error: exception., backtrace: exception.backtrace, stage: 'response' }.to_json end end end end |
#build_methods_table ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/smooth/resource/router.rb', line 109 def build_methods_table router = self @methods_table_class = Class.new do k = self router.rules.each do |_| , block = _ method_name = .fetch(:name) k.send :define_method, method_name, (block || router.lookup_handler_for([:method], [:to])) end end end |
#counter ⇒ Object
124 125 126 127 |
# File 'lib/smooth/resource/router.rb', line 124 def counter @counter ||= 0 @counter += 1 end |
#define_route(request_method, route_pattern, *args, &block) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/smooth/resource/router.rb', line 158 def define_route(request_method, route_pattern, *args, &block) request_method = Verbs.fetch(request_method.to_sym, :get) bucket = table[request_method] ||= {} = args. name = .fetch(:as, "#{ request_method }_#{ counter }") describe_route(request_method, route_pattern) rules << bucket[route_pattern] = [ .merge(name: name, method: request_method, args: args, pattern: route_pattern, template: Smooth.util.uri_template(route_pattern)), block ] end |
#desc(description, *_args) ⇒ Object
133 134 135 |
# File 'lib/smooth/resource/router.rb', line 133 def desc(description, *_args) descriptions[:current] = description end |
#describe_route(request_method, route_pattern) ⇒ Object
173 174 175 176 177 178 179 180 |
# File 'lib/smooth/resource/router.rb', line 173 def describe_route(request_method, route_pattern) documentation = descriptions[request_method] ||= {} if description = descriptions[:current] documentation[route_pattern] = description descriptions.delete(:current) end end |
#expand_routes(from_attributes = {}) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/smooth/resource/router.rb', line 38 def (from_attributes = {}) route_patterns_table.reduce({}) do |memo, p| route_name, details = p memo[route_name] = Smooth.util.(details[:template], from_attributes) memo end end |
#interface_documentation ⇒ Object
I may be getting this in a convoluted way may be easier to build up naturally
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/smooth/resource/router.rb', line 18 def interface_documentation descriptions.keys.reduce({}) do |memo, verb| routes = descriptions[verb] routes.each do |_| pattern, description = _ memo["#{ verb.to_s.upcase } #{ pattern }"] = description end memo end end |
#lookup_handler_for(method, signifier) ⇒ Object
Allows for a configuration syntax like
routes do
get "/books", :to => :query
end
the lookup_handler_for method will attempt to discern which object is best suited to handle the request based on the http verb and the signifier
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/smooth/resource/router.rb', line 191 def lookup_handler_for(method, signifier) method = method.to_sym signifier = signifier.to_sym resource = self.resource case when method == :get && signifier == :query ->(req) { resource.fetch(:query, :default).respond_to_request(req) } when (method == :show || method == :get) && signifier == :show ->(req) { resource.fetch(:query, :default).respond_to_find_request(req) } when method == :get ->(req) { resource.fetch(:query, signifier).respond_to_request(req) } # Mutation Methods when method == :put || method == :post || method == :delete ->(req) { resource.fetch(:command, signifier).respond_to_request(req) } else ->(req) { Smooth::ErrorResponse.new('Unable to find matching route', req) } end end |
#methods_table ⇒ Object
129 130 131 |
# File 'lib/smooth/resource/router.rb', line 129 def methods_table @methods_table ||= (@methods_table_class || build_methods_table).new end |
#patterns ⇒ Object
64 65 66 |
# File 'lib/smooth/resource/router.rb', line 64 def patterns rules.flatten.compact.map { |r| r.fetch(:pattern) } end |
#route_patterns_table ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/smooth/resource/router.rb', line 46 def route_patterns_table return @route_patterns_table if @route_patterns_table @route_patterns_table = rules.flatten.compact.reduce({}) do |memo, rule| memo.tap do name = rule[:name] pattern = rule[:pattern] template = rule[:template] memo[name] = { pattern: pattern, template: template, variables: Array(template.variables) } end end end |
#route_table ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/smooth/resource/router.rb', line 30 def route_table @route_table ||= route_patterns_table.reduce({}) do |memo, p| route_name, details = p memo[route_name] = details[:pattern] memo end end |
#uri_templates ⇒ Object
68 69 70 |
# File 'lib/smooth/resource/router.rb', line 68 def uri_templates rules.flatten.compact.map { |r| r.fetch(:template) } end |