lipa-web 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,36 @@
1
+ require File.join(File.dirname(__FILE__), "..", "spec_helper")
2
+
3
+ describe Lipa::Web::Application do
4
+ include Rack::Test::Methods
5
+
6
+ before :each do
7
+ @srv = root :srv do
8
+ example_node
9
+
10
+ node :node_man_json do
11
+ json { |j| j[:name] = name }
12
+ end
13
+ end
14
+ end
15
+
16
+ def app
17
+ Lipa::Web::Application.new(@srv)
18
+ end
19
+
20
+ it 'should have default json response' do
21
+ get "group/test_node.json"
22
+
23
+ last_response.header['Content-Type'].should eql("application/json")
24
+ last_response.body.gsub(/\s*/,'').should == fixture("node.json").gsub(/\s*/,'')
25
+ end
26
+
27
+ it 'should have custom json response' do
28
+ get "node_man_json.json"
29
+
30
+ last_response.header['Content-Type'].should eql("application/json")
31
+ last_response.body.should == '{"name":"node_man_json"}'
32
+ end
33
+
34
+ end
35
+
36
+
@@ -0,0 +1,48 @@
1
+ require File.join(File.dirname(__FILE__), "..", "spec_helper")
2
+
3
+ describe Lipa::Web::Application do
4
+ include Rack::Test::Methods
5
+
6
+ before :each do
7
+ @srv = root :srv do
8
+ views File.join(File.dirname(__FILE__), "views")
9
+
10
+ example_node
11
+
12
+ node :node_with_template do
13
+ xml builder("my_template.builder")
14
+ end
15
+
16
+ node :node_man_xml do
17
+ xml { |xml| xml.name name }
18
+ end
19
+ end
20
+ end
21
+
22
+ def app
23
+ Lipa::Web::Application.new(@srv)
24
+ end
25
+
26
+ it 'should have default xml response' do
27
+ get "group/test_node.xml"
28
+
29
+ last_response.header['Content-Type'].should eql("application/xml")
30
+ last_response.body.gsub(/\s*/,'').should == fixture("node.xml").gsub(/\s*/,'')
31
+ end
32
+
33
+ it 'should render custom xml template' do
34
+ get "/node_with_template.xml"
35
+
36
+ last_response.header['Content-Type'].should eql("application/xml")
37
+ last_response.body.gsub(/\s*/, '').should == fixture("node_with_template.xml").gsub(/\s*/,'')
38
+ end
39
+
40
+ it 'should have custom xml response' do
41
+ get "/node_man_xml.xml"
42
+
43
+
44
+ last_response.header['Content-Type'].should eql("application/xml")
45
+ last_response.body.gsub(/\s*/, '').should eql("<name>node_man_xml</name>")
46
+ end
47
+ end
48
+
@@ -39,9 +39,12 @@ describe Lipa::Web::Server do
39
39
  end
40
40
 
41
41
  it 'should default path to dir of views eql "./views"' do
42
- @srv.run!
43
42
  @srv.views.should eql(File.join(File.absolute_path("."), "views"))
44
43
  end
44
+
45
+ it 'should default public folder of views eql "./public"' do
46
+ @srv.static_folder.should eql(File.join(File.absolute_path("."), "public"))
47
+ end
45
48
 
46
49
  def test_server(srv, opts)
47
50
  Rack::Server.should_receive(:start).with(opts).and_return(nil)
@@ -0,0 +1,26 @@
1
+ require "lipa/web"
2
+ require "rack/test"
3
+
4
+ def fixture(name)
5
+ path = File.join(File.dirname(__FILE__), "fixtures", name)
6
+ File.open(path).read.gsub(/^\s*\n/,'')
7
+ end
8
+
9
+ def example_node
10
+ node :group do
11
+ node :test_node do
12
+ param_int 20
13
+ param_bool false
14
+ param_float 32.2
15
+ param_string "Hello"
16
+ param_time Time.new(2000,"jan",1,20,15,1,0)
17
+ param_proc run{5+3}
18
+ param_ref ref("../other_node")
19
+
20
+ node :child_1
21
+ node :child_2
22
+ end
23
+
24
+ node :other_node
25
+ end
26
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lipa-web
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-05 00:00:00.000000000 Z
12
+ date: 2011-11-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: lipa
16
- requirement: &71011880 !ruby/object:Gem::Requirement
16
+ requirement: &12870400 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *71011880
24
+ version_requirements: *12870400
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rack
27
- requirement: &71011620 !ruby/object:Gem::Requirement
27
+ requirement: &12869580 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.3.5
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *71011620
35
+ version_requirements: *12869580
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: json
38
- requirement: &71011360 !ruby/object:Gem::Requirement
38
+ requirement: &12868760 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,21 @@ dependencies:
43
43
  version: 1.6.1
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *71011360
46
+ version_requirements: *12868760
47
+ - !ruby/object:Gem::Dependency
48
+ name: builder
49
+ requirement: &12867780 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *12867780
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: rake
49
- requirement: &71010610 !ruby/object:Gem::Requirement
60
+ requirement: &12866500 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ! '>='
@@ -54,10 +65,10 @@ dependencies:
54
65
  version: 0.9.2.2
