wire-framework 0.1.4.26 → 0.1.5

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.
@@ -1,106 +1,127 @@
1
- require_relative '../render'
1
+ ##
2
+ # Copyright 2017 Bryan T. Meyers
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ ##
2
16
 
3
- module Render
4
- # Partials are URI mapped renderers which generate only a piece of a document
5
- # @author Bryan T.Meyers
6
- module Partial
7
- extend Render
17
+ require 'rest-less'
18
+ require 'tilt'
8
19
 
9
- # DSL method to enable forwarding to remote
10
- # @return [void]
11
- def self.use_forward
12
- $current_resource[:forward] = true
13
- end
20
+ module Render
21
+ # Partials are URI mapped renderers which generate only a piece of a document
22
+ # @author Bryan T.Meyers
23
+ module Partial
14
24
 
15
- # DSL method to pull in Source like objects
16
- # @param [Symbol] name the key for this item
17
- # @param [Hash] path the remote sub-URI for this item
18
- # @return [void]
19
- def self.extra(name, path)
20
- unless $current_resource[:sources]
21
- $current_resource[:sources] = {}
22
- end
23
- $current_resource[:sources][name] = path
24
- end
25
+ # Configure repo with listing template
26
+ # @param [Hash] conf the raw configuration
27
+ # @return [Hash] post-processed configuration
28
+ def self.configure(conf)
29
+ conf['resources'].each do |k, v|
30
+ if v.is_a? Hash
31
+ conf['resources'][k]['multiple'] = Tilt.new(v['multiple'], 1, { ugly: true })
32
+ conf['resources'][k]['single'] = Tilt.new(v['single'], 1, { ugly: true })
33
+ elsif v.is_a? String
34
+ #TODO: fix needless duplication
35
+ conf['resources'][k]['multiple'] = Tilt.new(v, 1, { ugly: true })
36
+ conf['resources'][k]['single'] = Tilt.new(v, 1, { ugly: true })
37
+ end
38
+ end
39
+ conf
40
+ end
25
41
 
26
- # Read a listing and render to HTML
27
- # @param [Array] actions the allowed actions for this URI
28
- # @param [Hash] context the context for this request
29
- # @return [Response] a Rack Response triplet, or status code
30
- def self.do_read_all(actions, context)
31
- resource = context.uri[2]
32
- body = ''
33
- mime = ''
34
- if context.resource[:forward]
35
- response = forward(:readAll, context)
36
- return response if response[0] != 200
37
- mime = response[1][:content_type]
38
- body = response[2]
39
- end
40
- template = context.resource[:multiple]
41
- if template
42
- hash = { actions: actions, resource: resource, mime: mime, response: body }
43
- if context.resource[:sources]
44
- context.resource[:sources].each do |k, v|
45
- hash[k] = RL.request(:get,
46
- "http://#{context.app[:remote_host]}/#{v}",
47
- {remote_user: context.user}
48
- )[2]
49
- end
50
- end
51
- mime = 'text/html'
52
- body = template.render(self,hash)
53
- end
54
- [200,{'Content-Type' => mime },body]
55
- end
42
+ # Read a listing and render to HTML
43
+ # @param [Array] actions the allowed actions for this URI
44
+ # @param [Hash] context the context for this request
45
+ # @return [Response] a Rack Response triplet, or status code
46
+ def self.do_read_all(actions, context)
47
+ resource = context.uri[2]
48
+ body = ''
49
+ mime = ''
50
+ if context.app['use_forward']
51
+ response = context.forward(:readAll)
52
+ return response if response[0] != 200
53
+ mime = response[1][:content_type]
54
+ body = response[2]
55
+ end
56
+ template = context.app['resources'][context.resource]['multiple']
57
+ if template
58
+ hash = { actions: actions,
59
+ resource: resource,
60
+ mime: mime,
61
+ response: body }
62
+ if ccontext.app['resources'][context.resource]['extras']
63
+ context.app['resources'][context.resource]['extras'].each do |k, v|
64
+ hash[k] = RL.request(:get,
65
+ "http://#{context.app['remote']}/#{v}",
66
+ { remote_user: context.user }
67
+ )[2]
68
+ end
69
+ end
70
+ mime = 'text/html'
71
+ body = template.render(self, hash)
72
+ end
73
+ [200, { 'Content-Type': mime }, body]
74
+ end
56
75
 
