Class SwxGateway
In: lib/swxruby/swx_gateway.rb
Parent: Object

Methods

Attributes

app_root  [RW] 
swx_config  [RW] 

Public Class methods

Eval all files in the services_path into the SwxServiceClasses namespace

[Source]

    # File lib/swxruby/swx_gateway.rb, line 14
14:                 def init_service_classes
15:                         Dir.glob(File.join(app_root, swx_config['services_path'], './**/*.rb'))      do |filename| 
16:                                 # Load service class into SwxServiceClasses namespace. 
17:                                 SwxServiceClasses.module_eval(File.read(filename))
18:                         end
19:                         true
20:                 end

Convert strings containing ‘null’ to nil. Null in Flash is equivalent to nil in Ruby.

[Source]

    # File lib/swxruby/swx_gateway.rb, line 23
23:                 def nillify_nulls(args_array)
24:                         # Convert all strings containing 'null' to nil
25:                         args_array.collect! { |arg| if arg == 'null' then nil else arg end }
26:                         # Return nil if the args array contained only 'null' strings
27:                         if args_array.compact.empty? then nil else args_array end
28:                 end

The entry point for SWX request processing. Takes a hash of params and goes to work generating SWX bytecode.

Special note: Contrary to Ruby convention, keys in the params hash are camelCase (instead of underscored). This is to maintain compatibility with the SWX AS library which sends request parameters using camelCase (ActionScript‘s variable naming convention).

Params

  • :args — JSON string of arguments (converted to a Ruby object and passed to the specified method of the service class)
  • :debug — Boolean. If set to true, the generated SWX file will attempt to establish a local connection the SWX Analyzer when opened in Flash Player.
  • :method — specifies the method to be called on the service class. May be either camelCased or underscored (camelCased will be converted to underscored before being called on the service class)
  • :serviceClass — specifies the service class
  • :url — (optional) the url of the SWF file making this request. Added to the generated SWX file to skirt cross-domain issues. If not specified, the resulting SWX file allow access from any domain

Examples

  SwxGateway.process(:args => 'Hello World!', :debug => true, :method => 'echo_data', :serviceClass => 'Simple', :url => 'http://myfunkysite/swxconsumer.swf')
  # => A binary string of SWX bytecode containing the result of +Simple.new#echo_data('Hello World!')+; debugging enabled and allowing access from the specified url

  SwxGateway.process(:args => 'Hello World!', :debug => true, :method => 'echo_data', :serviceClass => 'Simple')
  # => Same as previous, except allows access from any url

  SwxGateway.process(:args => [1,2], :debug => false, :method => 'addNumbers', :serviceClass => 'Simple', :url => 'http://myfunkysite/swxconsumer.swf')
  # calls params[:method].underscore
  # => A binary string of SWX bytecode containing the result of +Simple.new#add_numbers(1, 2)+; no debugging and allowing access from the specified url

[Source]

    # File lib/swxruby/swx_gateway.rb, line 53
53:     def process(params)
54:       # Set defaults if the SWX gateway isn't configured
55:       swx_config ||= {'compression_level' => 4, 'allow_domain' => true}
56:       
57:                         # convert JSON arguments to a Ruby object
58:                         args = json_to_ruby params[:args]
59:                         
60:       unless args.nil?
61:         # Ensure that none of the arguments contain 'undefined'
62:                         raise ArgumentError, "The request contained undefined args.\n  serviceClass: #{params[:serviceClass]}\n  method: #{params[:method]}\n  args: #{args.join(', ')}" if args.any? { |argument| argument == 'undefined' }
63:                                 # Convert 'null' strings in args array to nil
64:                           args = nillify_nulls(args)
65:                 end
66:                         
67:                         # Fetch the class constant for the specified service class
68:                         validate_service_class_name(params[:serviceClass])
69:       service_class = class_eval("SwxServiceClasses::#{params[:serviceClass]}")
70: 
71:                         # convert camelCased params[:method] to underscored (does nothing if params[:method] is already underscored)
72:                         # This effectively bridges the gap between ActionScript and Ruby variable/method naming conventions.
73:                         params[:method] = params[:method].underscore
74: 
75:                         # Prevent nefarious use of methods that the service class inherited from Object
76:                         raise NoMethodError unless (service_class.public_instance_methods - Object.public_instance_methods).include?(params[:method])
77:                         
78:       # Instantiate the service class, call the specified method, and capture the response
79:                         service_class_response = if args.nil?
80:                                 # No args were passed, so assume the service class' method doesn't take any arguments
81:               service_class.new.send(params[:method])
82:                         else
83:                                 # Call the service class' method and pass in the arguments (uses an * to pass an array as multiple arguments)
84:               service_class.new.send(params[:method], *args)
85:                         end
86: 
87:       # convert 'true' and 'false' to real booleans
88:       debug_param = params[:debug] == 'true' ? true : false
89:       
90:       # assemble and return swx file 
91:                         SwxAssembler.write_swf(service_class_response, debug_param, swx_config['compression_level'], params[:url], swx_config['allow_domain'])
92:     end

[Validate]