55
66
  type: :development
56
67
  prerelease: false
57
- version_requirements: *71010610
68
+ version_requirements: *12866500
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: bundler
60
- requirement: &71010290 !ruby/object:Gem::Requirement
71
+ requirement: &13291520 !ruby/object:Gem::Requirement
61
72
  none: false
62
73
  requirements:
63
74
  - - ! '>='
@@ -65,10 +76,10 @@ dependencies:
65
76
  version: '0'
66
77
  type: :development
67
78
  prerelease: false
68
- version_requirements: *71010290
79
+ version_requirements: *13291520
69
80
  - !ruby/object:Gem::Dependency
70
81
  name: rspec
71
- requirement: &71009740 !ruby/object:Gem::Requirement
82
+ requirement: &13290180 !ruby/object:Gem::Requirement
72
83
  none: false
73
84
  requirements:
74
85
  - - ! '>='
@@ -76,10 +87,10 @@ dependencies:
76
87
  version: 2.7.0
77
88
  type: :development
78
89
  prerelease: false
79
- version_requirements: *71009740
90
+ version_requirements: *13290180
80
91
  - !ruby/object:Gem::Dependency
81
92
  name: yard
82
- requirement: &71009510 !ruby/object:Gem::Requirement
93
+ requirement: &13289020 !ruby/object:Gem::Requirement
83
94
  none: false
84
95
  requirements:
85
96
  - - ! '>='
@@ -87,10 +98,10 @@ dependencies:
87
98
  version: '0'
88
99
  type: :development
89
100
  prerelease: false
90
- version_requirements: *71009510
101
+ version_requirements: *13289020
91
102
  - !ruby/object:Gem::Dependency
92
103
  name: rdiscount
93
- requirement: &71009170 !ruby/object:Gem::Requirement
104
+ requirement: &13287940 !ruby/object:Gem::Requirement
94
105
  none: false
95
106
  requirements:
96
107
  - - ! '>='
@@ -98,10 +109,10 @@ dependencies:
98
109
  version: '0'
99
110
  type: :development
100
111
  prerelease: false
101
- version_requirements: *71009170
112
+ version_requirements: *13287940
102
113
  - !ruby/object:Gem::Dependency
103
114
  name: pry
104
- requirement: &71008760 !ruby/object:Gem::Requirement
115
+ requirement: &13286980 !ruby/object:Gem::Requirement
105
116
  none: false
106
117
  requirements:
107
118
  - - ! '>='
@@ -109,10 +120,10 @@ dependencies:
109
120
  version: '0'
110
121
  type: :development
111
122
  prerelease: false
112
- version_requirements: *71008760
123
+ version_requirements: *13286980
113
124
  - !ruby/object:Gem::Dependency
114
125
  name: notes
115
- requirement: &71008270 !ruby/object:Gem::Requirement
126
+ requirement: &13285260 !ruby/object:Gem::Requirement
116
127
  none: false
117
128
  requirements:
118
129
  - - ! '>='
@@ -120,10 +131,10 @@ dependencies:
120
131
  version: '0'
121
132
  type: :development
122
133
  prerelease: false
123
- version_requirements: *71008270
134
+ version_requirements: *13285260
124
135
  - !ruby/object:Gem::Dependency
125
136
  name: rack-test
126
- requirement: &71007960 !ruby/object:Gem::Requirement
137
+ requirement: &13278700 !ruby/object:Gem::Requirement
127
138
  none: false
128
139
  requirements:
129
140
  - - ~>
@@ -131,10 +142,10 @@ dependencies:
131
142
  version: 0.6.1
132
143
  type: :development
133
144
  prerelease: false
134
- version_requirements: *71007960
145
+ version_requirements: *13278700
135
146
  - !ruby/object:Gem::Dependency
136
147
  name: guard-rspec
137
- requirement: &71007580 !ruby/object:Gem::Requirement
148
+ requirement: &13276780 !ruby/object:Gem::Requirement
138
149
  none: false
139
150
  requirements:
140
151
  - - ! '>='
@@ -142,7 +153,7 @@ dependencies:
142
153
  version: '0'
143
154
  type: :development
144
155
  prerelease: false
145
- version_requirements: *71007580
156
+ version_requirements: *13276780
146
157
  description:
147
158
  email: atimin@gmail.com
148
159
  executables: []
@@ -151,18 +162,27 @@ extra_rdoc_files:
151
162
  - README.md
152
163
  - NEWS.md
153
164
  files:
154
- - lib/lipa/web/application.rb
155
165
  - lib/lipa/web/version.rb
