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,45 +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
+ ##
2
16
 
3
17
  module Render
4
- # Document renders a file to an HTML representation
5
- # @author Bryan T. Meyers
6
- module Document
7
- extend Render
18
+ # Document renders a file to an HTML representation
19
+ # @author Bryan T. Meyers
20
+ module Document
8
21
 
9
- # Renders a document or listing to HTML
10
- # @param [Array] actions the actions allowed for this URI
11
- # @param [Wire::Context] context the context for this request
12
- # @param [Symbol] specific the type of read to perform
13
- # @return [Response] a Rack Response triplet, or status code
14
- def self.do_read(actions, context, specific)
15
- response = forward(specific, context)
16
- mime = response[1]['content-type']
17
- renderer = $renderers[mime]
18
- if renderer
19
- template = $templates[renderer]
20
- template.render(self, { actions: actions, context: context, mime: mime, response: response[2] })
21
- else
22
- response
23
- end
24
- end
22
+ # Renders a document or listing to HTML
23
+ # @param [Array] actions the actions allowed for this URI
24
+ # @param [Wire::Context] context the context for this request
25
+ # @param [Symbol] specific the type of read to perform
26
+ # @return [Response] a Rack Response triplet, or status code
27
+ def self.do_read(actions, context, specific)
28
+ response = context.forward(specific)
29
+ mime = response[1]['content-type']
30
+ #TODO: Fix lookup
31
+ renderer = $wire_renderers[mime]
32
+ if renderer
33
+ template = renderer['partial']
34
+ template.render(self, { actions: actions,
35
+ context: context,
36
+ mime: mime,
37
+ response: response[2] })
38
+ else
39
+ response
40
+ end
41
+ end
25
42
 
26
- # Proxy method used when routing
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.invoke(actions, context)
31
- case context.action
32
- when :create,:update,:delete
33
- forward(context.action, context)
34
- when :read
35
- if context.uri[3]
36
- do_read(actions, context, :read)
37
- else
38
- do_read(actions, context, :readAll)
39
- end
40
- else
41
- 405
42
- end
43
- end
44
- end
43
+ # Proxy method used when routing
44
+ # @param [Array] actions the allowed actions for this URI
45
+ # @param [Hash] context the context for this request
46
+ # @return [Response] a Rack Response triplet, or status code
47
+ def self.invoke(actions, context)
48
+ case context.action
49
+ when :create, :update, :delete
50
+ context.forward(context.action)
51
+ when :read
52
+ if context.id
53
+ do_read(actions, context, :read)
54
+ else
55
+ do_read(actions, context, :readAll)
56
+ end
57
+ else
58
+ 405
59
+ end
60
+ end
61
+ end
45
62
  end
@@ -1,50 +1,65 @@
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
17
  module Render
4
18
 
5
- # Editor allows a document to be displayed in an editing form
6
- # @author Bryan T. Meyers
7
- module Editor
8
- extend Render
19
+ # Editor allows a document to be displayed in an editing form
20
+ # @author Bryan T. Meyers
21
+ module Editor
9
22
 
10
- # Open an editor for a document
11
- # @param [Array] actions the allowed actions for this URI
12
- # @param [Hash] context the context for this request
13
- # @return [Response] the Editor with containing document, or status code
14
- def self.do_read(actions, context)
15
- resource = context.uri[2]
16
- query = context.query
17
- id = context.uri[3...context.uri.length].join('/')
18
- response = forward(:read, context)
19
- body = (response[0] == 200 ) ? response[2] : ''
20
- if query[:type]
21
- mime = query[:type]
22
- elsif response[1]['content-type']
23
- mime = response[1]['content-type']
24
- else
25
- return [404, {}, 'EDITOR: Document type not set for new document']
26
- end
27
- template = $editors[mime]
28
- if template
29
- template.render(self, { actions: actions, resource: resource, id: id, mime: mime, response: body })
30
- else
31
- body
32
- end
33
- end
23
+ # Open an editor for a document
24
+ # @param [Array] actions the allowed actions for this URI
25
+ # @param [Hash] context the context for this request
26
+ # @return [Response] the Editor with containing document, or status code
27
+ def self.do_read(actions, context)
28
+ response = context.forward(:read)
29
+ body = (response[0] == 200) ? response[2] : ''
30
+ if context.query[:type]
31
+ mime = context.query[:type]
32
+ elsif response[1]['content-type']
33
+ mime = response[1]['content-type']
34
+ else
35
+ return [404, {}, 'EDITOR: Document type not set for new document']
36
+ end
37
+ #TODO: Fix lookup
38
+ template = $wire_editors[mime]
39
+ if template
40
+ template.render(self, { actions: actions,
41
+ resource: context.resource,
42
+ id: context.id,
43
+ mime: mime,
44
+ response: body })
45
+ else
46
+ body
47
+ end
48
+ end
34
49
 
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 :create,:update
42
- forward(context.action, context)
43
- when :read
44
- do_read(actions, context)
45
- else
46
- 405
47
- end
48
- end
49
- end
50
+ # Proxy method used when routing
51
+ # @param [Array] actions the allowed actions for this URI
52
+ # @param [Hash] context the context for this request
53
+ # @return [Response] a Rack Response triplet, or status code
54
+ def self.invoke(actions, context)
55
+ case context.action
56
+ when :create, :update
57
+ context.forward(context.action)
58
+ when :read
59
+ do_read(actions, context)
60
+ else
61
+ 405
62
+ end
63
+ end
64
+ end
50
65
  end
