flame 5.0.0.rc3 → 5.0.0.rc4

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
  SHA1:
3
- metadata.gz: 85eb7af6c0680bae7f00910a6e7ba21746509241
4
- data.tar.gz: 38b4a71563308b2bde1e3456858eb6c6c6077bcb
3
+ metadata.gz: 259cf7c25a2317589f783f23035eacedceb74b99
4
+ data.tar.gz: 3dbf704b496815c17b9fa91abccf994de48d98f1
5
5
  SHA512:
6
- metadata.gz: 6cdb664fcf2109d1b38a34f7d70019c4118bb4489b1e2a6a5c1d45fab4806c1f972c5f6cd1a5f3597cdea2a589fa36a9cb07fc0ebccb8ad3af156dd946ab159f
7
- data.tar.gz: b3e61c07ab4ab996c2f5964b0ddc0380df3898e56c5b7c39e7372426db709524bfb0381da4bc52f01f94a1f998e98c373392d7bac8b3d1232d8e1726d85350ff
6
+ metadata.gz: acc9200dcfafcbf1619fb10c64c8785ab2de5008887b1f095d1ba2fc9957fdb130606b5d374e629389b1cc5951b9f4532285e87d0be17e8198b296939b2dc66d
7
+ data.tar.gz: f0e522ab711b0235623846a4e99559b8bcb06ce8c6ae78936da29fcab4c61a832c8748f420b081873492d589a9cb8b064ad6c235aed9c76170c84e85eda3200a
@@ -28,7 +28,7 @@ module Flame
28
28
  def require_dirs(dirs)
29
29
  caller_dir = File.dirname caller_file
30
30
  dirs.each do |dir|
31
- Dir[File.join(caller_dir, dir, '**', '*.rb')]
31
+ Dir[File.join(caller_dir, dir, '**/*.rb')]
32
32
  .reject { |file| File.executable?(file) }
33
33
  .sort_by { |s| [File.basename(s)[0], s] }
34
34
  .each { |file| require File.expand_path(file) }
@@ -82,8 +82,6 @@ module Flame
82
82
  caller(2..2).first.split(':')[0]
83
83
  end
84
84
 
85
- using GorillaPatch::DeepMerge
86
-
87
85
  ## Mount controller in application class
88
86
  ## @param controller [Symbol] the snake-cased name of mounted controller
89
87
  ## (without `Controller` or `::IndexController` for namespaces)
@@ -105,13 +103,9 @@ module Flame
105
103
  ## end
106
104
  def mount(controller_name, path = nil, &block)
107
105
  ## Add routes from controller to glob array
