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.
data/NEWS.md CHANGED
@@ -1,3 +1,27 @@
1
+ 2011-11-10 Release 0.2.0
2
+ ---------------------------
3
+ - Added serve static files
4
+
5
+ ```Ruby
6
+ root :srv do
7
+ static_folder File.dirname(__FILE__) + "/public"
8
+ end
9
+ ```
10
+
11
+ - Added support XML format
12
+ - Added custom html response
13
+
14
+ ```Ruby
15
+ node :page_1 do
16
+ say "Hello"
17
+ html { <h1>#{say}</h1> }
18
+ end
19
+ ```
20
+
21
+ 2011-11-05 Release 0.1.1
22
+ ---------------------------
23
+ Resolved problem with dependency json and ruby version (support only >=1.9.2)
24
+
1
25
  2011-11-05 Release 0.1.0
2
26
  ----------------------------
3
27
  Initial release
data/README.md CHANGED
@@ -1,65 +1,129 @@
1
1
  LipaWeb [![Build Status](https://secure.travis-ci.org/flipback/lipa-web.png)](http://travis-ci.org/flipback/lipa-web)
2
2
  =======================================================
3
- LipaWeb - micro webframework based on [Lipa](http://lipa.flipbacl.new) for web access to treelike structures
3
+ LipaWeb - micro webframework based on [Lipa](http://lipa.flipback.net) for web access to treelike structures
4
4
 
5
- Features
5
+ Installation
6
6
  ----------------------------------------------------
7
- - Based on Rack and Lipa
8
- - Support HTML,JSON and text format response
9
- - Support ERB templates with layouts
7
+ `gem install lipa-web`
10
8
 
11
- Example
9
+ Get started
12
10
  ----------------------------------------------------
13
11
 
14
12
  require 'lipa-web'
15
- srv = root :universe do
16
- kind :planet_system do
17
- num_planet run{
18
- count = 0
19
- children.values.each do |planet|
20
- count += 1 if planet.kind == :planet
21
- end
22
- count
23
- }
13
+ app = root :app do
14
+ node :group do
15
+ node :node_1 do
16
+ msg "Hello World!"
17
+ end
24
18
  end
19
+ end
25
20
 
26
- kind :planet do
27
- has_live false
28
- has_water false
29
- number 0
30
- end
21
+ app.run! #Run server
22
+ # GET http://127.0.0.1:9292/group/node
31
23
 
32
- planet_system :sun_system do
33
- planet :mercury do
34
- number 1
35
- radius 46_001_210
36
- end
24
+ ###Server options
37
25
 
38
- planet :venus do
39
- number 2
40
- radius 107_476_259
41
- end
26
+ Server has default options:
42
27
 
43
- planet :earth do
44
- number 3
45
- radius 147_098_074
46
- has_live true
47
- has_water true
28
+ 1. host '127.0.0.1'
29
+ 2. port 9292
30
+ 3. server :webrik - name of server(see servers of Rack)
31
+ 4. debug false
32
+ 5. views "./views" - dir for templates
33
+ 6. static_folder "./public" - dir for static files
48
34
 
49
- node :moon, :radius => 363_104
50
- end
51
- end
35
+ You can set your options in root of description
36
+
37
+ root :app do
38
+ static_folder "path/to/dir/with/static/files"
39
+ port 80
40
+ server :thin
52
41
  end
53
42
 
54
- srv.run! #Run server
55
- # GET http://127.0.0.1:9292
43
+ ###Formats
56
44
 
57
- Installation
58
- -----------------------------------------------------
59
- `gem install lipa-web`
45
+ You can get representation of data in HTML, JSON or XML formats:
46
+
47
+ GET http://127.0.0.1:9292/group/node #=> render default HTML template for node
48
+ GET http://127.0.0.1:9292/group/node.json
49
+ #=>
50
+ {
51
+ "name":"node",
52
+ "full_name":"/group/node",
53
+ "parent": {
54
+ "name":"group",
55
+ "full_name":"/group",
56
+ }
57
+ "msg":"Hello World!"
58
+ }
59
+
60
+ GET http://127.0.0.1:9292/group/node.xml
61
+ #=>
62
+ <node>
63
+ <name>node</node>
64
+ <full_name>/group/node</full_name>
65
+ <parent>
66
+ <name>group</node>
67
+ <full_name>/group</full_name>
68
+ </parent>
69
+ <msg>Hello World!</msg>
70
+ </node>
71
+
72
+ ### Templates
73
+
74
+ Now LipaWeb is supporting only ERB templates for rendering in HTML format and Builder for XML. Use this syntax for linking templates with node:
75
+
76
+ node :node_1 do
77
+ html erb("path/to/file/in/views/dir") #HTML format
78
+ xml builder("path/to/file/in/views/dir") #XML format
79
+ end
80
+
81
+ Also you can use layout. For it:
82
+
83
+ root :app do
84
+ layout "path/to/file/views/dir"
85
+ end
86
+
87
+ ### Custom responses
88
+
89
+ LipaWeb provide custom responses in block:
90
+
91
+ node :node_1 do
92
+ msg "Hello"
93
+
94
+ html { "<h1>#{msg}</h1" }
95
+ json { |json| json[:msg] = msg }
96
+ xml { |xml| xml.msg msg }
97
+ end
98
+
99
+ All template binding with node. For example:
100
+
101
+ #*.html.erb:
102
+ <h1><%= name %></h1>
103
+ <p><%= parent.name %></h1>
104
+
105
+ #*builder
106
+ xml.node do
107
+ xml.name name
108
+ xml.parent parent.name
109
+ end
110
+
111
+ ### Lipa
112
+
113
+ LipaWeb is extension of Lipa and inherit its features:
114
+
115
+ root :app do
116
+ kind :greater do
117
+ html { <h1>Hello, #{name.to_s.capitalize}!</h1> }
118
+ end
119
+
120
+ greater :ann
121
+ greater :aleksey
122
+ #....
123
+ end
60
124
 
61
125
  Reference
62
126
  ----------------------------------
63
127
  Home page: https://github.com/flipback/lipa-web
64
128
 
65
- Lipa home page: http://lipa.flipback.net
129
+ Lipa home page: http://lipa.flipback.net
@@ -0,0 +1,33 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'../lib')
2
+ require "lipa/web"
3
+
4
+ srv = root :srv do
5
+ kind :group do
6
+ count run{
7
+ c = 0
8
+ children.values.each do |ch|
9
+ c += if ch.kind == :group
10
+ ch.count
11
+ else
12
+ 1
13
+ end
14
+ end
15
+ c
16
+ }
17
+ end
18
+
19
+ kind :object do
20
+ size 0
21
+ location "somethere"
22
+ any_attrs "..."
23
+ end
24
+
25
+ group :group_1 do
26
+ #make ten object
27
+ (1...10).each do |i|
28
+ object "object_#{i}"
29
+ end
30
+ end
31
+ end
32
+
33
+ srv.run!
@@ -25,11 +25,12 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
25
 
26
26
  require "lipa/web/version"
27
27
  require "lipa/web/helpers"
28
+ require "lipa/web/response"
28
29
  require "lipa/web/application"
29
30
  require "lipa/web/server"
30
31
 
31
32
  class Lipa::Node
32
- include Lipa::Web::NodeHelper
33
+ include Lipa::Web::Helpers::Node
33
34
  end
34
35
 
35
36
  def root(name, &block)
@@ -40,6 +41,7 @@ def root(name, &block)
40
41
  server :webrick
41
42
  debug false
42
43
  views File.join(File.absolute_path("."), "views")
44
+ static_folder File.join(File.absolute_path("."), "public")
43
45
  end
44
46
 
45
47
  root.instance_eval &block if block_given?
@@ -31,7 +31,7 @@ module Lipa
31
31
  module Web
32
32
  # Rack application
33
33
  class Application
34
- include ResponseHelper
34
+ include Response
35
35
 
36
36
  # Init app
37
37
  #
@@ -46,13 +46,18 @@ module Lipa
46
46
  if node
47
47
  respond(node, format)
48
48
  else
49
- [ 500,
50
-
51
- {"Content-Type" => "text/html"},
52
- [
53
- "Node is not existence"
49
+ static_path = File.join(@root.static_folder, env['PATH_INFO'])
50
+ if File.exist?(static_path)
51
+ [ 200, { "Content-Type" => "text/#{format}" }, [File.read(static_path)]]
52
+ else
53
+ [ 500,
54
+
55
+ {"Content-Type" => "text/html"},
56
+ [
57
+ "Node is not existence"
58
+ ]
54
59
  ]
55
- ]
60
+ end
56
61
  end
57
62
  end
58
63
 
@@ -1,3 +1,3 @@
1
- require "lipa/web/helpers/node_helper"
2
- require "lipa/web/helpers/html_helper"
3
- require "lipa/web/helpers/response_helper"
1
+ require "lipa/web/helpers/node"
2
+ require "lipa/web/helpers/html"
3
+ require "lipa/web/helpers/response"
@@ -0,0 +1,40 @@
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
+ module Lipa
27
+ module Web
28
+ module Helpers
29
+ module HTML
30
+ # Make link for node
31
+ #
32
+ # @param node [Lipa::Node] linking node
33
+ # @return [String] html <a href='path/to/node'>name_of_node</a>
34
+ def link_to(node)
35
+ %(<a href="#{node.full_name}">#{node.name}</a>)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,120 @@
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
+ module Lipa
27
+ module Web
28
+ module Helpers
29
+ module Node
30
+
31
+ # Assignment erb template fot html response
32
+ #
33
+ # @param path [String] path about views directory
34
+ #
35
+ # @example
36
+ # node :page_1 do
37
+ # html erb("page.html.erb")
38
+ # end
39
+ def erb(path)
40
+ { :render => :erb, :template => File.join(root.attrs[:views], path) }
41
+ end
42
+
43
+ # Assignment text for html response
44
+ #
45
+ # @param msg [String] text message
46
+ #
47
+ # @example
48
+ # node :page_1 do
49
+ # html text("Hello World!")
50
+ # end
51
+ def text(msg)
52
+ { :render => :text, :msg => msg }
53
+ end
54
+
55
+ # Definition html response
56
+ #
57
+ # @see NodeHelpers#erb
58
+ # @see NodeHelpers#text
59
+ def html(opts=nil,&block)
60
+ if block_given?
61
+ @html = {:block => block}
62
+ else
63
+ if opts.nil?
64
+ @html
65
+ else
66
+ @html = opts
67
+ end
68
+ end
69
+ end
70
+
71
+ # Definition json response
72
+ #
73
+ # @example
74
+ # node :josn_1 do
75
+ # json { |j| j[:name] = name }
76
+ # end
77
+ # # => { "name":"json_1" }
78
+ def json(&block)
79
+ if block_given?
80
+ @json = {:block => block}
81
+ else
82
+ @json
83
+ end
84
+ end
85
+
86
+ # Assignment template fot xml response
87
+ #
88
+ # @param path [String] path about views directory
89
+ #
90
+ # @example
91
+ # node :page_1 do
92
+ # xml builder("page.builder")
93
+ # end
94
+
95
+ def builder(path)
96
+ { :render => :builder, :template => File.join(root.attrs[:views], path) }
97
+ end
98
+
99
+ # Definition xml response
100
+ #
101
+ # @example
102
+ # node :xml_1 do
103
+ # xml { |xml| xml.msg "Hello" }
104
+ # end
105
+ # # => <msg>Hello</msg>
106
+ def xml(opts=nil, &block)
107
+ if block_given?
108
+ @xml = { :block => block }
109
+ else
110
+ if opts.nil?
111
+ @xml
112
+ else
113
+ @xml = opts
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end