Class: Smooth::Resource::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/smooth/resource/router.rb

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

Instance Method Summary collapse

Constructor Details

#initialize(resource, _options = {}) ⇒ Router

Returns a new instance of Router.



9
10
11
12
13
14
# File 'lib/smooth/resource/router.rb', line 9

def initialize(resource, _options = {})
  @resource = resource
  @table = {}
  @descriptions = {}
  @rules = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/smooth/resource/router.rb', line 149

def method_missing(meth, *args, &block)
  if Verbs.keys.include?(meth.to_sym)
    pattern = args.shift
    define_route(meth, pattern, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#descriptionsObject (readonly)

Returns the value of attribute descriptions.



4
5
6
# File 'lib/smooth/resource/router.rb', line 4

def descriptions
  @descriptions
end

#resourceObject (readonly)

Returns the value of attribute resource.



4
5
6
# File 'lib/smooth/resource/router.rb', line 4

def resource
  @resource
end

#rulesObject (readonly)

Returns the value of attribute rules.



4
5
6
# File 'lib/smooth/resource/router.rb', line 4

def rules
  @rules
end

#tableObject (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 |_|
    options, _ = _

    handler = methods_table.method(options[:name])

    sinatra.send options[:method], options[: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.message, 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.message, backtrace: exception.backtrace, stage: 'response' }.to_json
      end
    end
  end
end

#build_methods_tableObject



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 |_|
      options, block = _
      method_name   = options.fetch(:name)
      k.send :define_method, method_name, (block || router.lookup_handler_for(options[:method], options[:to]))
    end
  end
end

#counterObject



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] ||= {}
  options = args.extract_options!

  name = options.fetch(:as, "#{ request_method }_#{ counter }")

  describe_route(request_method, route_pattern)

  rules << bucket[route_pattern] = [
    options.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 expand_routes(from_attributes = {})
  route_patterns_table.reduce({}) do |memo, p|
    route_name, details = p
    memo[route_name] = Smooth.util.expand_url_template(details[:template], from_attributes)
    memo
  end
end

#interface_documentationObject

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_tableObject



129
130
131
# File 'lib/smooth/resource/router.rb', line 129

def methods_table
  @methods_table ||= (@methods_table_class || build_methods_table).new
end

#patternsObject



64
65
66
# File 'lib/smooth/resource/router.rb', line 64

def patterns
  rules.flatten.compact.map { |r| r.fetch(:pattern) }
end

#route_patterns_tableObject



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_tableObject



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_templatesObject



68
69
70
# File 'lib/smooth/resource/router.rb', line 68

def uri_templates
  rules.flatten.compact.map { |r| r.fetch(:template) }
end