lb-project 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9f1933319af3ce3333456607636bcc90c5bdef86ff4f566564bcddb87f04d5bf
4
- data.tar.gz: 1324cbf4adfe901a4daca6717a018844f7545c6f17b598ffdfee528e3c3667fd
3
+ metadata.gz: c27f7acd45b2567e52fb686194cea98e7ca4d656900616c8bab1cb7f0ab481d3
4
+ data.tar.gz: dc1683fcba720d1d548e039ad619287be10f486a3c2afea1e33a50170257bc58
5
5
  SHA512:
6
- metadata.gz: 7ffbba086fb2ab6171d9cf8deb20f443cb78aa6b60e544bec681c7c1719a950b387a8db820e62e972269d498de4bf03d82c1f966174a747069642e7e3da71417
7
- data.tar.gz: f97fd83b3efc966484ca78d599140069081863fe8335528deb71a7aa657af65c94ceabaf5fc2f4843cde065ed625844663fbce1e8b680ed449566772e0a5d1b6
6
+ metadata.gz: 03340f24aef69ad08d89b22fd9a6b8f6670530d8c16c48add9ad72e75456529ee4bffac76c62819b424288fb2559d1b9001165f70b78c86c0db4e99083a5f47f
7
+ data.tar.gz: 14fbadd80b4c7fea37d9c8452c2910397f4e8643ddf6154614b58141e465666d7e639df9d1434abd9e4378f7505f4e9f1c23487122126305bbfe44ff3500f386
@@ -28,6 +28,7 @@ Gem::Specification.new do |gem|
28
28
  gem.add_dependency 'dry-struct', '~> 0.5'
29
29
  gem.add_dependency 'dry-types', '~> 0.13'
30
30
  gem.add_dependency 'dry-view', '~> 0.5'
31
+ gem.add_dependency 'rack_csrf', '~> 2.6'
31
32
  gem.add_dependency 'roda', '~> 3.7'
32
33
  gem.add_dependency 'roda-i18n', '~> 0.4'
33
34
 
@@ -37,5 +38,6 @@ Gem::Specification.new do |gem|
37
38
  gem.add_development_dependency 'guard-rspec', '~> 4.7', '>= 4.7.3'
38
39
  gem.add_development_dependency 'guard-rubocop', '~> 1.3'
39
40
  gem.add_development_dependency 'rubocop', '~> 0.52'
41
+ gem.add_development_dependency 'shotgun', '~> 0.9.2'
40
42
  gem.add_development_dependency 'slim', '~> 3.0'
41
43
  end
@@ -6,6 +6,7 @@ require 'yaml'
6
6
  # Web
7
7
  require 'roda'
8
8
  require 'roda/plugins/public'
9
+ require 'rack/csrf'
9
10
 
10
11
  # r18n
11
12
  require 'r18n-core'
@@ -22,6 +23,9 @@ require 'lb/project/version'
22
23
  # Custom Types
23
24
  require 'lb/project/types'
24
25
 
26
+ # Site
27
+ require 'lb/project/site'
28
+
25
29
  # Config
26
30
  require 'lb/project/config'
27
31
  require 'lb/project/settings'
@@ -51,6 +55,10 @@ module LB
51
55
  self
52
56
  end
53
57
 
58
+ def self.settings
59
+ @settings
60
+ end
61
+
54
62
  # Get root path
55
63
  #
56
64
  # @return [dir_name]
@@ -132,9 +140,6 @@ end
132
140
  # API
133
141
  require 'lb/project/api'
134
142
 
135
- # Site
136
- require 'lb/project/site'
137
-
138
143
  # Routing
139
144
  require 'lb/project/route'
140
145
  require 'lb/project/app'
@@ -149,3 +154,6 @@ require 'lb/project/page'
149
154
 
150
155
  # Render
151
156
  require 'lb/project/render'
157
+
158
+ # Roda Plugin
159
+ require 'lb/project/roda'
@@ -34,7 +34,7 @@ module LB
34
34
  #
35
35
  # @api private
36
36
  #
37
- def self.template_path
37
+ def template_path
38
38
  LB::Project.template_path
