rpcoder 0.0.4 → 0.1.0

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/README.rdoc CHANGED
@@ -22,7 +22,7 @@
22
22
  RPCoder.function "getMail" do |f|
23
23
  f.path = "/mails/:id" # => ("/mails/" + id)
24
24
  f.method = "GET"
25
- f.set_return_type "Mail"
25
+ f.add_return_type :mail, "Mail"
26
26
  f.add_param :id, "int"
27
27
  f.description = 'メールを取得'
28
28
  end
@@ -30,14 +30,13 @@
30
30
  RPCoder.function "getMails" do |f|
31
31
  f.path = "/mails"
32
32
  f.method = "GET"
33
- f.set_return_type "Array", {:array_type => "Mail"}
33
+ f.add_return_type :mails, "Mail", {:array? => true}
34
34
  f.description = 'メールを送信'
35
35
  end
36
36
 
37
37
  RPCoder.function "sendMail" do |f|
38
38
  f.path = "/mails/create"
39
39
  f.method = "POST"
40
- f.set_return_type "void"
41
40
  f.add_param :subject, "String"
42
41
  f.add_param :body, "String"
43
42
  f.description = 'メールを送信'
@@ -50,9 +49,19 @@
50
49
  == code for using RPC
51
50
 
52
51
  var rpc:RPC = new RPC('http://localhost:3000');
52
+
53
53
  rpc.getMail(1, function(mail:Mail):void {
54
54
  // success
55
55
  }, function():void {
56
56
  // failure
57
57
  })
58
+
59
+ rpc.getMails(function(mails:Array):void {
60
+ // success
61
+ for each (var mail as Mail in mails)
62
+ {
63
+ }
64
+ }, function():void {
65
+ // failure
66
+ })
58
67
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.1.0
@@ -1,3 +1,5 @@
1
+ require 'rpcoder/param'
2
+
1
3
  module RPCoder
2
4
  class Function
3
5
  PARAMS_IN_URL_RE = /:[\w\d]+/
@@ -7,6 +9,14 @@ module RPCoder
7
9
  @params ||= []
8
10
  end
9
11
 
12
+ def return_types
13
+ @return_types ||= []
14
+ end
15
+
16
+ def has_return_type?
17
+ !return_types.empty?
18
+ end
19
+
10
20
  def add_param(name, type, options = {})
11
21
  params << Param.new(name, type, options)
12
22
  end
@@ -29,28 +39,8 @@ module RPCoder
29
39
  params.select { |i| !param_strs.include?(i.name.to_s) }
30
40
  end
31
41
 
32
- def set_return_type(type, options = {})
33
- @return_type = type
34
- @return_type_options = options
35
- end
36
-
37
- def return_type_instance_creator(elem)
38
- type = @return_type
39
- type = @return_type_options[:array_type] if @return_type == 'Array'
40
- if Type.original_types.include?(type)
41
- elem
42
- else
43
- "new #{type}(#{elem})"
44
- end
45
- end
46
-
47
- class Param
48
- attr_accessor :name, :type, :options
49
- def initialize(name, type, options = {})
50
- @name = name
51
- @type = type
52
- @options = options
53
- end
42
+ def add_return_type(name, type, options = {})
43
+ return_types << Param.new(name, type, options)
54
44
  end
55
45
  end
56
46
  end
@@ -0,0 +1,45 @@
1
+ module RPCoder
2
+ class Param
3
+ def self.original_types
4
+ [:int, :String, :Boolean, :Array]
5
+ end
6
+
7
+ attr_accessor :name, :type, :options
8
+ def initialize(name, type, options = {})
9
+ @name = name
10
+ @type = type
11
+ @options = options
12
+ end
13
+
14
+ def array?
15
+ options[:array?]
16
+ end
17
+
18
+ def original_type?
19
+ Param.original_types.include?(type.to_sym)
20
+ end
21
+
22
+ def array_param
23
+ Param.new(name, options[:array_type])
24
+ end
25
+
26
+ def instance_creator(elem = 'elem', options = {})
27
+ elem = element_accessor(elem, options)
28
+ if original_type?
29
+ elem
30
+ else
31
+ "new #{type}(#{elem})"
32
+ end
33
+ end
34
+
35
+ def element_accessor(elem = 'elem', options = {})
36
+ if options[:object?]
37
+ "object['#{elem}']"
38
+ else
39
+ elem
40
+ end
41
+ end
42
+
43
+ end
44
+ end
45
+
data/lib/rpcoder/type.rb CHANGED
@@ -1,11 +1,9 @@
1
+ require 'rpcoder/param'
2
+
1
3
  module RPCoder
2
4
  class Type
3
5
  attr_accessor :name, :description
4
6
 
5
- def self.original_types
6
- [:int, :String, :Boolean, :Array]
7
- end
8
-
9
7
  def fields
10
8
  @fields ||= []
11
9
  end
