flame 4.18.0 → 4.18.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ebb50e9412146b4ff8539c37a33078d360bd7414
4
- data.tar.gz: 80a08f804d28d61dd8bf6cfcd9681d923b23cb41
3
+ metadata.gz: 9fbe52ed5ac3bee05b2739396b58c83dce8f6969
4
+ data.tar.gz: b54f7a5c830967546d696a4fcccf93e22dcab9c5
5
5
  SHA512:
6
- metadata.gz: 9680f9cd7ddca87d9024211c0c98c20e611061b2a6934c16a364e083f57076a92dbe5e15d14ce97f090450658517bec0fb3b84a8a3800ccc9da5a7532efdbda9
7
- data.tar.gz: 50d5041a23247616a53dd3e605def07edef86705c89cda45dc8f988e55d7f439fde677fef0fca088dcd0265f582556a812581863bc9f7920556ae656f280ecbc
6
+ metadata.gz: 2c384ad693932e066041d145885cace94ebe75a3eaa1830853df0bb6ddacbcb423f9f69c36d5c2bcca2297079ab636529f9aa6294c1389378a43444b16dd72c1
7
+ data.tar.gz: 4747eaf2eb6651776abf63ccd809e174ff747006be9289e1ff6aa564e55e4f7aa475ffbe7dee6d1b3b21fa382dbe69d787c452c183fceacbab7a48052472d1c9
@@ -19,7 +19,7 @@ module Flame
19
19
  def_delegators(
20
20
  :@dispatcher,
21
21
  :config, :request, :params, :halt, :session, :response, :status, :body,
22
- :default_body, :cached_tilts
22
+ :default_body, :cached_tilts, :find_static
23
23
  )
24
24
 
25
25
  ## Initialize the controller for request execution
@@ -35,13 +35,14 @@ module Flame
35
35
  end
36
36
 
37
37
  ## Build a URI to the given controller and action, or path
38
- def url_to(*args)
38
+ def url_to(*args, **options)
39
39
  first_arg = args.first
40
40
  path =
41
41
  if first_arg.is_a?(String) || first_arg.is_a?(Flame::Path)
42
- first_arg
42
+ static_file = find_static(first_arg)
43
+ static_file.path(with_version: options[:version])
43
44
  else
44
- path_to(*args)
45
+ path_to(*args, **options)
45
46
  end
46
47
  "#{request.scheme}://#{request.host_with_port}#{path}"
47
48
  end
@@ -33,7 +33,7 @@ module Flame
33
33
  def run!
34
34
  catch :halt do
35
35
  try_static ||
36
- try_static(GEM_STATIC_FILES) ||
36
+ try_static(dir: GEM_STATIC_FILES) ||
37
37
  try_route ||
38
38
  halt(404)
39
39
  end
@@ -4,27 +4,58 @@ module Flame
4
4
  class Dispatcher
5
5
  ## Module for working with static files
6
6
  module Static
7
+ def find_static(filename = request.path_info, dir: config[:public_dir])
8
+ StaticFile.new(filename, dir)
9
+ end
10
+
7
11
  private
8
12
 
9
13
  ## Find static files and try return it
10
- def try_static(dir = config[:public_dir])
11
- file = File.join(dir, URI.decode(request.path_info))
12
- return nil unless File.exist?(file) && File.file?(file)
14
+ def try_static(*args)
15
+ file = find_static(*args)
16
+ return nil unless file.exist?
13
17
  return_static(file)
14
18
  end
15
19
 
16
- def static_cached?(file_time)
17
- since = request.env['HTTP_IF_MODIFIED_SINCE']
18
- since && Time.httpdate(since).to_i >= file_time.to_i
19
- end
20
-
21
20
  def return_static(file)
22
- file_time = File.mtime(file)
23
- halt 304 if static_cached?(file_time)
24
- response.content_type = File.extname(file)
21
+ halt 304 if file.newer? request.env['HTTP_IF_MODIFIED_SINCE']
22
+ response.content_type = file.extname
25
23
  response[Rack::CACHE_CONTROL] = 'no-cache'