156
- - lib/lipa/web/helpers/html_helper.rb
157
- - lib/lipa/web/helpers/response_helper.rb
158
- - lib/lipa/web/helpers/node_helper.rb
166
+ - lib/lipa/web/response.rb
159
167
  - lib/lipa/web/helpers.rb
168
+ - lib/lipa/web/application.rb
169
+ - lib/lipa/web/helpers/node.rb
170
+ - lib/lipa/web/helpers/response.rb
171
+ - lib/lipa/web/helpers/html.rb
160
172
  - lib/lipa/web/server.rb
173
+ - lib/lipa/web/response/json.rb
174
+ - lib/lipa/web/response/xml.rb
175
+ - lib/lipa/web/response/html.rb
161
176
  - lib/lipa/web.rb
162
177
  - spec/server_spec.rb
178
+ - spec/spec_helper.rb
163
179
  - spec/application_spec.rb
180
+ - spec/formats/xml_spec.rb
181
+ - spec/formats/json_spec.rb
182
+ - spec/formats/html_spec.rb
164
183
  - examples/universe.rb
165
184
  - examples/hello_word.rb
185
+ - examples/many_objects.rb
166
186
  - Rakefile
167
187
  - README.md
168
188
  - NEWS.md
@@ -1,128 +0,0 @@
1
- =begin
2
- Web access to Lipa
3
-
4
- Copyright (c) 2011 Aleksey Timin
5
-
6
- Permission is hereby granted, free of charge, to any person obtaining
7
- a copy of this software and associated documentation files (the
8
- 'Software'), to deal in the Software without restriction, including
9
- without limitation the rights to use, copy, modify, merge, publish,
10
- distribute, sublicense, and/or sell copies of the Software, and to
11
- permit persons to whom the Software is furnished to do so, subject to
12
- the following conditions:
13
-
14
- The above copyright notice and this permission notice shall be
15
- included in all copies or substantial portions of the Software.
16
-
17
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
18
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
- =end
25
-
26
- require "json"
27
-
28
- module Lipa
29
- module Web
30
- module ResponseHelper
31
- # Rendering node data in format
32
- #
33
- # @param node [Lipa::Node] node for rendering
34
- # @param format of rendering
35
- def respond(node, format)
36
- case format.to_s
37
- when "json"
38
- json_format(node)
39
- else
40
- html_format(node)
41
- end
42
- end
43
-
44
- private
45
- def context(node)
46
- node.instance_eval("def binding_for(#{node.attrs.keys.join(",")}) binding; end")
47
- node.extend(HtmlHelper)
48
- block = block_given? ? Proc.new : nil
49
- node.binding_for(*node.eval_attrs.values, &block)
50
- end
51
-
52
- def read_template(path)
53
- File.open(path).read
54
- end
55
-
56
- def default_template
57
- path = File.join(File.dirname(__FILE__), '..', 'views', 'node.html.erb') # default path
58
- read_template(path)
59
- end
60
-
61
- def render_erb(node)
62
- template = read_template(node.html[:template])
63
- root = node.root
64
- if root.layout
65
- layout = read_template(File.join(root.views, root.layout))
66
- ERB.new(layout).result(context(node) { ERB.new(template).result(context(node)) })
67
- else
68
- ERB.new(template).result(context(node))
69
- end
70
- end
71
-
72
- def html_format(node)
73
- status = 200
74
- header = {}
75
- body = ""
76
-
77
- if node.html
78
- case node.html[:render]
79
- when :erb
80
- body = render_erb(node)
81
- header["Content-Type"] = "text/html"
82
- when :text
83
- body = node.html[:msg]
84
- header["Content-Type"] = "text/plain"
85
- end
86
- else
87
- body = ERB.new(default_template).result(context(node))
88
- end
89
-
90
- [ status, header, [body]]
91
- end
92
-
93
- def json_format(node)
94
- status = 200
95
- header = {}
96
- body = ""
97
-
98
- if node.json
99
- j = {}
100
- node.json[:block].call(j)
101
- body = j.to_json
102
- else
103
- body = render_default_json(node)
104
- end
105
-
106
- header["Content-Type"] = "application/json"
107
- [ status, header, [body]]
108
- end
109
-
110
- def render_default_json(node)
111
- j = {}
112
- j[:name] = node.name
113
- j[:full_name] = node.full_name
114
- j[:parent] = json_link_to(node.parent)
115
- j[:children] = node.children.values.each.map {|ch| json_link_to(ch) }
116
- node.eval_attrs.each_pair do |k,v|
117
- j[k] = v.kind_of?(Lipa::Node) ? json_link_to(v) : v
118
- end
119
-
120
- j.to_json
121
- end
122
-
123
- def json_link_to(node)
124
- { :name => node.name, :full_name => node.full_name }
125
- end
126
- end
127
- end
128
- end