@@ -14,36 +12,7 @@ module RPCoder
14
12
  fields << Field.new(name, type, options)
15
13
  end
16
14
 
17
- class Field
18
- attr_accessor :name, :type, :options
19
- def initialize(name, type, options = {})
20
- @name = name
21
- @type = type
22
- @options = options
23
- end
24
-
25
- def original_type?
26
- Type.original_types.include?(@type.to_sym)
27
- end
28
-
29
- def array?
30
- @type.to_sym == :Array
31
- end
32
-
33
- def array_field
34
- Field.new(name, options[:array_type])
35
- end
36
-
37
- def instance_creator(elem = nil, options = {})
38
- elem = name if elem.nil?
39
- if original_type?
40
- "object['#{name}']"
41
- elsif options[:direct]
42
- "new #{type}(#{elem})"
43
- else
44
- "new #{type}(object['#{elem}'])"
45
- end
46
- end
15
+ class Field < Param
47
16
  end
48
17
  end
49
18
  end
@@ -105,21 +105,29 @@ package <%= name_space %>
105
105
  request("<%= func.method %>", <%= func.path_parts.join(' + ') %>, params,
106
106
  function(e:ResultEvent, t:Object):void {
107
107
  t = t; // FIXME: for removing warning
108
- <%- if func.return_type && func.return_type == 'Array' -%>
109
108
  var hash:Object = JSON.decode(e.result as String);
110
- var array:Array = new Array();
109
+
110
+ <%- func.return_types.each do |param| -%>
111
+ <%- if param.array? -%>
112
+ var <%= param.name %>_list:Array = new Array();
113
+ for each (var <%= param.name %>:Object in hash['<%= param.name %>'])
114
+ <%= param.name %>_list.push(<%= param.instance_creator(param.name) %>);
115
+ <%- end -%>
116
+ <%- end -%>
117
+
111
118
  <%-
112
- @elem_count ||= 0
113
- @elem_count += 1
119
+ args = []
120
+ if func.has_return_type?
121
+ args = func.return_types.map {|param|
122
+ if param.array?
123
+ "#{param.name}_list"
124
+ else
125
+ param.instance_creator("hash['#{param.name}']")
126
+ end
127
+ }
128
+ end
114
129
  -%>
115
- for each (var elem<%= @elem_count %>:Object in hash)
116
- array.push(<%= func.return_type_instance_creator("elem#{@elem_count}") %>);
117
- success(array);
118
- <%- elsif func.return_type != 'void' -%>
119
- success(<%= func.return_type_instance_creator('JSON.decode(e.result as String)') %>);
120
- <%- else -%>
121
- success();
122
- <%- end -%>
130
+ success(<%= args.join(', ') %>);
123
131
  },
124
132
  function(e:FaultEvent, t:Object):void {
125
133
  t = t; // FIXME: for removing warning
@@ -23,9 +23,9 @@ package <%= name_space %>
23
23
  <%- if field.array? -%>
24
24
  _<%= field.name %> = new Array();
25
25
  for each(var elem:Object in object['<%= field.name %>'])
26
- _<%= field.name %>.push(<%= field.array_field.instance_creator(:elem, {:direct => true}) %>);
26
+ _<%= field.name %>.push(<%= field.instance_creator(:elem) %>);
27
27
  <%- else -%>
28
- _<%= field.name %> = <%= field.instance_creator %>;
28
+ _<%= field.name %> = <%= field.instance_creator(field.name, {:object? => true}) %>;
29
29
  <%- end -%>
30
30
  <%- end -%>
31
31
  }
data/rpcoder.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rpcoder}
8
- s.version = "0.0.4"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["jugyo", "Toshiyuki Hirooka"]
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
25
25
  "VERSION",
26
26
  "lib/rpcoder.rb",
27
27
  "lib/rpcoder/function.rb",
28
+ "lib/rpcoder/param.rb",
28
29
  "lib/rpcoder/type.rb",
29
30
  "lib/templates/API.erb",
30
31
  "lib/templates/Type.erb",
@@ -32,6 +33,7 @@ Gem::Specification.new do |s|
32
33
  "spec/fixtures/foo/bar/API.as",
33
34
  "spec/fixtures/foo/bar/Mail.as",
34
35
  "spec/rpcoder/function_spec.rb",
36
+ "spec/rpcoder/param_spec.rb",
35
37
  "spec/rpcoder_spec.rb",
36
38
  "spec/spec_helper.rb"
37
39
  ]
@@ -42,6 +44,7 @@ Gem::Specification.new do |s|
42
44
  s.summary = %q{Simple RPC generator for as3}
43
45
  s.test_files = [
44
46
  "spec/rpcoder/function_spec.rb",
47
+ "spec/rpcoder/param_spec.rb",
45
48
  "spec/rpcoder_spec.rb",
46
49
  "spec/spec_helper.rb"
47
50
  ]
