Class: Sinatra::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/codebutler/sinatra.rb

Constant Summary collapse

URI_CHAR =
'[^/?:,&#\.]'.freeze
PARAM =
/:(#{URI_CHAR}+)/.freeze
SPLAT =
/(.*?)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}, &b) ⇒ Event

Returns a new instance of Event.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/codebutler/sinatra.rb', line 146

def initialize(path, options = {}, &b)
  @path = path
  @block = b
  @param_keys = []
  @options = options
  regex = @path.to_s.gsub(PARAM) do
    @param_keys << $1
    "(#{URI_CHAR}+)"
  end
  
  regex.gsub!('*', SPLAT.to_s)
  
  @pattern = /^#{regex}$/
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



144
145
146
# File 'lib/codebutler/sinatra.rb', line 144

def block
  @block
end

#optionsObject (readonly)

Returns the value of attribute options.



144
145
146
# File 'lib/codebutler/sinatra.rb', line 144

def options
  @options
end

#param_keysObject (readonly)

Returns the value of attribute param_keys.



144
145
146
# File 'lib/codebutler/sinatra.rb', line 144

def param_keys
  @param_keys
end

#pathObject (readonly)

Returns the value of attribute path.



144
145
146
# File 'lib/codebutler/sinatra.rb', line 144

def path
  @path
end

#patternObject (readonly)

Returns the value of attribute pattern.



144
145
146
# File 'lib/codebutler/sinatra.rb', line 144

def pattern
  @pattern
end

Instance Method Details

#invoke(request) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/codebutler/sinatra.rb', line 161

def invoke(request)
  params = {}
  if agent = options[:agent] 
    return unless request.user_agent =~ agent
    params[:agent] = $~[1..-1]
  end
  if host = options[:host] 
    return unless host === request.host
  end
  return unless pattern =~ request.path_info.squeeze('/')
  params.merge!(param_keys.zip($~.captures.map(&:from_param)).to_hash)
  Result.new(block, params, 200)
end