26
- response['Last-Modified'] = file_time.httpdate
27
- body File.read(file)
24
+ response['Last-Modified'] = file.mtime.httpdate
25
+ body file.content
26
+ end
27
+
28
+ ## Class for static files with helpers methods
29
+ class StaticFile
30
+ def initialize(filename, dir)
31
+ @filename = filename.to_s
32
+ @path = File.join dir, URI.decode(@filename)
33
+ end
34
+
35
+ def exist?
36
+ File.exist?(@path) && File.file?(@path)
37
+ end
38
+
39
+ def mtime
40
+ File.mtime(@path)
41
+ end
42
+
43
+ def extname
44
+ File.extname(@path)
45
+ end
46
+
47
+ def newer?(http_since)
48
+ http_since && Time.httpdate(http_since).to_i >= mtime.to_i
49
+ end
50
+
51
+ def content
52
+ File.read(@path)
53
+ end
54
+
55
+ def path(with_version: false)
56
+ path = @filename
57
+ with_version ? "#{path}?v=#{mtime.to_i}" : path
58
+ end
28
59
  end
29
60
  end
30
61
  end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flame
4
+ module Errors
5
+ ## Error for Route initialization
6
+ class RouteArgumentsOrderError < StandardError
7
+ def initialize(path, wrong_ordered_arguments)
8
+ @path = path
9
+ @wrong_ordered_arguments = wrong_ordered_arguments
10
+ end
11
+
12
+ def message
13
+ "Path '#{@path}' should have" \
14
+ " '#{@wrong_ordered_arguments.first}' argument before" \
15
+ " '#{@wrong_ordered_arguments.last}'"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -3,7 +3,7 @@
3
3
  module Flame
4
4
  module Errors
5
5
  ## Error for Route initialization
6
- class RouteArgumentsError < StandardError
6
+ class RouteExtraArgumentsError < StandardError
7
7
  def initialize(ctrl, action, path, extra)
8
8
  @ctrl = ctrl
9
9
  @action = action
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flame
4
+ module Errors
5
+ ## Error for not found template file in Render
6
+ class TemplateNotFoundError < StandardError
7
+ def initialize(controller, path)
8
+ @controller = controller
9
+ @controller = @controller.class unless @controller.is_a? Class
10
+ @path = path
11
+ end
12
+
13
+ def message
14
+ "Template '#{@path}' not found for '#{@controller}'"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'forwardable'
4
+
3
5
  require_relative 'errors/argument_not_assigned_error'
4
6
 
5
7
  module Flame
@@ -53,7 +55,7 @@ module Flame
53
55
  path_part = PathPart.new parameter_name, arg: parameter_type
54
56
  path_part unless parts.include? path_part
55
57
  end
56
- self.class.new @path.empty? ? "/#{action}" : self, *parameters
58
+ self.class.new @path.empty? ? "/#{action}" : self, *parameters.compact
57
59
  end
58
60
 
59
61
  ## Can recieve other as String
@@ -8,6 +8,8 @@ require 'tilt/erb'
8
8
 
9
9
  require 'gorilla-patch/inflections'
10
10
 
11
+ require_relative 'errors/template_not_found_error'
12
+
11
13
  module Flame
12
14
  ## Helper for render functionality
13
15
  class Render
@@ -21,8 +23,9 @@ module Flame
21
23
  @locals = options.merge(options.delete(:locals) || {})
22
24
  ## Find filename
23
25
  @filename = find_file(path)
24
- # @controller.instance_exec { halt 404 } unless @filename
25
- return unless @filename
26
+ unless @filename
27
+ raise Flame::Errors::TemplateNotFoundError.new(controller, path)
28
+ end
26
29
  @layout = nil if File.basename(@filename)[0] == '_'
27
30
  end
28
31
 
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'errors/route_arguments_error'
3
+ require_relative 'errors/route_extra_arguments_error'
4
+ require_relative 'errors/route_arguments_order_error'
4
5
 
5
6
  module Flame
6
7
  module Validators
@@ -13,21 +14,28 @@ module Flame
13
14
  end
14
15
 
15
16
  def valid?
