Class: SwxGateway

Inherits:
Object show all
Defined in:
lib/swxruby/swx_gateway.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.app_rootObject

Returns the value of attribute app_root.



11
12
13
# File 'lib/swxruby/swx_gateway.rb', line 11

def app_root
  @app_root
end

.swx_configObject

Returns the value of attribute swx_config.



11
12
13
# File 'lib/swxruby/swx_gateway.rb', line 11

def swx_config
  @swx_config
end

Class Method Details

.init_service_classesObject

Eval all files in the services_path into the SwxServiceClasses namespace



14
15
16
17
18
19
20
# File 'lib/swxruby/swx_gateway.rb', line 14

def init_service_classes
	Dir.glob(File.join(app_root, swx_config['services_path'], './**/*.rb'))	do |filename| 
		# Load service class into SwxServiceClasses namespace. 
		SwxServiceClasses.module_eval(File.read(filename))
	end
	true
end

.json_to_ruby(arguments) ⇒ Object

:nodoc:



94
95
96
# File 'lib/swxruby/swx_gateway.rb', line 94

def json_to_ruby(arguments) #:nodoc:
  JSON.parse arguments unless arguments.nil? || arguments.empty?
end

.nillify_nulls(args_array) ⇒ Object

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



23
24
25
26
27
28
# File 'lib/swxruby/swx_gateway.rb', line 23

def nillify_nulls(args_array)
	# Convert all strings containing 'null' to nil
	args_array.collect! { |arg| if arg == 'null' then nil else arg end }
	# Return nil if the args array contained only 'null' strings
	if args_array.compact.empty? then nil else args_array end
end

.process(params) ⇒ Object

SwxGateway.process(:args => [1,2], :debug => false, :method => ‘addNumbers’, :serviceClass => ‘Simple’, :url => ‘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

Raises:

  • (NoMethodError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/swxruby/swx_gateway.rb', line 53

def process(params)
  # Set defaults if the SWX gateway isn't configured
  swx_config ||= {'compression_level' => 4, 'allow_domain' => true}
  
			# convert JSON arguments to a Ruby object
			args = json_to_ruby params[:args]
			
  unless args.nil?
    # Ensure that none of the arguments contain 'undefined'
	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' }
# Convert 'null' strings in args array to nil
 args = nillify_nulls(args)
end
			
			# Fetch the class constant for the specified service class
			validate_service_class_name(params[:serviceClass])
  service_class = class_eval("SwxServiceClasses::#{params[:serviceClass]}")

			# convert camelCased params[:method] to underscored (does nothing if params[:method] is already underscored)
			# This effectively bridges the gap between ActionScript and Ruby variable/method naming conventions.
			params[:method] = params[:method].underscore

			# Prevent nefarious use of methods that the service class inherited from Object
			raise NoMethodError unless (service_class.public_instance_methods - Object.public_instance_methods).include?(params[:method])
			
  # Instantiate the service class, call the specified method, and capture the response
			service_class_response = if args.nil?
# No args were passed, so assume the service class' method doesn't take any arguments
   service_class.new.send(params[:method])
			else
# Call the service class' method and pass in the arguments (uses an * to pass an array as multiple arguments)
   service_class.new.send(params[:method], *args)
			end

  # convert 'true' and 'false' to real booleans
  debug_param = params[:debug] == 'true' ? true : false
  
  # assemble and return swx file 
			SwxAssembler.write_swf(service_class_response, debug_param, swx_config['compression_level'], params[:url], swx_config['allow_domain'])
end

.validate_service_class_name(service_class) ⇒ Object

:nodoc:



98
99
100
101
102
# File 'lib/swxruby/swx_gateway.rb', line 98

def validate_service_class_name(service_class) #:nodoc:
	unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ service_class
    raise NameError, "#{service_class.inspect} is not a valid constant name!"
  end
end