amfora 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ pkg/*
2
+ .loadpath
3
+ .project
4
+ /rdoc/
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Dima Berastau
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,16 @@
1
+ = Amfora
2
+
3
+ RESTful Middleware that provides AMF0/AMF3 support to Rails and Rack applications.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
10
+ * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but
11
+ bump version in a commit by itself I can ignore when I pull)
12
+ * Send me a pull request. Bonus points for topic branches.
13
+
14
+ == Copyright
15
+
16
+ Copyright (c) 2009 Dima Berastau. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "amfora"
8
+ gem.summary = %Q{RESTful AMF0/AMF3 Rack Middleware}
9
+ gem.description = %Q{RESTful Rack Middleware that provides AMF0/AMF3 support to Rails and Rack applications}
10
+ gem.email = "dima.berastau@gmail.com"
11
+ gem.homepage = "http://github.com/dima/amfora"
12
+ gem.authors = ['Dima Berastau', 'Tony Hillerson', 'Stephen Augenstein']
13
+ gem.add_development_dependency "rspec"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ if File.exist?('VERSION')
40
+ version = File.read('VERSION')
41
+ else
42
+ version = ""
43
+ end
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "amfora #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/amfora.gemspec ADDED
@@ -0,0 +1,66 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{amfora}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Dima Berastau", "Tony Hillerson", "Stephen Augenstein"]
12
+ s.date = %q{2009-11-19}
13
+ s.description = %q{RESTful Rack Middleware that provides AMF0/AMF3 support to Rails and Rack applications}
14
+ s.email = %q{dima.berastau@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "amfora.gemspec",
27
+ "lib/amf.rb",
28
+ "lib/amf/active_record.rb",
29
+ "lib/amf/class_mapping.rb",
30
+ "lib/amf/constants.rb",
31
+ "lib/amf/messages.rb",
32
+ "lib/amf/pure.rb",
33
+ "lib/amf/pure/deserializer.rb",
34
+ "lib/amf/pure/io_helpers.rb",
35
+ "lib/amf/pure/remoting.rb",
36
+ "lib/amf/pure/serializer.rb",
37
+ "lib/amf/version.rb",
38
+ "lib/amfora.rb",
39
+ "lib/rack/amf.rb",
40
+ "lib/rack/amf/application.rb",
41
+ "spec/amfora_spec.rb",
42
+ "spec/spec_helper.rb"
43
+ ]
44
+ s.homepage = %q{http://github.com/dima/amfora}
45
+ s.rdoc_options = ["--charset=UTF-8"]
46
+ s.require_paths = ["lib"]
47
+ s.rubygems_version = %q{1.3.5}
48
+ s.summary = %q{RESTful AMF0/AMF3 Rack Middleware}
49
+ s.test_files = [
50
+ "spec/amfora_spec.rb",
51
+ "spec/spec_helper.rb"
52
+ ]
53
+
54
+ if s.respond_to? :specification_version then
55
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
56
+ s.specification_version = 3
57
+
58
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
59
+ s.add_development_dependency(%q<rspec>, [">= 0"])
60
+ else
61
+ s.add_dependency(%q<rspec>, [">= 0"])
62
+ end
63
+ else
64
+ s.add_dependency(%q<rspec>, [">= 0"])
65
+ end
66
+ end
data/lib/amf.rb ADDED
@@ -0,0 +1,70 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
2
+ $:.unshift "#{File.expand_path(File.dirname(__FILE__))}/amf/"
3
+
4
+ require 'amf/version'
5
+ require 'amf/class_mapping'
6
+ require 'amf/messages'
7
+
8
+ module AMF
9
+ begin
10
+ raise LoadError, 'C extentions not implemented'
11
+ rescue LoadError
12
+ require 'amf/pure'
13
+ end
14
+
15
+ ClassMapper = AMF::ClassMapping.new
16
+
17
+ class << self
18
+ # Deserialize the AMF string _source_ into a Ruby data structure and return it.
19
+ def deserialize(source, options = {})
20
+ options[:amf_version] ||= 0
21
+ if options[:amf_version] == 0
22
+ AMF::AMF0Deserializer.new.deserialize(source)
23
+ elsif options[:amf_version] == 3
24
+ AMF::AMF3Deserializer.new.deserialize(source)
25
+ else
26
+ raise AMFError, "unsupported version #{amf_version}"
27
+ end
28
+ end
29
+
30
+ # Serialize the given Ruby data structure _obj_ into an AMF stream
31
+ def serialize(obj, options = {}, &block)
32
+ options[:amf_version] ||= 3
33
+ if options[:amf_version] == 0
34
+ AMF::AMF0Serializer.new(options).serialize(obj, &block)
35
+ elsif options[:amf_version] == 3
36
+ AMF::AMF3Serializer.new(options).serialize(obj, &block)
37
+ else
38
+ raise AMFError, "unsupported version #{amf_version}"
39
+ end
40
+ end
41
+ end
42
+
43
+ # The base exception for AMF errors.
44
+ class AMFError < StandardError; end
45
+ end
46
+
47
+ if defined?(ActiveRecord::Base)
48
+ require 'amf/active_record'
49
+ Mime::Type.register 'application/x-amf', :amf
50
+ ActiveRecord::Base.send :include, AMF::ActiveRecord
51
+ end
52
+
53
+ class Array
54
+ def to_amf(options = {})
55
+ raise "Not all elements respond to to_amf" unless all? { |e| e.respond_to? :to_amf }
56
+
57
+ options[:amf_version] ||= 3
58
+
59
+ if options[:amf_version] == 3
60
+ serializer_class = AMF::AMF3Serializer
61
+ elsif options[:amf_version] == 0
62
+ serializer_class = AMF::AMF0Serializer
63
+ else
64
+ raise AMF::AMFError, "unsupported serializer version #{options[:amf_version]}"
65
+ end
66
+
67
+ serializer = serializer_class.new({:options => options})
68
+ serializer.serialize(self)
69
+ end
70
+ end
@@ -0,0 +1,60 @@
1
+ module AMF
2
+ module Serialization
3
+ class AMFSerializer < ActiveRecord::Serialization::Serializer
4
+ def initialize(record, options = {})
5
+ super(record, options)
6
+ @options[:amf_version] = 3
7
+ end
8
+
9
+ def serialize
10
+ if @options[:amf_version] == 3
11
+ serializer_class = AMF::AMF3Serializer
12
+ elsif @options[:amf_version] == 0
13
+ serializer_class = AMF::AMF0Serializer
14
+ else
15
+ raise AMF::AMFError, "unsupported serializer version #{@options[:amf_version]}"
16
+ end
17
+
18
+ serializer = serializer_class.new({:serializable_names => serializable_names, :options => @options})
19
+
20
+ serializer.serialize(@record) do |s|
21
+ add_includes do |association, records, opts|
22
+ s.stream << add_associations(association, records, opts, s)
23
+ end
24
+ yield serializer if block_given?
25
+ end
26
+ end
27
+
28
+ def add_associations(association, records, opts, serializer)
29
+ result = serializer.write_utf8_vr(association.to_s)
30
+ if records.is_a?(Enumerable)
31
+ result << records.each { |record| record.to_amf(opts) }.join("")
32
+ else
33
+ if record = @record.send(association)
34
+ result << record.to_amf(opts)
35
+ end
36
+ end
37
+ result
38
+ end
39
+ end
40
+ end
41
+
42
+ module ActiveRecord
43
+ def self.included(base)
44
+ base.send :include, InstanceMethods
45
+ end
46
+
47
+ module InstanceMethods
48
+ def to_amf(options = {}, &block)
49
+ serializer = AMF::Serialization::AMFSerializer.new(self, options)
50
+ block_given? ? serializer.to_s(&block) : serializer.to_s
51
+ end
52
+
53
+ def from_amf(amf, options = {})
54
+ options[:amf_version] ||= 3
55
+ self.attributes = AMF.deserialize(amf, options[:amf_version])
56
+ self
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,137 @@
1
+ module AMF
2
+ class ClassMapping
3
+ class MappingSet
4
+ attr_accessor :default_as_prefix
5
+
6
+ def initialize #:nodoc:
7
+ @as_mappings = {}
8
+ @ruby_mappings = {}
9
+ @default_as_prefix = ""
10
+
11
+ # Map defaults
12
+ map :as => 'flex.messaging.messages.AbstractMessage', :ruby => 'AMF::Messages::AbstractMessage'
13
+ map :as => 'flex.messaging.messages.RemotingMessage', :ruby => 'AMF::Messages::RemotingMessage'
14
+ map :as => 'flex.messaging.messages.AsyncMessage', :ruby => 'AMF::Messages::AsyncMessage'
15
+ map :as => 'flex.messaging.messages.CommandMessage', :ruby => 'AMF::Messages::CommandMessage'
16
+ map :as => 'flex.messaging.messages.AcknowledgeMessage', :ruby => 'AMF::Messages::AcknowledgeMessage'
17
+ map :as => 'flex.messaging.messages.ErrorMessage', :ruby => 'AMF::Messages::ErrorMessage'
18
+ end
19
+
20
+ # Map a given AS class to a ruby class.
21
+ #
22
+ # Use fully qualified names for both.
23
+ #
24
+ # Example:
25
+ #
26
+ # m.map :as 'com.example.Date', :ruby => 'Example::Date'
27
+ def map(params)
28
+ [:as, :ruby].each {|k| params[k] = params[k].to_s} # Convert params to strings
29
+ @as_mappings[params[:as]] = params[:ruby]
30
+ @ruby_mappings[params[:ruby]] = params[:as]
31
+ end
32
+
33
+ # Returns the AS class name for the given ruby class name, returing nil if
34
+ # not found
35
+ def get_as_class_name(class_name) #:nodoc:
36
+ unless as_class_name = @ruby_mappings[class_name.to_s]
37
+ as_class_name = "#{@default_as_prefix}#{class_name}"
38
+ end
39
+ as_class_name
40
+ end
41
+
42
+ # Returns the ruby class name for the given AS class name, returing nil if
43
+ # not found
44
+ def get_ruby_class_name(class_name) #:nodoc:
45
+ unless ruby_class_name = @as_mappings[class_name.to_s]
46
+ ruby_class_name = class_name.sub(default_as_prefix, "")
47
+ end
48
+ ruby_class_name
49
+ end
50
+ end
51
+
52
+ # Define class mappings in the block. Block is passed a MappingSet object as
53
+ # the first parameter.
54
+ #
55
+ # Example:
56
+ #
57
+ # AMF::ClassMapper.define do |m|
58
+ # m.map :as => 'AsClass', :ruby => 'RubyClass'
59
+ # end
60
+ def define #:yields: mapping_set
61
+ yield mappings
62
+ end
63
+
64
+ def default_as_prefix
65
+ mappings.default_as_prefix
66
+ end
67
+
68
+ def default_as_prefix=(value)
69
+ mappings.default_as_prefix = value
70
+ end
71
+
72
+ # Returns the AS class name for the given ruby object. Will also take a string
73
+ # containing the ruby class name
74
+ def get_as_class_name(obj)
75
+ # Get class name
76
+ if obj.is_a?(String)
77
+ ruby_class_name = obj
78
+ else
79
+ ruby_class_name = obj.class.name
80
+ end
81
+
82
+ # Get mapped AS class name
83
+ mappings.get_as_class_name(ruby_class_name)
84
+ end
85
+
86
+ def get_ruby_class_name(as_class_name)
87
+ mappings.get_ruby_class_name(as_class_name.to_s)
88
+ end
89
+
90
+ # Instantiates a ruby object using the mapping configuration based on the
91
+ # source AS class name. If there is no mapping defined, it returns a hash.
92
+ def get_ruby_obj(as_class_name)
93
+ ruby_class_name = mappings.get_ruby_class_name(as_class_name)
94
+ if ruby_class_name.nil?
95
+ # Populate a simple hash, since no mapping
96
+ return Hash.new
97
+ else
98
+ ruby_class = deep_const_get(ruby_class_name)
99
+ return ruby_class.new
100
+ end
101
+ end
102
+
103
+ # Return the constant located at _path_. The format of _path_ has to be
104
+ # either ::A::B::C or A::B::C. In any case A has to be located at the top
105
+ # level (absolute namespace path?). If there doesn't exist a constant at
106
+ # the given path, an ArgumentError is raised.
107
+ def deep_const_get(path) # :nodoc:
108
+ path = path.to_s
109
+ path.split(/::/).inject(Object) do |p, c|
110
+ case
111
+ when c.empty? then p
112
+ when p.const_defined?(c) then p.const_get(c)
113
+ else raise ArgumentError, "can't find const #{path}"
114
+ end
115
+ end
116
+ end
117
+
118
+ # Populates the ruby object using the given properties
119
+ def populate_ruby_obj(obj, props, dynamic_props=nil)
120
+ props.merge! dynamic_props if dynamic_props
121
+ hash_like = obj.respond_to?("[]=")
122
+ props.each do |key, value|
123
+ if obj.respond_to?("#{key}=")
124
+ obj.send("#{key}=", value)
125
+ elsif hash_like
126
+ obj[key.to_sym] = value
127
+ end
128
+ end
129
+ obj
130
+ end
131
+
132
+ private
133
+ def mappings
134
+ @mappings ||= MappingSet.new
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,47 @@
1
+ module AMF
2
+ # AMF0 Type Markers
3
+ AMF0_NUMBER_MARKER = 0x00 #"\000"
4
+ AMF0_BOOLEAN_MARKER = 0x01 #"\001"
5
+ AMF0_STRING_MARKER = 0x02 #"\002"
6
+ AMF0_OBJECT_MARKER = 0x03 #"\003"
7
+ AMF0_MOVIE_CLIP_MARKER = 0x04 #"\004" # Unused
8
+ AMF0_NULL_MARKER = 0x05 #"\005"
9
+ AMF0_UNDEFINED_MARKER = 0x06 #"\006"
10
+ AMF0_REFERENCE_MARKER = 0x07 #"\a"
11
+ AMF0_HASH_MARKER = 0x08 #"\b"
12
+ AMF0_OBJECT_END_MARKER = 0x09 #"\t"
13
+ AMF0_STRICT_ARRAY_MARKER = 0x0A #"\n"
14
+ AMF0_DATE_MARKER = 0x0B #"\v"
15
+ AMF0_LONG_STRING_MARKER = 0x0C #"\f"
16
+ AMF0_UNSUPPORTED_MARKER = 0x0D #"\r"
17
+ AMF0_RECORDSET_MARKER = 0x0E #"\016" # Unused
18
+ AMF0_XML_MARKER = 0x0F #"\017"
19
+ AMF0_TYPED_OBJECT_MARKER = 0x10 #"\020"
20
+ AMF0_AMF3_MARKER = 0x11 #"\021"
21
+
22
+ # AMF3 Type Markers
23
+ AMF3_UNDEFINED_MARKER = 0x00 #"\000"
24
+ AMF3_NULL_MARKER = 0x01 #"\001"
25
+ AMF3_FALSE_MARKER = 0x02 #"\002"
26
+ AMF3_TRUE_MARKER = 0x03 #"\003"
27
+ AMF3_INTEGER_MARKER = 0x04 #"\004"
28
+ AMF3_DOUBLE_MARKER = 0x05 #"\005"
29
+ AMF3_STRING_MARKER = 0x06 #"\006"
30
+ AMF3_XML_DOC_MARKER = 0x07 #"\a"
31
+ AMF3_DATE_MARKER = 0x08 #"\b"
32
+ AMF3_ARRAY_MARKER = 0x09 #"\t"
33
+ AMF3_OBJECT_MARKER = 0x0A #"\n"
34
+ AMF3_XML_MARKER = 0x0B #"\v"
35
+ AMF3_BYTE_ARRAY_MARKER = 0x0C #"\f"
36
+
37
+ # Other AMF3 Markers
38
+ AMF3_EMPTY_STRING = 0x01
39
+ AMF3_ANONYMOUS_OBJECT = 0x01
40
+ AMF3_DYNAMIC_OBJECT = 0x0B
41
+ AMF3_CLOSE_DYNAMIC_OBJECT = 0x01
42
+ AMF3_CLOSE_DYNAMIC_ARRAY = 0x01
43
+
44
+ # Other Constants
45
+ MAX_INTEGER = 268435455
46
+ MIN_INTEGER = -268435456
47
+ end