Class: Sipatra::Base

Inherits:
Object
  • Object
show all
Includes:
HelperMethods
Defined in:
lib/sipatra/base.rb

Direct Known Subclasses

Application

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HelperMethods

#add_address_header, #add_header, #address_header, #address_headers, #create_address, #create_uri, #header, #header?, #headers, #modify_header, #proxy, #push_route, #remove_header, #send_response

Constructor Details

#initializeBase

Returns a new instance of Base.



14
15
16
# File 'lib/sipatra/base.rb', line 14

def initialize()
  @params = Hash.new { |hash,key| hash[key.to_s] if Symbol === key }
end

Class Attribute Details

.filtersObject (readonly)

Returns the value of attribute filters.



145
146
147
# File 'lib/sipatra/base.rb', line 145

def filters
  @filters
end

.req_handlersObject (readonly)

Returns the value of attribute req_handlers.



145
146
147
# File 'lib/sipatra/base.rb', line 145

def req_handlers
  @req_handlers
end

.resp_handlersObject (readonly)

Returns the value of attribute resp_handlers.



145
146
147
# File 'lib/sipatra/base.rb', line 145

def resp_handlers
  @resp_handlers
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



10
11
12
# File 'lib/sipatra/base.rb', line 10

def context
  @context
end

#logObject

Returns the value of attribute log.



10
11
12
# File 'lib/sipatra/base.rb', line 10

def log
  @log
end

#messageObject Also known as: request, response

Returns the value of attribute message.



10
11
12
# File 'lib/sipatra/base.rb', line 10

def message
  @message
end

#paramsObject

Returns the value of attribute params.



10
11
12
# File 'lib/sipatra/base.rb', line 10

def params
  @params
end

#sessionObject

Returns the value of attribute session.



10
11
12
# File 'lib/sipatra/base.rb', line 10

def session
  @session
end

#sip_factoryObject

Returns the value of attribute sip_factory.



10
11
12
# File 'lib/sipatra/base.rb', line 10

def sip_factory
  @sip_factory
end

Class Method Details

.after(message_type = nil, &block) ⇒ Object



235
236
237
# File 'lib/sipatra/base.rb', line 235

def after(message_type = nil, &block)
  add_filter(:after, message_type, &block)
end

.after_filtersObject



255
256
257
# File 'lib/sipatra/base.rb', line 255

def after_filters
  filters[:after]
end

.before(message_type = nil, &block) ⇒ Object



231
232
233
# File 'lib/sipatra/base.rb', line 231

def before(message_type = nil, &block)
  add_filter(:before, message_type, &block)
end

.before_filtersObject



251
252
253
# File 'lib/sipatra/base.rb', line 251

def before_filters
  filters[:before]
end

.configure(*envs) {|_self| ... } ⇒ Object

permits configuration of the application

Yields:

  • (_self)

Yield Parameters:

  • _self (Sipatra::Base)

    the object that the method was called on



148
149
150
# File 'lib/sipatra/base.rb', line 148

def configure(*envs, &block)
  yield self if envs.empty? || envs.include?(environment.to_sym)
end

.disable(*opts) ⇒ Object

Same as calling ‘set :option, false` for each of the given options.



177
178
179
# File 'lib/sipatra/base.rb', line 177

def disable(*opts)
  opts.each { |key| set(key, false) }
end

.enable(*opts) ⇒ Object

Same as calling ‘set :option, true` for each of the given options.



172
173
174
# File 'lib/sipatra/base.rb', line 172

def enable(*opts)
  opts.each { |key| set(key, true) }
end

.extensionsObject

Extension modules registered on this class and all superclasses.



189
190
191
192
193
194
195
# File 'lib/sipatra/base.rb', line 189

def extensions
  if superclass.respond_to?(:extensions)
    (@extensions + superclass.extensions).uniq
  else
    @extensions
  end
end

.helpers(*modules, &block) ⇒ Object

Methods defined in the block and/or in the module arguments available to handlers.



183
184
185
186
# File 'lib/sipatra/base.rb', line 183