@@ -1,41 +1,61 @@
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
- module Error
5
- extend Render
20
+ module Error
6
21
 
7
- def self.error(match, path)
8
- unless $current_app[:errors]
9
- $current_app[:errors] = {}
10
- end
11
- $current_app[:errors][match] = Tilt.new(path, 1, { ugly: true })
12
- end
22
+ # Configure error templates
23
+ # @param [Hash] conf the raw configuration
24
+ # @return [Hash] post-processed configuration
25
+ def self.configure(conf)
26
+ conf['errors'].each do |k, v|
27
+ conf['errors'][k] = Tilt.new(v, 1, { ugly: true })
28
+ end
29
+ conf
30
+ end
13
31
 
14
- def self.error_check(actions, context, result)
15
- errors = context.app[:errors]
16
- if errors
17
- template = errors[result[0]]
18
- if template
19
- result[2] = template.render(self, {actions: actions, context: context, result: result})
20
- end
21
- end
22
- result
23
- end
32
+ def self.error_check(actions, context, result)
33
+ errors = context.app['errors']
34
+ if errors
35
+ template = errors[result[0]]
36
+ if template
37
+ result[2] = template.render(self, { actions: actions,
38
+ context: context,
39
+ result: result })
40
+ end
41
+ end
42
+ result
43
+ end
24
44
 
25
- def self.invoke(actions, context)
26
- case context.action
27
- when :create,:update
28
- result = forward(context.action, context)
29
- when :read
30
- if context.uri[3]
31
- result = forward(:read, context)
32
- else
33
- result = forward(:readAll, context)
34
- end
35
- else
36
- result = 405
37
- end
38
- error_check(actions, context, result)
39
- end
40
- end
45
+ def self.invoke(actions, context)
46
+ case context.action
47
+ when :create, :update
48
+ result = context.forward(context.action)
49
+ when :read
50
+ if context.id
51
+ result = context.forward(:read)
52
+ else
53
+ result = context.forward(:readAll)
54
+ end
55
+ else
56
+ result = 405
57
+ end
58
+ error_check(actions, context, result)
59
+ end
60
+ end
41
61
  end
@@ -1,65 +1,76 @@
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
17
  module Render
4
- # Instant allows for previews of edited documents
5
- # @author Bryan T. Meyers
6
- module Instant
7
- extend Render
18
+ # Instant allows for previews of edited documents
19
+ # @author Bryan T. Meyers
20
+ module Instant
8
21
 