39
39
  end
40
40
 
@@ -44,7 +44,7 @@ module LB
44
44
  #
45
45
  # @api private
46
46
  #
47
- def self.public_path
47
+ def public_path
48
48
  LB::Project.public_path
49
49
  end
50
50
 
@@ -54,15 +54,15 @@ module LB
54
54
  #
55
55
  # @api private
56
56
  #
57
- def self.config
57
+ def config
58
58
  LB::Project.config
59
59
  end
60
60
 
61
- def self.t(*params)
61
+ def t(*params)
62
62
  LB::Project.t(*params)
63
63
  end
64
64
 
65
- def self.logger
65
+ def logger
66
66
  LB::Project.logger
67
67
  end
68
68
  end
@@ -44,7 +44,7 @@ module LB
44
44
 
45
45
  def self.define_registry_as(registry_name, rmethod)
46
46
  define_method("register_#{registry_name}_as") do |key|
47
- send(rmethod).register(key.to_s, self)
47
+ send(rmethod).register(key.to_sym, self)
48
48
  instance_variable_set("@#{registry_name}_key", key.to_s)
49
49
  end
50
50
  end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Roda
4
+ # LB::Project Roda Plugins
5
+ module RodaPlugins
6
+ # LB::Project plugin
7
+ module LBProject
8
+ # Plugin instance methods
9
+ module InstanceMethods
10
+ def page(name, overrides = {})
11
+ page = LB::Project::Page.page_registry[name].new
12
+ page.call(current_context(page.with(overrides)))
13
+ end
14
+
15
+ private
16
+
17
+ def current_site
18
+ site.with_flash(flash)
19
+ end
20
+
21
+ def current_context(page)
22
+ {
23
+ context: ::Struct.new(:site, :page).new(current_site, page),
24
+ locals: {}
25
+ }
26
+ end
27
+
28
+ def site
29
+ LB::Project.settings.site.new.with(csrf_config.merge(session_config))
30
+ end
31
+
32
+ def csrf_config
33
+ {
34
+ csrf_token: -> { Rack::Csrf.token(request.env) },
35
+ csrf_metatag: -> { Rack::Csrf.metatag(request.env) },
36
+ csrf_tag: -> { Rack::Csrf.tag(request.env) }
37
+ }
38
+ end
39
+
40
+ def session_config
41
+ {
42
+ session: -> { request.session }
43
+ }
44
+ end
45
+ end
46
+
47
+ # Plugin request methods
48
+ module RequestMethods
49
+ def parent
50
+ File.dirname script_name
51
+ end
52
+
53
+ def redirect_parent
54
+ redirect parent
55
+ end
56
+ end
57
+ end
58
+
59
+ register_plugin :lb_project, LBProject
60
+ end
61
+ end
@@ -8,7 +8,10 @@ module LB
8
8
  translations = [File.join(LB::Project.root, 'locales')]
9
9
  route.plugin :i18n, translations: translations
10
10
  route.opts[:root] = LB::Project.root
11
- route.plugin :public, root: 'docs'
11
+ route.plugin :public, root: LB::Project.config.public_path
12
+ route.plugin :flash
13
+ route.plugin :lb_project
14
+ route.plugin :json
12
15
  end
13
16
  end
14
17
  end
@@ -6,6 +6,7 @@ module LB
6
6
  class Settings < Dry::Struct
7
7
  attribute :root, Types::Strict::String
8
8
  attribute :config, LB::Project::Config
9
+ attribute :site, Types::Strict::Class.default(LB::Project::Site)
9
10
  end
10
11
  end
11
12
  end
@@ -6,6 +6,11 @@ module LB
6
6
  class Site
7
7
  include R18n::Helpers
8
8
 
9
+ DEFAULT_MESSAGE_TYPES = {
10
+ notice: { level: :success, timeout: 5 },
11
+ alert: { level: :error, timeout: 0 }
12
+ }.freeze
13
+
9
14
  def initialize(context = {})
10
15
  @context = context
11
16
  end
@@ -18,6 +23,12 @@ module LB
18
23
  options.fetch(*args)
19
24
  end