57
- # Read a Partial and render it to HTML
58
- # @param [Array] actions the allowed actions for this URI
59
- # @param [Hash] context the context for this request
60
- # @return [Response] a Rack Response triplet, or status code
61
- def self.do_read(actions, context)
62
- app = context.uri[1]
63
- resource = context.uri[2]
64
- id = context.uri[3...context.uri.length].join('/')
65
- body = ''
66
- mime = ''
67
- if context.resource[:forward]
68
- response = forward(:read, context)
69
- return response if response[0] != 200
70
- mime = response[1][:content_type]
71
- body = response[2]
72
- end
73
- template = context.resource[:single]
74
- if template
75
- hash = { actions: actions, app: app, resource: resource, id: id, mime: mime, response: body }
76
- if context.resource[:sources]
77
- context.resource[:sources].each do |k, v|
78
- hash[k] = RL.request(:get, "http://#{context.app[:remote_host]}/#{v}")[2]
79
- end
80
- end
81
- mime = 'text/html'
82
- body = template.render(self,hash)
83
- end
84
- [200,{'Content-Type' => mime },body]
85
- end
76
+ # Read a Partial and render it to HTML
77
+ # @param [Array] actions the allowed actions for this URI
78
+ # @param [Hash] context the context for this request
79
+ # @return [Response] a Rack Response triplet, or status code
80
+ def self.do_read(actions, context)
81
+ body = ''
82
+ mime = ''
83
+ if context.app['resources'][context.resource]['use_forward']
84
+ response = context.forward(:read)
85
+ return response if response[0] != 200
86
+ mime = response[1][:content_type]
87
+ body = response[2]
88
+ end
89
+ template = context.app['resources'][context.resource]['single']
90
+ if template
91
+ hash = { actions: actions,
92
+ app: context.app,
93
+ resource: context.resource,
94
+ id: context.id,
95
+ mime: mime,
96
+ response: body }
97
+ if context.app['resources'][context.resource]['extras']
98
+ context.app['resources'][context.resource]['extras'].each do |k, v|
99
+ hash[k] = RL.request(:get, "http://#{context.app[:remote_host]}/#{v}")[2]
100
+ end
101
+ end
102
+ mime = 'text/html'
103
+ body = template.render(self, hash)
104
+ end
105
+ [200, { 'Content-Type': mime }, body]
106
+ end
86
107
 
87
- # Proxy method used when routing
88
- # @param [Array] actions the allowed actions for this URI
89
- # @param [Hash] context the context for this request
90
- # @return [Response] a Rack Response triplet, or status code
91
- def self.invoke(actions, context)
92
- case context.action
93
- when :create,:update,:delete
94
- forward(context.action, context)
95
- when :read
96
- if context.uri[3]
97
- do_read(actions, context)
98
- else
99
- do_read_all(actions, context)
100
- end
101
- else
102
- 403
103
- end
104
- end
105
- end
108
+ # Proxy method used when routing
109
+ # @param [Array] actions the allowed actions for this URI
110
+ # @param [Hash] context the context for this request
111
+ # @return [Response] a Rack Response triplet, or status code
112
+ def self.invoke(actions, context)
113
+ case context.action
114
+ when :create, :update, :delete
115
+ context.forward(context.action)
116
+ when :read
117
+ if context.id
118
+ do_read(actions, context)
119
+ else
120
+ do_read_all(actions, context)
121
+ end
122
+ else
123
+ 403
124
+ end
125
+ end
126
+ end
106
127
  end
@@ -1,48 +1,62 @@
1
- require_relative '../render'
1
+ ##
2
+ # Copyright 2017 Bryan T. Meyers
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ ##
16
+
17
+ require 'tilt'
2
18
 
3
19
  module Render
