atoms 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README +0 -0
- data/Rakefile +2 -0
- data/atoms.gemspec +20 -0
- data/lib/atoms.rb +142 -0
- data/lib/atoms/version.rb +3 -0
- metadata +63 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README
ADDED
File without changes
|
data/Rakefile
ADDED
data/atoms.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require "atoms/version"
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "atoms"
|
6
|
+
s.version = Atoms::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["rusintez"]
|
9
|
+
s.email = ["rusintez@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{atoms = cool.io + rocketamf + grape}
|
12
|
+
s.description = %q{an experiment of using cool.io for serving AMF3 to flash clients with API written in grape style}
|
13
|
+
|
14
|
+
s.rubyforge_project = "atoms"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
end
|
data/lib/atoms.rb
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
require 'cool.io'
|
2
|
+
require 'stringio'
|
3
|
+
require 'rocketamf'
|
4
|
+
|
5
|
+
module Atoms
|
6
|
+
class Server
|
7
|
+
|
8
|
+
def self.run(host = "0.0.0.0", port = 5000)
|
9
|
+
@server = Cool.io::TCPServer.new(host, port, Atoms::Connection)
|
10
|
+
@server.attach(Cool.io::Loop.default)
|
11
|
+
puts "Echo server listening on #{host}:#{port}"
|
12
|
+
Cool.io::Loop.default.run
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
class Router
|
18
|
+
|
19
|
+
attr_accessor :routes
|
20
|
+
|
21
|
+
class << self
|
22
|
+
|
23
|
+
attr_accessor :endpoints
|
24
|
+
|
25
|
+
def config(&block)
|
26
|
+
@router = new
|
27
|
+
@router.routes = []
|
28
|
+
instance_eval(&block)
|
29
|
+
end
|
30
|
+
|
31
|
+
def attach(c)
|
32
|
+
@router.routes << c.new
|
33
|
+
end
|
34
|
+
|
35
|
+
def add(endpoint)
|
36
|
+
self.endpoints = [] unless self.endpoints
|
37
|
+
self.endpoints << endpoint
|
38
|
+
end
|
39
|
+
|
40
|
+
def respond(header, data = {})
|
41
|
+
index = endpoints.index{|e| e.header == header}
|
42
|
+
endpoint = endpoints[index] if index
|
43
|
+
endpoint.call(data) if endpoint # encode to rocket_amf and send back to server
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class Route
|
49
|
+
|
50
|
+
#self.class.to_s
|
51
|
+
|
52
|
+
class << self
|
53
|
+
|
54
|
+
attr_accessor :ns
|
55
|
+
|
56
|
+
def namespace(space = nil, &block)
|
57
|
+
@ns ||= []
|
58
|
+
@ns << space
|
59
|
+
nest(block)
|
60
|
+
@ns.delete(space)
|
61
|
+
end
|
62
|
+
|
63
|
+
alias_method :group, :namespace
|
64
|
+
alias_method :resource, :namespace
|
65
|
+
alias_method :resources, :namespace
|
66
|
+
|
67
|
+
def nest(*blocks)
|
68
|
+
blocks.reject!{ |b| b.nil? }
|
69
|
+
if blocks.any?
|
70
|
+
blocks.each {|b| instance_eval &b}
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def get(path, &block);
|
75
|
+
endpoint = Atoms::Endpoint.generate(header(path), &block)
|
76
|
+
Atoms::Router.add endpoint
|
77
|
+
end
|
78
|
+
|
79
|
+
def header(path)
|
80
|
+
parts = []
|
81
|
+
parts << self.to_s
|
82
|
+
#parts << version.to_s if version
|
83
|
+
parts << ns.collect{ |n| n.capitalize }.join('.') if ns
|
84
|
+
#parts << ns.to_s.capitalize if ns
|
85
|
+
parts << path.to_s.capitalize if path
|
86
|
+
p = parts.join('.')
|
87
|
+
puts p
|
88
|
+
p
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
class Endpoint
|
94
|
+
|
95
|
+
attr_reader :params
|
96
|
+
|
97
|
+
def params
|
98
|
+
@params ||= {}
|
99
|
+
end
|
100
|
+
|
101
|
+
def call(prms)
|
102
|
+
@params = prms
|
103
|
+
response = instance_eval &self.class.block
|
104
|
+
end
|
105
|
+
|
106
|
+
class << self
|
107
|
+
|
108
|
+
attr_accessor :block, :header
|
109
|
+
|
110
|
+
def generate(header, &block)
|
111
|
+
c = Class.new(Atoms::Endpoint)
|
112
|
+
c.class_eval do
|
113
|
+
@header = header
|
114
|
+
@block = block
|
115
|
+
end
|
116
|
+
c
|
117
|
+
end
|
118
|
+
|
119
|
+
def call(prms)
|
120
|
+
new.call(prms)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
class Connection < Cool.io::TCPSocket
|
126
|
+
|
127
|
+
def on_connect
|
128
|
+
#puts "#{remote_addr}:#{remote_port} connected"
|
129
|
+
# todo - create a session
|
130
|
+
end
|
131
|
+
|
132
|
+
def on_close
|
133
|
+
#puts "#{remote_addr}:#{remote_port} disconnected"
|
134
|
+
#todo - destroy session
|
135
|
+
end
|
136
|
+
|
137
|
+
def on_read(data)
|
138
|
+
request = RocketAMF.deserialize(data,3)
|
139
|
+
write RocketAMF.serialize(Atoms::Router.respond(request[:header], request[:data]),3)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: atoms
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.2
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- rusintez
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-02-08 00:00:00 +08:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: an experiment of using cool.io for serving AMF3 to flash clients with API written in grape style
|
18
|
+
email:
|
19
|
+
- rusintez@gmail.com
|
20
|
+
executables: []
|
21
|
+
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files: []
|
25
|
+
|
26
|
+
files:
|
27
|
+
- .gitignore
|
28
|
+
- Gemfile
|
29
|
+
- README
|
30
|
+
- Rakefile
|
31
|
+
- atoms.gemspec
|
32
|
+
- lib/atoms.rb
|
33
|
+
- lib/atoms/version.rb
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: ""
|
36
|
+
licenses: []
|
37
|
+
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project: atoms
|
58
|
+
rubygems_version: 1.5.0
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: atoms = cool.io + rocketamf + grape
|
62
|
+
test_files: []
|
63
|
+
|