def helpers(*modules, &block)
  include(*modules) if modules.any?
  class_eval(&block) if block_given?
end

.inherited(subclass) ⇒ Object



246
247
248
249
# File 'lib/sipatra/base.rb', line 246

def inherited(subclass)
  subclass.reset!
  super
end

.invoke_hook(name, *args) ⇒ Object



259
260
261
# File 'lib/sipatra/base.rb', line 259

def invoke_hook(name, *args)
  extensions.each { |e| e.send(name, *args) if e.respond_to?(name) }
end

.register_extension(*extensions, &block) ⇒ Object

Extends current class with all modules passed as arguements if a block is present, creates a module with the block and extends the current class with it.



200
201
202
203
204
205
206
207
# File 'lib/sipatra/base.rb', line 200

def register_extension(*extensions, &block)
  extensions << Module.new(&block) if block_given?
  @extensions += extensions
  extensions.each do |extension|
    extend extension
    extension.registered(self) if extension.respond_to?(:registered)
  end
end

.reset!Object



239
240
241
242
243
244
# File 'lib/sipatra/base.rb', line 239

def reset!
  @req_handlers          = {}
  @resp_handlers         = {}
  @extensions            = []
  @filters               = {:before => [], :after => []}
end

.response(*args, &block) ⇒ Object



209
210
211
212
213
214
215
216
# File 'lib/sipatra/base.rb', line 209

def response(*args, &block)
  method_name = args.shift if (!args.first.kind_of? Hash) and (!args.first.kind_of? Integer)
  code_int = args.shift if !args.first.kind_of? Hash
  opts = *args
  pattern = code_int || 0
  sip_method_name = method_name ? method_name.to_s.upcase : "_"
  handler("response_#{sip_method_name}  \"#{pattern}\"", sip_method_name, pattern, [], opts || {}, &block)
end

.set(option, value = self, &block) ⇒ Object

Sets an option to the given value. If the value is a proc, the proc will be called every time the option is accessed.

Raises:

  • (ArgumentError)


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/sipatra/base.rb', line 154

def set(option, value=self, &block)
  raise ArgumentError if block && value != self
  value = block if block
  if value.kind_of?(Proc)
    metadef(option, &value)
    metadef("#{option}?") { !!__send__(option) }
    metadef("#{option}=") { |val| metadef(option, &Proc.new{val}) }
  elsif value == self && option.respond_to?(:each)
    option.each { |k,v| set(k, v) }
  elsif respond_to?("#{option}=")
    __send__ "#{option}=", value
  else
    set option, Proc.new{value}
  end
  self
end

.settingsObject

Access settings defined with Base.set.



36
37
38
# File 'lib/sipatra/base.rb', line 36

def self.settings
  self
end

Instance Method Details

#do_requestObject

called to process a SIP request



26
27
28
# File 'lib/sipatra/base.rb', line 26

def do_request
  call! self.class.req_handlers
end

#do_responseObject

called to process a SIP response



31
32
33
# File 'lib/sipatra/base.rb', line 31

def do_response
  call! self.class.resp_handlers
end

#haltObject

Exit the current block, halts any further processing of the message. TODO: handle a response (as param)



48
49
50
# File 'lib/sipatra/base.rb', line 48

def halt
  throw :halt
end

#passObject

Pass control to the next matching handler.



53
54
55
# File 'lib/sipatra/base.rb', line 53

def pass
  throw :pass
end

#request?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/sipatra/base.rb', line 57

def request?
  !message.respond_to?(:getRequest)
end

#response?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/sipatra/base.rb', line 61

def response?
  message.respond_to?(:getRequest)
end

#set_bindings(*args) ⇒ Object

called from Java to set SIP servlet bindings



19
20
21
22
23
# File 'lib/sipatra/base.rb', line 19

def set_bindings(*args)
  @context, @sip_factory, @session, @message, @log = args
  session.extend Sipatra::SessionExtension
  message.extend Sipatra::MessageExtension
end

#settingsObject

Access settings defined with Base.set.



41
42
43
# File 'lib/sipatra/base.rb', line 41

def settings
  self.class.settings
end