@@ -102,7 +102,10 @@ package foo.bar
102
102
  request("GET", "/mails/" + id, params,
103
103
  function(e:ResultEvent, t:Object):void {
104
104
  t = t; // FIXME: for removing warning
105
- success(new Mail(JSON.decode(e.result as String)));
105
+ var hash:Object = JSON.decode(e.result as String);
106
+
107
+
108
+ success(new Mail(hash['mail']));
106
109
  },
107
110
  function(e:FaultEvent, t:Object):void {
108
111
  t = t; // FIXME: for removing warning
@@ -124,10 +127,12 @@ package foo.bar
124
127
  function(e:ResultEvent, t:Object):void {
125
128
  t = t; // FIXME: for removing warning
126
129
  var hash:Object = JSON.decode(e.result as String);
127
- var array:Array = new Array();
128
- for each (var elem1:Object in hash)
129
- array.push(new Mail(elem1));
130
- success(array);
130
+
131
+ var mails_list:Array = new Array();
132
+ for each (var mails:Object in hash['mails'])
133
+ mails_list.push(new Mail(mails));
134
+
135
+ success(mails_list, hash['count']);
131
136
  },
132
137
  function(e:FaultEvent, t:Object):void {
133
138
  t = t; // FIXME: for removing warning
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ module RPCoder
4
+ describe "Param" do
5
+ context do
6
+ let(:param) { Param.new(:foo, 'int') }
7
+ subject { param }
8
+ it { subject.name.should == :foo }
9
+ it { subject.type.should == 'int' }
10
+ it { should_not be_array }
11
+ end
12
+
13
+ context do
14
+ let(:param) { Param.new(:foo, 'int', {:array? => true}) }
15
+ subject { param }
16
+ it { should be_array }
17
+ end
18
+
19
+ context do
20
+ it { Param.new(:foo, 'int').instance_creator.should == 'elem' }
21
+ it { Param.new(:foo, 'Foo').instance_creator.should == 'new Foo(elem)' }
22
+ it { Param.new(:foo, 'int').instance_creator('bar').should == 'bar' }
23
+ it { Param.new(:foo, 'int').instance_creator('bar', {:object? => true}).should == "object['bar']" }
24
+ it { Param.new(:foo, 'Foo').instance_creator('bar', {:object? => true}).should == "new Foo(object['bar'])" }
25
+ end
26
+
27
+ end
28
+ end
29
+
data/spec/rpcoder_spec.rb CHANGED
@@ -12,7 +12,7 @@ describe "RPCoder" do
12
12
  f.path = "/mails/:id" # => ("/mails/" + id)
13
13
  f.description = 'get mail'
14
14
  f.method = "GET"
15
- f.set_return_type "Mail"
15
+ f.add_return_type :mail, "Mail"
16
16
  f.add_param :id, :int
17
17
  f.add_param :foo, :String, :expect => ["A","B"]
18
18
  f.add_param :bar, :Array
@@ -23,7 +23,8 @@ describe "RPCoder" do
23
23
  f.path = "/mails/"
24
24
  f.description = 'get mails'
25
25
  f.method = "GET"
26
- f.set_return_type "Array", {:array_type => "Mail"}
26
+ f.add_return_type :mails, "Mail", {:array? => true}
27
+ f.add_return_type :count, "int"
27
28
  end
28
29
 
29
30
  RPCoder.type "Mail" do |t|
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rpcoder
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.4
5
+ version: 0.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - jugyo
@@ -97,6 +97,7 @@ files:
97
97
  - VERSION
98
98
  - lib/rpcoder.rb
99
99
  - lib/rpcoder/function.rb
100
+ - lib/rpcoder/param.rb
100
101
  - lib/rpcoder/type.rb
101
102
  - lib/templates/API.erb
102
103
  - lib/templates/Type.erb
@@ -104,6 +105,7 @@ files:
104
105
  - spec/fixtures/foo/bar/API.as
105
106
  - spec/fixtures/foo/bar/Mail.as
106
107
  - spec/rpcoder/function_spec.rb
108
+ - spec/rpcoder/param_spec.rb
107
109
  - spec/rpcoder_spec.rb
108
110
  - spec/spec_helper.rb
109
111
  homepage: http://github.com/one-up/rpcoder
@@ -119,7 +121,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
119
121
  requirements:
120
122
  - - ">="
121
123
  - !ruby/object:Gem::Version
122
- hash: 3871653312860039459
124
+ hash: 26553161876268994
123
125
  segments:
124
126
  - 0
125
127
  version: "0"
@@ -138,5 +140,6 @@ specification_version: 3
138
140
  summary: Simple RPC generator for as3
139
141
  test_files:
140
142
  - spec/rpcoder/function_spec.rb
143
+ - spec/rpcoder/param_spec.rb
141
144
  - spec/rpcoder_spec.rb
142
145
  - spec/spec_helper.rb