4
- # Style uses Tilt to render and serve stylesheets
5
- # @author Bryan T. Meyers
6
- module Style
7
- extend Render
20
+ # Style uses Tilt to render and serve stylesheets
21
+ # @author Bryan T. Meyers
22
+ module Style
8
23
 
9
- # DSL method to create a style
10
- # @param [String] resource the sub-URI for this style
11
- # @param [Hash] path the file location of the stylesheet
12
- # @return [void]
13
- def self.style(resource, path)
14
- unless $current_app[:styles]
15
- $current_app[:styles] = {}
16
- end
17
- $current_app[:styles][resource] = path.nil? ? nil : Tilt.new(path, 1, { ugly: true }).render
18
- end
24
+ # Configure styles
25
+ # @param [Hash] conf the raw configuration
26
+ # @return [Hash] post-processed configuration
27
+ def self.configure(conf)
28
+ conf['styles'].each do |k, v|
29
+ conf['styles'][k] = Tilt.new(v, 1, {ugly: true}).render
30
+ end
31
+ conf
32
+ end
19
33
 
20
- # Render a stylesheet to CSS
21
- # @param [Hash] context the context for this request
22
- # @return [Response] a Rack Response triplet, or status code
23
- def self.do_read_all(context)
24
- resource = context.uri[2]
25
- template = context.app[:styles][resource]
26
- headers = {'Cache-Control' => 'public,max-age=3600'}
27
- if template
28
- headers['Content-Type'] = 'text/css'
29
- [200, headers, [template]]
30
- else
31
- 404
32
- end
33
- end
34
+ # Render a stylesheet to CSS
35
+ # @param [Hash] context the context for this request
36
+ # @return [Response] a Rack Response triplet, or status code
37
+ def self.do_read_all(context)
38
+ resource = context.uri[2]
39
+ template = context.app['styles'][resource]
40
+ headers = {'Cache-Control': 'public,max-age=3600'}
41
+ if template
42
+ headers['Content-Type'] = 'text/css'
43
+ [200, headers, [template]]
44
+ else
45
+ 404
46
+ end
47
+ end
34
48
 
35
- # Proxy method used when routing
36
- # @param [Array] actions the allowed actions for this URI
37
- # @param [Hash] context the context for this request
38
- # @return [Response] a Rack Response triplet, or status code
39
- def self.invoke(actions, context)
40
- case context.action
41
- when :read
42
- do_read_all(context)
43
- else
44
- 403
45
- end
46
- end
47
- end
49
+ # Proxy method used when routing
50
+ # @param [Array] actions the allowed actions for this URI
51
+ # @param [Hash] context the context for this request
52
+ # @return [Response] a Rack Response triplet, or status code
53
+ def self.invoke(actions, context)
54
+ case context.action
55
+ when :read
56
+ do_read_all(context)
57
+ else
58
+ 403
59
+ end
60
+ end
61
+ end
48
62
  end
data/lib/app/repo.rb CHANGED
@@ -1,148 +1,154 @@
1
- require 'awesome_print'
1
+ ##
2
+ # Copyright 2017 Bryan T. Meyers
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ ##
16
+
2
17
  require 'base64'
3
- require_relative '../wire'
18
+ require 'tilt'
4
19
  require_relative 'repo/svn'
5
20
 
6
21
  # Repo is a Wire::App for accessing versioned content
7
22
  # @author Bryan T. Meyers
8
23
  module Repo
9
24
 
10
- # Select the location of the repositories
11
- # @param [Symbol] path location of the repositories
12
- # @return [void]
13
- def repos(path)
14
- $current_app[:repos_path] = path
15
- end
16
-
17
- # Select the render template for file listings
18
- # @param [Symbol] path location of the Tilt compatible template
19
- # @return [void]
20
- def listing(path)
21
- $current_app[:template] = Tilt.new(path, 1, { ugly: true })
22
- end
23
-
24
- # Select the sub-directory for web-serveable content
25
- # @param [Symbol] path the sub-directory path
26
- # @return [void]
27
- def web_folder(path)
28
- $current_app[:web] = path
29
- end
25
+ # Configure repo with listing template
26
+ # @param [Hash] conf the raw configuration
27
+ # @return [Hash] post-processed configuration
28
+ def self.configure(conf)
29
+ conf['listing'] = Tilt.new(conf['listing'], 1, { ugly: true })
30
+ conf
31
+ end
30
32
 