16
- ## Get hash of any extra arguments
17
- extra = %i[req opt].find do |type|
18
- found = extra_arguments(type).find do |place, args|
19
- break { place: place, type: type, args: args } if args.any?
20
- end
21
- break found if found
22
- end
23
- ## Return true if no any extra argument
24
- return true unless extra
25
- ## Raise error with extra arguments
26
- raise Errors::RouteArgumentsError.new(@ctrl, @action, @path, extra)
17
+ extra_valid? && order_valid?
27
18
  end
28
19
 
29
20
  private
30
21
 
22
+ def extra_valid?
23
+ extra_arguments = first_extra_arguments
24
+ ## Raise error if extra arguments
25
+ return true unless extra_arguments
26
+ raise Errors::RouteExtraArgumentsError.new(
27
+ @ctrl, @action, @path, extra_arguments
28
+ )
29
+ end
30
+
31
+ def order_valid?
32
+ wrong_ordered_arguments = first_wrong_ordered_arguments
33
+ return true unless wrong_ordered_arguments
34
+ raise Errors::RouteArgumentsOrderError.new(
35
+ @path, wrong_ordered_arguments
36
+ )
37
+ end
38
+
31
39
  ## Split path to args array
32
40
  def path_arguments
33
41
  @path_arguments ||= @path.parts
@@ -41,10 +49,9 @@ module Flame
41
49
 
42
50
  ## Take args from controller's action
43
51
  def action_arguments
44
- return @action_arguments if @action_arguments
45
52
  ## Get all parameters (arguments) from method
46
53
  ## Than collect and sort parameters into hash
47
- @ctrl.instance_method(@action).parameters
54
+ @action_arguments ||= @ctrl.instance_method(@action).parameters
48
55
  .each_with_object(req: [], opt: []) do |param, hash|
49
56
  ## Only required parameters must be in `:req`
50
57
  hash[param[0]] << param[1]
@@ -52,11 +59,33 @@ module Flame
52
59
  end
53
60
 
54
61
  ## Calculate path and action extra arguments
55
- def extra_arguments(type)
56
- {
57
- ctrl: action_arguments[type] - path_arguments[type],
58
- path: path_arguments[type] - action_arguments[type]
59
- }
62
+ def all_extra_arguments
63
+ %i[req opt].each_with_object({}) do |type, extra_arguments|
64
+ extra_arguments[type] = {
65
+ ctrl: action_arguments[type] - path_arguments[type],
66
+ path: path_arguments[type] - action_arguments[type]
67
+ }
68
+ end
69
+ end
70
+
71
+ def first_extra_arguments
72
+ ## Get hash of any extra arguments
73
+ all_extra_arguments.find do |type, extra_arguments|
74
+ found = extra_arguments.find do |place, args|
75
+ break { place: place, type: type, args: args } if args.any?
76
+ end
77
+ break found if found
78
+ end
79
+ end
80
+
81
+ def first_wrong_ordered_arguments
82
+ opt_arguments = action_arguments[:opt].zip(path_arguments[:opt])
83
+ opt_arguments.map! do |args|
84
+ args.map { |arg| Flame::Path::PathPart.new(arg, arg: :opt) }
85
+ end
86
+ opt_arguments.find do |action_argument, path_argument|
87
+ action_argument != path_argument
88
+ end
60
89
  end
61
90
  end
62
91
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Flame
4
- VERSION = '4.18.0'
4
+ VERSION = '4.18.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flame
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.18.0
4
+ version: 4.18.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Popov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-27 00:00:00.000000000 Z
11
+ date: 2017-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -230,8 +230,10 @@ files:
230
230
  - lib/flame/dispatcher/response.rb
231
231
  - lib/flame/dispatcher/static.rb
232
232
  - lib/flame/errors/argument_not_assigned_error.rb
233
- - lib/flame/errors/route_arguments_error.rb
233
+ - lib/flame/errors/route_arguments_order_error.rb
234
+ - lib/flame/errors/route_extra_arguments_error.rb
234
235
  - lib/flame/errors/route_not_found_error.rb
236
+ - lib/flame/errors/template_not_found_error.rb
235
237
  - lib/flame/path.rb
236
238
  - lib/flame/render.rb
237
239
  - lib/flame/router.rb