9
- # Render a temporary document to HTML
10
- # @param [Array] actions the allowed actions for this URI
11
- # @param [Hash] context the context for this request
12
- # @return [Response] a Rack Response triplet, or status code
13
- def self.do_update(actions, context)
14
- body = context.body
15
- if body
16
- body = body.split('=')[1]
17
- if body
18
- body = URI.decode(body)
19
- end
20
- end
21
- resource = context.uri[2]
22
- id = context.uri[3]
23
- ## Default to not found
24
- message = ''
25
- status = 404
26
- if resource
27
- if body
28
- ## Assume unsupported mime type
29
- status = 415
30
- message = 'INSTANT: Unsupported MIME Type'
31
- renderer = $renderers["#{resource}/#{id}"]
32
- if renderer
33
- template = $templates[renderer]
34
- result = template.render(self,
35
- { actions: actions,
36
- context: context,
37
- mime: "#{resource}/#{id}",
38
- response: body,
39
- })
40
- template = context.app[:template]
41
- if template
42
- message = template[:path].render(self, { actions: actions, context: context, content: result })
43
- else
44
- message = result
45
- end
46
- status = 200
47
- end
48
- end
49
- end
50
- [status, {}, message]
51
- end
22
+ # Render a temporary document to HTML
23
+ # @param [Array] actions the allowed actions for this URI
24
+ # @param [Hash] context the context for this request
25
+ # @return [Response] a Rack Response triplet, or status code
26
+ def self.do_update(actions, context)
27
+ ## Default to not found
28
+ message = ''
29
+ status = 404
30
+ if context.resource
31
+ body = context.body
32
+ if body
33
+ #TODO: This looks off...
34
+ body = body.split('=')[1]
35
+ if body
36
+ body = URI.decode(body)
37
+ end
38
+ ## Assume unsupported mime type
39
+ status = 415
40
+ message = 'INSTANT: Unsupported MIME Type'
41
+ renderer = $wire_renderers["#{context.resource}/#{context.id}"]
42
+ if renderer
43
+ template = $wire_templates[renderer]
44
+ result = template.render(self, { actions: actions,
45
+ context: context,
46
+ mime: "#{context.resource}/#{context.id}",
47
+ response: body, })
48
+ #TODO: Fix template lookup
49
+ template = context.app[:template]
50
+ if template
51
+ message = template[:path].render(self, { actions: actions,
52
+ context: context,
53
+ content: result })
54
+ else
55
+ message = result
56
+ end
57
+ status = 200
58
+ end
59
+ end
60
+ end
61
+ [status, {}, message]
62
+ end
52
63
 
53
- # Proxy method used when routing
54
- # @param [Array] actions the allowed actions for this URI
55
- # @param [Hash] context the context for this request
56
- # @return [Response] a Rack Response triplet, or status code
57
- def self.invoke(actions, context)
58
- if context.action.eql? :update
59
- do_update(actions, context)
60
- else
61
- 405
62
- end
63
- end
64
- end
64
+ # Proxy method used when routing
65
+ # @param [Array] actions the allowed actions for this URI
66
+ # @param [Hash] context the context for this request
67
+ # @return [Response] a Rack Response triplet, or status code
68
+ def self.invoke(actions, context)
69
+ if context.action.eql? :update
70
+ do_update(actions, context)
71
+ else
72
+ 405
73
+ end
74
+ end
75
+ end
65
76
  end
@@ -1,85 +1,110 @@
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
17
  module Render
4
- # Page builds the a page that is presented directly to a user
5
- # @author Bryan T. Meyers
6
- module Page
7
- include Render
8
- extend self
18
+ # Page builds the a page that is presented directly to a user
19
+ # @author Bryan T. Meyers
20
+ module Page
9
21
 
