Class: Lowkiq::Web::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/lowkiq/web/action.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_method, segments, &body) ⇒ Action

Returns a new instance of Action.



16
17
18
19
20
# File 'lib/lowkiq/web/action.rb', line 16

def initialize(request_method, segments, &body)
  @request_method = request_method
  @url_pattern = self.class.segments_to_regex(segments)
  @body = body
end

Class Method Details

.segments_to_regex(segments) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/lowkiq/web/action.rb', line 4

def self.segments_to_regex(segments)
  prepared = segments.map do |segment|
    case segment
    when Symbol
      "(?<#{segment}>[^\/]+)"
    else
      segment
    end
  end.join( '/' )
  Regexp.new '\A' + '/' + prepared + '\z'
end

Instance Method Details

#call(req) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/lowkiq/web/action.rb', line 22

def call(req)
  return if @request_method != req.request_method
  match = @url_pattern.match req.path_info
  return unless match
  data = @body.call req, match
  [200, {}, [JSON.generate(data)]]
end