31
- # Create a new Repo
32
- # @param [Hash] context the context for this request
33
- # @return [Response] status code
34
- def do_create(context)
35
- path = context.app[:repos_path]
36
- resource = context.uri[2]
37
- if path
38
- if Dir.exist?("#{path}/#{resource}")
39
- 401
40
- else
41
- do_create_file(path, resource)
42
- end
43
- else
44
- 400
45
- end
46
- end
33
+ # Create a new Repo
34
+ # @param [Hash] context the context for this request
35
+ # @return [Response] status code
36
+ def do_create(context)
37
+ path = context.config['repos']
38
+ resource = context.resource
39
+ if path
40
+ if Dir.exist?("#{path}/#{resource}")
41
+ 401
42
+ else
43
+ do_create_file(path, resource)
44
+ end
45
+ else
46
+ 400
47
+ end
48
+ end
47
49
 
48
- # Get the a root directory listing
49
- # @param [Hash] context the context for this request
50
- # @return [Response] the listing, or status code
51
- def do_read_all(context)
52
- resource = context.uri[2]
53
- referrer = context.referer
54
- repos = context.app[:repos_path]
55
- web = context.app[:web]
56
- mime = 'text/html'
57
- list = do_read_listing(web, repos, resource)
58
- if list == 404
59
- return 404
60
- end
61
- template = context.app[:template]
62
- list = template.render(self, list: list, resource: resource, id: '', referrer: referrer)
63
- headers = {}
64
- headers['Content-Type'] = mime
65
- headers['Cache-Control'] = 'public'
66
- headers['Expires'] = "#{(Time.now + 1000).utc}"
67
- [200, headers, [list]]
68
- end
50
+ # Get the a root directory listing
51
+ # @param [Hash] context the context for this request
52
+ # @return [Response] the listing, or status code
53
+ def do_read_all(context)
54
+ mime = 'text/html'
55
+ list = do_read_listing(context.config['web'],
56
+ context.config['repos'],
57
+ context.resource)
58
+ if list == 404
59
+ return 404
60
+ end
61
+ template = context.config['listing']
62
+ list = template.render(self,
63
+ list: list,
64
+ resource: context.resource,
65
+ id: '',
66
+ referrer: context.referer)
67
+ headers = { 'Content-Type': mime,
68
+ 'Cache-Control': 'public',
69
+ 'Expires': "#{(Time.now + 1000).utc}" }
70
+ [200, headers, [list]]
71
+ end
69
72
 
70
- # Get the a single file or directory listing
71
- # @param [Hash] context the context for this request
72
- # @return [Response] the file, listing, or status code
73
- def do_read(context)
74
- path = context.uri[2]
75
- referrer = context.referer
76
- repos = context.app[:repos_path]
77
- web = context.app[:web]
78
- rev = context.query[:rev]
79
- id = context.uri[3...context.uri.length].join('/')
80
- info = do_read_info(rev, web, repos, path, id)
81
- if info == 404
82
- return 404
83
- end
84
- type = info[:@kind]
85
- if type.eql? 'dir'
86
- mime = 'text/html'
87
- list = do_read_listing(web, repos, path, id)
88
- template = context.app[:template]
89
- body = template.render(self, list: list, resource: path, id: id, referrer: referrer)
90
- else
91
- body = do_read_file(rev, web, repos, path, id)
92
- if body == 500
93
- return body
94
- end
95
- mime = do_read_mime(rev, web, repos, path, id)
96
- end
97
- headers = {}
98
- headers['Content-Type'] = mime
99
- headers['Cache-Control'] = 'public'
100
- headers['Expires'] = "#{(Time.now + 1000).utc}"
101
- [200, headers, [body]]
102
- end
73
+ # Get the a single file or directory listing
74
+ # @param [Hash] context the context for this request
75
+ # @return [Response] the file, listing, or status code
76
+ def do_read(context)
77
+ path = context.resource
78
+ referrer = context.referer
79
+ repos = context.config['repos']
80
+ web = context.config['web']
81
+ rev = context.query[:rev]
82
+ id = context.id
83
+ info = do_read_info(rev, web, repos, path, id)
84
+ if info == 404
85
+ return 404
86
+ end
87
+ type = info[:@kind]
88
+ if type.eql? 'dir'
89
+ mime = 'text/html'
90
+ list = do_read_listing(web, repos, path, id)
91
+ template = context.config['listing']
92
+ body = template.render(self,
93
+ list: list,
94
+ resource: path,
95
+ id: id,
96
+ referrer: referrer)
97
+ else
98
+ body = do_read_file(rev, web, repos, path, id)
99
+ if body == 500
100
+ return body
101
+ end
102
+ mime = do_read_mime(rev, web, repos, path, id)
103
+ end
104
+ headers = { 'Content-Type': mime,
105
+ 'Cache-Control': 'public',
106
+ 'Expires': "#{(Time.now + 1000).utc}" }
107
+ [200, headers, [body]]
108
+ end
103
109
 