108
-
109
- routes_refine = Router::RoutesRefine.new(
106
+ router.add Router::RoutesRefine.new(
110
107
  router, namespace, controller_name, path, &block
111
108
  )
112
-
113
- router.routes.deep_merge! routes_refine.routes
114
- router.reverse_routes.merge! routes_refine.reverse_routes
115
109
  end
116
110
 
117
111
  using GorillaPatch::Namespace
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'forwardable'
4
- require 'gorilla-patch/namespace'
4
+ require 'gorilla_patch/namespace'
5
5
 
6
6
  require_relative 'controller/path_to'
7
7
  require_relative 'render'
@@ -40,6 +40,28 @@ module Flame
40
40
  end
41
41
  end
42
42
 
43
+ ## Re-define public instance method from module
44
+ ## @example Define actions from module in controller
45
+ ## class MyController < BaseController
46
+ ## include with_actions Module1
47
+ ## include with_actions Module2
48
+ ## ....
49
+ ## end
50
+ def self.with_actions(mod, exclude: [])
51
+ Module.new do
52
+ @mod = mod
53
+ @exclude = exclude
54
+
55
+ def self.included(ctrl)
56
+ ctrl.include @mod
57
+
58
+ (@mod.public_instance_methods - @exclude).each do |meth|
59
+ ctrl.send :define_method, meth, @mod.public_instance_method(meth)
60
+ end
61
+ end
62
+ end
63
+ end
64
+
43
65
  def_delegators(
44
66
  :@dispatcher,
45
67
  :config, :request, :params, :halt, :session, :response, :status, :body,
@@ -194,6 +216,17 @@ module Flame
194
216
  parts = [modules.last] if parts.empty?
195
217
  Flame::Path.merge nil, parts.join('_')
196
218
  end
219
+
220
+ def refined_http_methods
221
+ @refined_http_methods ||= []
222
+ end
223
+
224
+ Flame::Router::HTTP_METHODS.each do |http_method|
225
+ downcased_http_method = http_method.downcase
226
+ define_method(downcased_http_method) do |action_path, action = nil|
227
+ refined_http_methods << [downcased_http_method, action_path, action]
228
+ end
229
+ end
197
230
  end
198
231
  end
199
232
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'gorilla-patch/symbolize'
3
+ require 'gorilla_patch/symbolize'
4
4
 
5
5
  require_relative 'dispatcher/request'
6
6
  require_relative 'dispatcher/response'
@@ -14,10 +14,10 @@ require_relative 'errors/route_not_found_error'
14
14
  module Flame
15
15
  ## Helpers for dispatch Flame::Application#call
16
16
  class Dispatcher
17
- GEM_STATIC_FILES = File.join __dir__, '..', '..', 'public'
17
+ GEM_STATIC_FILES = File.join(__dir__, '../../public').freeze
18
18
 
19
19
  extend Forwardable
20
- def_delegators :@app_class, :path_to
20
+ def_delegators :@app_class, :router, :path_to
21
21
 
22
22
  attr_reader :request, :response
23
23
 
@@ -37,13 +37,15 @@ module Flame
37
37
  ## Start of execution the request
38
38
  def run!
39
39
  catch :halt do
40
+ validate_request
41
+
40
42
  try_options ||
41
43
  try_static ||
42
44
  try_static(dir: GEM_STATIC_FILES) ||
43
45
  try_route ||
44
46
  halt(404)
45
47
  end
46
- response.write body unless request.http_method == :HEAD
48
+ response.write body unless request.head?
47
49
  response.finish
48
50
  end
49
51
 
@@ -71,7 +73,13 @@ module Flame
71
73
 
72
74
  ## Parameters of the request
73
75
  def params
74
- @params ||= request.params.symbolize_keys(deep: true)
76
+ @params ||=
77
+ begin
78
+ request.params.symbolize_keys(deep: true)
79
+ rescue ArgumentError => e
80
+ raise unless e.message.include?('invalid %-encoding')
81
+ {}
82
+ end
75
83
  end
76
84
 
77
85
  ## Session object as Hash
@@ -92,8 +100,7 @@ module Flame
92
100
  ## Available routes endpoint
93
101
  def available_endpoint
94
102
  return @available_endpoint if defined? @available_endpoint
95
- @available_endpoint =
96
- @app_class.router.routes.navigate(*request.path.parts)
103
+ @available_endpoint = router.navigate(*request.path.parts)
97
104
  end
98
105
 
99
106
  ## Interrupt the execution of route, and set new optional data
@@ -141,6 +148,14 @@ module Flame
141
148
 
142
149
  private
143
150
 
151
+ def validate_request
152
+ ## https://github.com/rack/rack/issues/337#issuecomment-48555831
153
+ request.params
154
+ rescue ArgumentError => e
155
+ raise unless e.message.include?('invalid %-encoding')
156
+ halt 400
157
+ end
158
+
144
159
  ## Return response if HTTP-method is OPTIONS
145
160
  def try_options
146
161
  return unless request.http_method == :OPTIONS
@@ -11,7 +11,17 @@ module Flame
11
11
 
12
12
  ## Override HTTP-method of the request if the param '_method' found
13
13
  def http_method
14
- @http_method ||= (params['_method'] || request_method).upcase.to_sym
14
+ return @http_method if defined?(@http_method)
15
+
16
+ method_from_method =
17
+ begin
18
+ params['_method']
19
+ rescue ArgumentError => e
20
+ ## https://github.com/rack/rack/issues/337#issuecomment-48555831
21
+ raise unless e.message.include?('invalid %-encoding')
22
+ end
23
+
24
+ @http_method = (method_from_method || request_method).upcase.to_sym
15
25
  end
16
26
  end
17
27
  end
@@ -23,7 +23,7 @@ module Flame
23
23
  ## @param route [Flame::Route] route that must be executed
24
24
  def execute_route(route, action = route.action)
25
25
  params.merge!(
26
- @app_class.router.path_of(route).extract_arguments(request.path)
26
+ router.path_of(route).extract_arguments(request.path)
27
27
  )
28
28
  # route.execute(self)
29
29
  controller = route.controller.new(self)
@@ -42,7 +42,7 @@ module Flame
42
42
  ## Return nil if must be no body for current HTTP status
43
43
  return if Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include?(status)
44
44
  ## Find the nearest route by the parts of requested path
45
- route = @app_class.router.find_nearest_route(request.path)
45
+ route = router.find_nearest_route(request.path)
46
46
  ## Return standard `default_body` if the route not found
47
47
  return default_body unless route
48
48
  controller = route.controller.new(self)
@@ -34,7 +34,7 @@ module Flame
34
34
  class StaticFile
35
35
  def initialize(filename, dir)
36
36
  @filename = filename.to_s
37
- @file_path = File.join dir, URI.decode_www_form_component(@filename)
37
+ @file_path = File.join dir, CGI.unescape(@filename)
38
38
  end
39
39
 
40
40
  def exist?
@@ -86,14 +86,7 @@ module Flame
86
86
  ## @param other_path [Flame::Path] other path with values at arguments
87
87
  ## @return [Hash{Symbol => String}] hash of arguments from two paths
88
88
  def extract_arguments(other_path)
89
- parts.each_with_index.with_object({}) do |(part, i), args|
90
- other_part = other_path.parts[i].to_s
91
- next args unless part.arg?
92
- break args if part.opt_arg? && other_part.empty?
93
- args[
94
- part[(part.opt_arg? ? 2 : 1)..-1].to_sym
95
- ] = URI.decode_www_form_component(other_part)
96
- end
89
+ Extractor.new(parts, other_path.parts).run
97
90
  end
98
91
 
99
92
  ## Assign arguments to path for `Controller#path_to`
@@ -137,11 +130,55 @@ module Flame
137
130
  param
138
131
  end
139
132
 
133
+ ## Class for extracting arguments from other path
134
+ class Extractor
135
+ def initialize(parts, other_parts)
136
+ @parts = parts
137
+ @other_parts = other_parts
138
+
139
+ @index = 0
140
+ @other_index = 0
141
+
142
+ @args = {}
143
+ end
144
+
145
+ def run
146
+ @parts.each do |part|
147
+ next static_part_found unless part.arg?
148
+
149
+ break if part.opt_arg? && @other_parts.count <= @other_index
150
+
151
+ @args[part.clean.to_sym] = extract
152
+ @index += 1
153
+ end
154
+
155
+ @args
156
+ end
157
+
158
+ private
159
+
160
+ def static_part_found
161
+ @index += 1
162
+ @other_index += 1
163
+ end
164
+
165
+ def extract
166
+ other_part = @other_parts[@other_index]
167
+
168
+ return if @parts[@index.next] == other_part
169
+
170
+ @other_index += 1
171
+ URI.decode_www_form_component(other_part)
172
+ end
173
+ end
174
+
175
+ private_constant :Extractor
176
+
140
177
  ## Class for one part of Path
141
178
  class Part
142
179
  extend Forwardable
143
180
 
144
- def_delegators :@part, :[], :hash
181
+ def_delegators :@part, :[], :hash, :empty?, :b
145
182
 
146
183
  ARG_CHAR = ':'
147
184
  ARG_CHAR_OPT = '?'
@@ -174,6 +211,7 @@ module Flame
174
211
  def to_s
175
212
  @part
176
213
  end
214
+ alias to_str to_s
177
215
 
178
216
  ## Is the path part an argument
179
217
  ## @return [true, false] an argument or not
@@ -6,7 +6,7 @@ require 'tilt'
6
6
  require 'tilt/plain'
7
7
  require 'tilt/erb'
8
8
 
9
- require 'gorilla-patch/inflections'
9
+ require 'gorilla_patch/inflections'
10
10
 
11
11
  require_relative 'errors/template_not_found_error'
12
12
 
@@ -89,7 +89,17 @@ module Flame
89
89
 
90
90
  ## Find template-file by path
91
91
  def find_file(path)
92
- find_files(path, controller_dirs).find { |file| Tilt[file] }
92
+ caller_path = caller_locations(4..4).first.path
93
+
94
+ caller_dir =
95
+ begin
96
+ File.dirname(caller_path).sub(views_dir, '') if Tilt[caller_path]
97
+ rescue LoadError
98
+ nil
99
+ end
100
+
101
+ find_files(path, controller_dirs | Array(caller_dir))
102
+ .find { |file| Tilt[file] }
93
103
  end
94
104
 
95
105
  ## Find layout-files by path
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'gorilla-patch/deep_merge'
4
- require 'gorilla-patch/inflections'
5
- require 'gorilla-patch/namespace'
6
- require 'gorilla-patch/transform'
3
+ require 'gorilla_patch/deep_merge'
4
+ require 'gorilla_patch/inflections'
5
+ require 'gorilla_patch/namespace'
6
+ require 'gorilla_patch/transform'
7
7
 
8
8
  require_relative 'router/routes'
9
9
  require_relative 'router/route'
@@ -11,6 +11,11 @@ require_relative 'router/route'
11
11
  module Flame
12
12
  ## Router class for routing
13
13
  class Router
14
+ HTTP_METHODS = %i[GET POST PUT PATCH DELETE].freeze
15
+
16
+ extend Forwardable
17
+ def_delegators :routes, :navigate
18
+
14
19
  attr_reader :app, :routes, :reverse_routes
15
20
 
16
21
  ## @param app [Flame::Application] host application
@@ -20,6 +25,15 @@ module Flame
20
25
  @reverse_routes = {}
21
26
  end
22
27
 
28
+ using GorillaPatch::DeepMerge
29
+
30
+ ## Add RoutesRefine to Router
31
+ ## @param routes_refine [Flame::Router::RoutesRefine] refined routes
32
+ def add(routes_refine)
33
+ routes.deep_merge! routes_refine.routes
34
+ reverse_routes.merge! routes_refine.reverse_routes
35
+ end
36
+
23
37
  ## Find the nearest route by path
24
38
  ## @param path [Flame::Path] path for route finding
25
39
  ## @return [Flame::Route, nil] return the found nearest route or `nil`
@@ -88,7 +102,7 @@ module Flame
88
102
  end
89
103
  end
90
104
 
91
- %i[GET POST PUT PATCH DELETE].each do |http_method|
105
+ HTTP_METHODS.each do |http_method|
92
106
  ## Define refine methods for all HTTP methods
93
107
  ## @overload post(path, action)
94
108
  ## Execute action on requested path and HTTP method
@@ -168,6 +182,10 @@ module Flame
168
182
 
169
183
  ## Execute block of refinings end sorting routes
170
184
  def execute(&block)
185
+ @controller.refined_http_methods
186
+ .each do |http_method, action_path, action|
187
+ send(http_method, action_path, action)
188
+ end
171
189
  instance_exec(&block) if block
172
190
  defaults
173
191
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Flame
4
- VERSION = '5.0.0.rc3'
4
+ VERSION = '5.0.0.rc4'
5
5
  end
@@ -6,7 +6,7 @@ source 'https://rubygems.org'
6
6
  git_source(:github) { |name| "https://github.com/#{name}.git" }
7
7
 
8
8
  ## system
9
- # gem 'gorilla-patch'
9
+ # gem 'gorilla_patch'
10
10
 
11
11
  ## server
12
12
  gem 'flame', github: 'AlexWayfer/flame'
@@ -70,7 +70,7 @@ class Question
70
70
  end
71
71
  end
72
72
 
73
- DB_CONFIG_FILE = File.join(__dir__, 'config', 'database.yml')
73
+ DB_CONFIG_FILE = File.join(__dir__, 'config/database.yml').freeze
74
74
 
75
75
  if File.exist? DB_CONFIG_FILE
76
76
  namespace :db do
@@ -97,7 +97,7 @@ if File.exist? DB_CONFIG_FILE
97
97
 
98
98
  DB_EXTENSIONS = %w[citext pgcrypto].freeze
99
99
 
100
- PGPASS_FILE = File.expand_path File.join('~', '.pgpass')
100
+ PGPASS_FILE = File.expand_path('~/.pgpass').freeze
101
101
 
102
102
  PGPASS_LINE =
103
103
  DB_CONFIG
@@ -556,7 +556,7 @@ if File.exist? DB_CONFIG_FILE
556
556
  end
557
557
 
558
558
  namespace :locales do
559
- CROWDIN_CONFIG_FILE = File.join('config', 'crowdin.yml')
559
+ CROWDIN_CONFIG_FILE = 'config/crowdin.yml'
560
560
 
561
561
  desc 'Upload files for translation'
562
562
  task :upload do
@@ -668,7 +668,7 @@ end
668
668
  namespace :static do
669
669
  desc 'Check static files'
670
670
  task :check do
671
- Dir[File.join(__dir__, 'public', '**', '*')].each do |file|
671
+ Dir[File.join(__dir__, 'public/**/*')].each do |file|
672
672
  basename = File.basename(file)
673
673
  grep_options = '--exclude-dir={\.git,log} --color=always'
674
674
  found = `grep -ir '#{basename}' ./ #{grep_options}`
@@ -689,7 +689,7 @@ namespace :config do
689
689
  task :check do
690
690
  example_suffix = '.example'
691
691
  Dir[
692
- File.join(__dir__, 'config', '**', "*#{example_suffix}*")
692
+ File.join(__dir__, "config/**/*#{example_suffix}*")
693
693
  ].each do |example_filename|
694
694
  regular_filename = example_filename.sub(example_suffix, '')
695
695
  if File.exist? regular_filename
@@ -770,7 +770,7 @@ end
770
770
  ## Update from git without migrations and restart (for static files update)
771
771
  desc 'Deploy to production server'
772
772
  task :deploy, :without_restart do |_t, args|
773
- servers = YAML.load_file File.join(__dir__, 'config', 'deploy.yml')
773
+ servers = YAML.load_file File.join(__dir__, 'config/deploy.yml')
774
774
  rake_command = "rake update[master#{',true' if args.without_restart}]"
775
775
  servers.each do |server|
776
776
  update_command = "cd #{server[:path]} && #{rake_command}"
@@ -47,7 +47,7 @@ worker_timeout env_config[:daemonize] ? 15 : 1_000_000
47
47
  threads 0, env_config[:threads_count] || 4
48
48
  daemonize env_config[:daemonize]
49
49
 
50
- # bind 'unix://' + File.join(%w[tmp sockets puma.sock])
50
+ # bind 'unix://tmp/sockets/puma.sock'
51
51
  env_config[:binds].each do |type, value|
52
52
  value = "#{value[:host]}:#{value[:port]}" if type == :tcp
53
53
  FileUtils.mkdir_p File.join(root_dir, File.dirname(value)) if type == :unix
@@ -111,11 +111,11 @@ def delete_filewatcher_pids_file
111
111
  end
112
112
 
113
113
  def server_config
114
- @server_config ||= YAML.load_file(File.join(__dir__, 'config', 'server.yml'))
114
+ @server_config ||= YAML.load_file(File.join(__dir__, 'config/server.yml'))
115
115
  end
116
116
 
117
117
  def puma_config_file
118
- @puma_config_file ||= File.join(__dir__, 'config', 'puma.rb')
118
+ @puma_config_file ||= File.join(__dir__, 'config/puma.rb')
119
119
  end
120
120
 
121
121
  def puma_pid_file
@@ -137,11 +137,11 @@ def environment
137
137
  end
138
138
 
139
139
  def log_files
140
- File.join(__dir__, %w[log {stdout,stderr}])
140
+ File.join(__dir__, 'log/{stdout,stderr}')
141
141
  end
142
142
 
143
143
  def waiting_mailing_lock
144
- while Dir[File.join(__dir__, 'tmp', 'mailing_*')].any?
144
+ while Dir[File.join(__dir__, 'tmp/mailing_*')].any?
145
145
  puts "\e[31m\e[1mMails sending in progress!\e[22m\e[0m\nWaiting..."
146
146
  sleep 1
147
147
  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: 5.0.0.rc3
4
+ version: 5.0.0.rc4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Popov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-15 00:00:00.000000000 Z
11
+ date: 2018-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.5'
27
27
  - !ruby/object:Gem::Dependency
28
- name: gorilla-patch
28
+ name: gorilla_patch
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
@@ -190,14 +190,14 @@ dependencies:
190
190
  requirements:
191
191
  - - "~>"
192
192
  - !ruby/object:Gem::Version
193
- version: '0'
193
+ version: '1'
194
194
  type: :development
195
195
  prerelease: false
196
196
  version_requirements: !ruby/object:Gem::Requirement
197
197
  requirements:
198
198
  - - "~>"
199
199
  - !ruby/object:Gem::Version
200
- version: '0'
200
+ version: '1'
201
201
  - !ruby/object:Gem::Dependency
202
202
  name: rack-utf8_sanitizer
203
203
  requirement: !ruby/object:Gem::Requirement
@@ -230,16 +230,16 @@ dependencies:
230
230
  name: rubocop
231
231
  requirement: !ruby/object:Gem::Requirement
232
232
  requirements:
233
- - - '='
233
+ - - "~>"
234
234
  - !ruby/object:Gem::Version
235
- version: '0.51'
235
+ version: '0.54'
236
236
  type: :development
237
237
  prerelease: false
238
238
  version_requirements: !ruby/object:Gem::Requirement
239
239
  requirements:
240
- - - '='
240
+ - - "~>"
241
241
  - !ruby/object:Gem::Version
242
- version: '0.51'
242
+ version: '0.54'
243
243
  - !ruby/object:Gem::Dependency
244
244
  name: simplecov
245
245
  requirement: !ruby/object:Gem::Requirement
@@ -320,7 +320,12 @@ files:
320
320
  homepage: https://github.com/AlexWayfer/flame
321
321
  licenses:
322
322
  - MIT
323
- metadata: {}
323
+ metadata:
324
+ bug_tracker_uri: https://github.com/AlexWayfer/flame/issues
325
+ documentation_uri: http://www.rubydoc.info/gems/flame/5.0.0.rc4
326
+ homepage_uri: https://github.com/AlexWayfer/flame
327
+ source_code_uri: https://github.com/AlexWayfer/flame
328
+ wiki_uri: https://github.com/AlexWayfer/flame/wiki
324
329
  post_install_message:
325
330
  rdoc_options: []
326
331
  require_paths:
@@ -329,7 +334,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
329
334
  requirements:
330
335
  - - ">="
331
336
  - !ruby/object:Gem::Version
332
- version: '0'
337
+ version: 2.3.0
333
338
  required_rubygems_version: !ruby/object:Gem::Requirement
334
339
  requirements:
335
340
  - - ">"
@@ -337,7 +342,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
337
342
  version: 1.3.1
338
343
  requirements: []
339
344
  rubyforge_project:
340
- rubygems_version: 2.6.14
345
+ rubygems_version: 2.6.14.1
341
346
  signing_key:
342
347
  specification_version: 4
343
348
  summary: Web-framework, based on MVC-pattern