20
25
 
26
+ def with(opts)
27
+ options.merge!(opts)
28
+
29
+ self
30
+ end
31
+
21
32
  def path_for(bundle, type)
22
33
  path = 'webpack-assets.json'
23
34
  file = File.read(path)
@@ -36,6 +47,75 @@ module LB
36
47
  def image_path(path)
37
48
  "#{LB::Project.config.image_base_path}/#{path}"
38
49
  end
50
+
51
+ def escaped_json(data)
52
+ "JSON#{Rack::Utils.escape_html(JSON.generate(escape_all(data)))}"
53
+ end
54
+
55
+ def escape_all(data)
56
+ case data
57
+ when Array
58
+ escape_array(data)
59
+ when Hash
60
+ escape_hash(data)
61
+ when String
62
+ Rack::Utils.escape_html(data)
63
+ else
64
+ data
65
+ end
66
+ end
67
+
68
+ def escape_array(data)
69
+ data.map(&method(:escape_all))
70
+ end
71
+
72
+ def escape_hash(data)
73
+ data.map do |key, value|
74
+ [key, escape_all(value)]
75
+ end.to_h
76
+ end
77
+
78
+ def flash?
79
+ %i[notice alert].any? { |type| flash[type] }
80
+ end
81
+
82
+ def with_flash(flash)
83
+ with(flash: flash)
84
+ end
85
+
86
+ def flash
87
+ self[:flash, {}]
88
+ end
89
+
90
+ def csrf_token
91
+ self[:csrf_token].call
92
+ end
93
+
94
+ def csrf_metatag
95
+ self[:csrf_metatag].call
96
+ end
97
+
98
+ def csrf_tag
99
+ self[:csrf_tag].call
100
+ end
101
+
102
+ def session
103
+ self[:session].call
104
+ end
105
+
106
+ def logged_in?
107
+ session.key?(:user_id)
108
+ end
109
+
110
+ def message_types
111
+ DEFAULT_MESSAGE_TYPES
112
+ end
113
+
114
+ def messages
115
+ flash.map do |type, message|
116
+ { message: message }.merge(MESSAGE_TYPES[type])
117
+ end
118
+ end
39
119
  end
40
120
  end
41
121
  end
@@ -3,6 +3,6 @@
3
3
  module LB
4
4
  module Project
5
5
  # Version
6
- VERSION = '0.1.1'
6
+ VERSION = '0.2.0'
7
7
  end
8
8
  end
@@ -11,8 +11,8 @@ module LB
11
11
  view.setting :layout, 'main'
12
12
  end
13
13
 
14
- def view_locals(options)
15
- options
14
+ def with(_options)
15
+ self
16
16
  end
17
17
  end
18
18
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lb-project
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Firas Zaidan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-08 00:00:00.000000000 Z
11
+ date: 2018-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-initializer
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack_csrf
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.6'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.6'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: roda
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -184,6 +198,20 @@ dependencies:
184
198
  - - "~>"
185
199
  - !ruby/object:Gem::Version
186
200
  version: '0.52'
201
+ - !ruby/object:Gem::Dependency
202
+ name: shotgun
203
+ requirement: !ruby/object:Gem::Requirement
204
+ requirements:
205
+ - - "~>"
206
+ - !ruby/object:Gem::Version
207
+ version: 0.9.2
208
+ type: :development
209
+ prerelease: false
210
+ version_requirements: !ruby/object:Gem::Requirement
211
+ requirements:
212
+ - - "~>"
213
+ - !ruby/object:Gem::Version
214
+ version: 0.9.2
187
215
  - !ruby/object:Gem::Dependency
188
216
  name: slim
189
217
  requirement: !ruby/object:Gem::Requirement
@@ -234,6 +262,7 @@ files:
234
262
  - lib/lb/project/registry.rb
235
263
  - lib/lb/project/registry/registration.rb
236
264
  - lib/lb/project/render.rb
265
+ - lib/lb/project/roda.rb
237
266
  - lib/lb/project/route.rb
238
267
  - lib/lb/project/settings.rb
239
268
  - lib/lb/project/site.rb