Class | RubyAMF::Envelope |
In: |
lib/rubyamf/envelope.rb
|
Parent: | RocketAMF::Envelope |
Adds several important features to RocketAMF::Envelope. None of these features are dependent on Rails, and as such can be used by any Rack compliant framework. Features are credentials support, easy parameter mapping based on configured parameter mappings, mapping scope support for serialization, and error handling for method dispatch using each_method_call.
mapping_scope | [RW] |
Finds and returns credentials set on the request as a hash with keys username and password, with the type dependent on the hash_key_access setting. setHeader(‘Credentials’) credentials are used first, followed by new-style DSRemoteCredentials. If no credentials are found, a hash is returned with a username and password of nil.
# File lib/rubyamf/envelope.rb, line 18 18: def credentials 19: ds_cred_key = RubyAMF.configuration.translate_case ? "ds_remote_credentials" : "DSRemoteCredentials" 20: if RubyAMF.configuration.hash_key_access == :symbol 21: userid_key = :userid 22: username_key = :username 23: password_key = :password 24: ds_cred_key = ds_cred_key.to_sym 25: else 26: userid_key = "userid" 27: username_key = "username" 28: password_key = "password" 29: end 30: 31: # Old style setHeader('Credentials', CREDENTIALS_HASH) 32: if @headers['Credentials'] 33: h = @headers['Credentials'] 34: return {username_key => h.data[userid_key], password_key => h.data[password_key]} 35: end 36: 37: # New style DSRemoteCredentials 38: messages.each do |m| 39: if m.data.is_a?(RocketAMF::Values::RemotingMessage) 40: if m.data.headers && m.data.headers[ds_cred_key] 41: username,password = Base64.decode64(m.data.headers[ds_cred_key]).split(':') 42: return {username_key => username, password_key => password} 43: end 44: end 45: end 46: 47: # Failure case sends empty credentials, because rubyamf_plugin does it 48: {username_key => nil, password_key => nil} 49: end
Extends default RocketAMF implementation to log caught exceptions and translate them into a RocketAMF::Values::ErrorMessage for return to flash after removing the backtrace (for safety).
# File lib/rubyamf/envelope.rb, line 78 78: def dispatch_call p 79: begin 80: ret = p[:block].call(p[:method], p[:args]) 81: raise ret if ret.is_a?(Exception) # If they return FaultObject like you could in rubyamf_plugin 82: ret 83: rescue Exception => e 84: # Log exception 85: RubyAMF.logger.log_error(e) 86: 87: # Clear backtrace so that RocketAMF doesn't send back the full backtrace 88: e.set_backtrace([]) 89: 90: # Create ErrorMessage object using the source message as the base 91: RocketAMF::Values::ErrorMessage.new(p[:source], e) 92: end 93: end
Given a controller, action, and the flash arguments array, returns a hash containing the arguments indexed by number as well as named key if a named mapping has been configured. Returned hash respects hash_key_access setting for named keys.
Example:
RubyAMF.configuration.map_params "c", "a", ["param1", "param2"] params = envelope.params_hash "c", "a", ["asdf", "fdsa"] params.should == {:param1 => "asdf", :param2 => "fdsa", 0 => "asdf", 1 => "fdsa"}
# File lib/rubyamf/envelope.rb, line 61 61: def params_hash controller, action, arguments 62: conf = RubyAMF.configuration 63: mapped = {} 64: mapping = conf.param_mappings[controller+"#"+action] 65: arguments.each_with_index do |arg, i| 66: mapped[i] = arg 67: if mapping && mapping[i] 68: mapping_key = conf.hash_key_access == :symbol ? mapping[i].to_sym : mapping[i].to_s 69: mapped[mapping_key] = arg 70: end 71: end 72: mapped 73: end