Class: Sipatra::Base
- Inherits:
-
Object
- Object
- Sipatra::Base
- Includes:
- HelperMethods
- Defined in:
- lib/sipatra/base.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.filters ⇒ Object
readonly
Returns the value of attribute filters.
-
.req_handlers ⇒ Object
readonly
Returns the value of attribute req_handlers.
-
.resp_handlers ⇒ Object
readonly
Returns the value of attribute resp_handlers.
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
-
#log ⇒ Object
Returns the value of attribute log.
-
#message ⇒ Object
(also: #request, #response)
Returns the value of attribute message.
-
#params ⇒ Object
Returns the value of attribute params.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
-
#sip_factory ⇒ Object
Returns the value of attribute sip_factory.
Class Method Summary collapse
- .after(message_type = nil, &block) ⇒ Object
- .after_filters ⇒ Object
- .before(message_type = nil, &block) ⇒ Object
- .before_filters ⇒ Object
-
.configure(*envs) {|_self| ... } ⇒ Object
permits configuration of the application.
-
.disable(*opts) ⇒ Object
Same as calling ‘set :option, false` for each of the given options.
-
.enable(*opts) ⇒ Object
Same as calling ‘set :option, true` for each of the given options.
-
.extensions ⇒ Object
Extension modules registered on this class and all superclasses.
-
.helpers(*modules, &block) ⇒ Object
Methods defined in the block and/or in the module arguments available to handlers.
- .inherited(subclass) ⇒ Object
- .invoke_hook(name, *args) ⇒ Object
-
.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.
- .reset! ⇒ Object
- .response(*args, &block) ⇒ Object
-
.set(option, value = self, &block) ⇒ Object
Sets an option to the given value.
-
.settings ⇒ Object
Access settings defined with Base.set.
Instance Method Summary collapse
-
#do_request ⇒ Object
called to process a SIP request.
-
#do_response ⇒ Object
called to process a SIP response.
-
#halt ⇒ Object
Exit the current block, halts any further processing of the message.
-
#initialize ⇒ Base
constructor
A new instance of Base.
-
#pass ⇒ Object
Pass control to the next matching handler.
- #request? ⇒ Boolean
- #response? ⇒ Boolean
-
#set_bindings(*args) ⇒ Object
called from Java to set SIP servlet bindings.
-
#settings ⇒ Object
Access settings defined with Base.set.
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
#initialize ⇒ Base
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
.filters ⇒ Object (readonly)
Returns the value of attribute filters.
145 146 147 |
# File 'lib/sipatra/base.rb', line 145 def filters @filters end |
.req_handlers ⇒ Object (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_handlers ⇒ Object (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
#context ⇒ Object
Returns the value of attribute context.
10 11 12 |
# File 'lib/sipatra/base.rb', line 10 def context @context end |
#log ⇒ Object
Returns the value of attribute log.
10 11 12 |
# File 'lib/sipatra/base.rb', line 10 def log @log end |
#message ⇒ Object Also known as: request, response
Returns the value of attribute message.
10 11 12 |
# File 'lib/sipatra/base.rb', line 10 def @message end |
#params ⇒ Object
Returns the value of attribute params.
10 11 12 |
# File 'lib/sipatra/base.rb', line 10 def params @params end |
#session ⇒ Object
Returns the value of attribute session.
10 11 12 |
# File 'lib/sipatra/base.rb', line 10 def session @session end |
#sip_factory ⇒ Object
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( = nil, &block) add_filter(:after, , &block) end |
.after_filters ⇒ Object
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( = nil, &block) add_filter(:before, , &block) end |
.before_filters ⇒ Object
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
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 |
.extensions ⇒ Object
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.
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) (option, &value) ("#{option}?") { !!__send__(option) } ("#{option}=") { |val| (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 |
.settings ⇒ Object
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_request ⇒ Object
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_response ⇒ Object
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 |
#halt ⇒ Object
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 |
#pass ⇒ Object
Pass control to the next matching handler.
53 54 55 |
# File 'lib/sipatra/base.rb', line 53 def pass throw :pass end |
#request? ⇒ Boolean
57 58 59 |
# File 'lib/sipatra/base.rb', line 57 def request? !.respond_to?(:getRequest) end |
#response? ⇒ Boolean
61 62 63 |
# File 'lib/sipatra/base.rb', line 61 def response? .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 .extend Sipatra::MessageExtension end |
#settings ⇒ Object
Access settings defined with Base.set.
41 42 43 |
# File 'lib/sipatra/base.rb', line 41 def settings self.class.settings end |