10
- # Render a full template, handling the gathering of additional Sources
11
- # @param [Array] actions the allowed actions for this URI
12
- # @param [Hash] context the context for this request
13
- # @param [Tilt::Template] template a pre-loaded Tilt template to render
14
- # @param [String] content the content to render into the page
15
- # @return [Response] a Rack Response triplet, or status code
16
- def render_template(actions, context, template, content)
17
- if template[:path]
18
- hash = { actions: actions, context: context, content: content }
19
- template[:sources].each do |k, s|
20
- uri = "http://#{context.app[:remote_host]}/#{s[:uri]}"
21
- go_ahead = true
22
- case s[:key]
23
- when :user
24
- go_ahead = (context.user and !context.user.empty?)
25
- uri += "/#{context.user}"
26
- when :resource
27
- go_ahead = (context.uri[2] and !context.uri[2].empty?)
28
- uri += "/#{context.uri[2]}"
29
- end
30
- temp = []
31
- if go_ahead
32
- temp = RL.request(:get, uri, {remote_user: context.user})
33
- end
34
- hash[k] = (temp[0] == 200) ? temp[2] : nil
35
- end
36
- message = template[:path].render(self, hash)
37
- if template[:use_layout]
38
- message = render_template(actions, context, $apps[:global][:template], message)
39
- end
40
- else
41
- message = 'Invalid Template'
42
- end
43
- message
44
- end
22
+ # Render a full template, handling the gathering of additional Sources
23
+ # @param [Array] actions the allowed actions for this URI
24
+ # @param [Hash] context the context for this request
25
+ # @param [Tilt::Template] template a pre-loaded Tilt template to render
26
+ # @param [String] content the content to render into the page
27
+ # @return [Response] a Rack Response triplet, or status code
28
+ def render_template(actions, context, template, content)
29
+ if template['file']
30
+ hash = { actions: actions, context: context, content: content }
31
+ template['sources'].each do |k, s|
32
+ uri = "http://#{context.app[:remote_host]}"
33
+ go_ahead = true
34
+ if s.is_a? Hash
35
+ uri += "/#{hash['uri']}"
36
+ case s['key']
37
+ when 'user'
38
+ go_ahead = (context.user and !context.user.empty?)
39
+ uri += "/#{context.user}"
40
+ when 'resource'
41
+ go_ahead = (context.uri[2] and !context.uri[2].empty?)
42
+ uri += "/#{context.uri[2]}"
43
+ else
44
+ # do nothing
45
+ end
46
+ else
47
+ uri += s
48
+ end
49
+ temp = []
50
+ if go_ahead
51
+ temp = RL.request(:get, uri, { remote_user: context.user })
52
+ end
53
+ if temp[0] == 200
54
+ begin
55
+ hash[k] = JSON.parse_clean(temp[2])
56
+ rescue
57
+ hash[k] = temp[2]
58
+ end
59
+ end
60
+ end
61
+ message = template[:path].render(self, hash)
62
+ if template['use_layout']
63
+ message = render_template(actions, context, $wire_apps[:global][:template], message)
64
+ end
65
+ else
66
+ message = 'Invalid Template'
67
+ end
68
+ message
69
+ end
45
70
 
46
- # Render a page to its final form
47
- # @param [Array] actions the allowed actions for this URI
48
- # @param [Hash] context the context for this request
49
- # @param [Symbol] specific the kind of read to perform
50
- # @return [Response] a Rack Response triplet, or status code
51
- def do_read(actions, context, specific)
52
- resource = context.uri[2]
53
- if resource
54
- result = forward(specific, context)
55
- template = context.app[:template]
56
- if template
57
- result[1]['Content-Type'] = 'text/html'
58
- result[2] = render_template(actions, context, template, result[2])
59
- end
60
- else
61
- result = [401,{},'Resource not specified']
62
- end
63
- result
64
- end
71
+ # Render a page to its final form
72
+ # @param [Array] actions the allowed actions for this URI
73
+ # @param [Hash] context the context for this request
74
+ # @param [Symbol] specific the kind of read to perform
75
+ # @return [Response] a Rack Response triplet, or status code
76
+ def do_read(actions, context, specific)
77
+ if context.resource
78
+ result = context.forward(specific)
79
+ #TODO: fix lookup
80
+ template = context.app[:template]
81
+ if template
82
+ result[1]['Content-Type'] = 'text/html'
83
+ result[2] = render_template(actions, context, template, result[2])
84
+ end
85
+ else
86
+ result = [401, {}, 'Resource not specified']
87
+ end
88
+ result
89
+ end
65
90
 
66
- # Proxy method used when routing
67
- # @param [Array] actions the allowed actions for this URI
68
- # @param [Hash] context the context for this request
69
- # @return [Response] a Rack Response triplet, or status code
70
- def self.invoke(actions, context)
71
- case context.action
72
- when :create,:update,:delete
73
- forward(context.action, context)
74
- when :read
75
- if context.uri[3]
76
- do_read(actions, context, :read)
77
- else
78
- do_read(actions, context, :readAll)
79
- end
80
- else
81
- 405
82
- end
83
- end
84
- end
91
+ # Proxy method used when routing
92
+ # @param [Array] actions the allowed actions for this URI
93
+ # @param [Hash] context the context for this request
94
+ # @return [Response] a Rack Response triplet, or status code
95
+ def self.invoke(actions, context)
96
+ case context.action
97
+ when :create, :update, :delete
98
+ context.forward(context.action)
99
+ when :read
100
+ if context.uri[3]
101
+ do_read(actions, context, :read)
102
+ else
103
+ do_read(actions, context, :readAll)
104
+ end
105
+ else
106
+ 405
107
+ end
108
+ end
109
+ end
85
110
  end