104
- # Update the a single file
105
- # @param [Hash] context the context for this request
106
- # @return [Response] status code
107
- def do_update(context)
108
- path = context.uri[2]
109
- repos = context.app[:repos_path]
110
- web = context.app[:web]
111
- content = context.json
112
- id = context.uri[3...context.uri.length].join('/')
113
- if content[:file]
114
- file = content[:file][:content].match(/base64,(.*)/)[1]
115
- file = Base64.decode64(file)
116
- if context.query[:type]
117
- mime = context.query[:type]
118
- else
119
- mime = content[:file][:mime]
120
- end
121
- do_update_file(web, repos, path, id, file, content[:message], mime, context.user)
122
- else
123
- do_update_file(web, repos, path, id, URI.unescape(content[:updated]), content[:message], context.query[:type], context.user)
124
- end
125
- end
110
+ # Update the a single file
111
+ # @param [Hash] context the context for this request
112
+ # @return [Response] status code
113
+ def do_update(context)
114
+ path = context.resource
115
+ repos = context.config['repos']
116
+ web = context.config['web']
117
+ content = context.json
118
+ id = context.id
119
+ if content[:file]
120
+ file = content[:file][:content].match(/base64,(.*)/)[1]
121
+ file = Base64.decode64(file)
122
+ if context.query[:type]
123
+ mime = context.query[:type]
124
+ else
125
+ mime = content[:file][:mime]
126
+ end
127
+ do_update_file(web, repos, path, id, file, content[:message], mime, context.user)
128
+ else
129
+ do_update_file(web, repos, path, id, URI.unescape(content[:updated]), content[:message], context.query[:type], context.user)
130
+ end
131
+ end
126
132
 
127
- # Proxy method used when routing
128
- # @param [Array] actions the allowed actions for this URI
129
- # @param [Hash] context the context for this request
130
- # @return [Response] a Rack Response triplet, or status code
131
- def invoke(actions, context)
132
- return 404 unless context.uri[2]
133
- case context.action
134
- when :create
135
- do_create(context)
136
- when :read
137
- if context.uri[3]
138
- do_read(context)
139
- else
140
- do_read_all(context)
141
- end
142
- when :update
143
- do_update(context)
144
- else
145
- 403
146
- end
147
- end
133
+ # Proxy method used when routing
134
+ # @param [Array] actions the allowed actions for this URI
135
+ # @param [Hash] context the context for this request
136
+ # @return [Response] a Rack Response triplet, or status code
137
+ def invoke(actions, context)
138
+ return 404 unless context.resource
139
+ case context.action
140
+ when :create
141
+ do_create(context)
142
+ when :read
143
+ if context.id
144
+ do_read(context)
145
+ else
146
+ do_read_all(context)
147
+ end
148
+ when :update
149
+ do_update(context)
150
+ else
151
+ 403
152
+ end
153
+ end
148
154
  end