prelands_rails 0.1.2 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,3 +2,25 @@
2
2
 
3
3
  Код, занимающийся валидацией и публикацией прелендов V4.
4
4
 
5
+ # How to publish gem
6
+
7
+ 1. Bump version inside `prelands_rails.gemspec`.
8
+ 2. Make commit & push to `master` branch.
9
+ 3. Then checkout `tags` branch and `git rebase master` (accept theirs if there are any conflicts).
10
+ 4. Then `git push` - and Gitlab CI publish the new version of the gem.
11
+
12
+ # Local developing
13
+
14
+ Execute in bash console using the actual path to the gem folder:
15
+
16
+ bundle config local.prelands_rails /home/scout/git/prelands_rails/
17
+
18
+ Expected output:
19
+
20
+ You are replacing the current global value of local.prelands_rails, which is currently "/home/scout/git/preland_rails/"
21
+
22
+ In the Gemfile of a host application specify actual gem version, branch and semi-github path:
23
+
24
+ gem 'prelands_rails', '0.1.4', github: 'prelands_rails/prelands_rails', branch: 'master'
25
+
26
+ Run `bundle`, launch puma server. Now your host application uses the local files.
@@ -92,6 +92,33 @@ module PrelandsRails
92
92
  end
93
93
  end
94
94
 
95
+ # Переместит файл в пределах +bucket_name+
96
+ # @return true если успешно переместил
97
+ def move_file(s3_old_file_name, s3_new_file_name, bucket_name)
98
+ log :move_file, ['', s3_old_file_name, ' moving to ', s3_new_file_name, ' in ', bucket_name, '.'].join('"')
99
+
100
+ obj = Aws::S3::Object.new(bucket_name, s3_old_file_name, client: @s3.client)
101
+ new_target = [bucket_name, s3_new_file_name].join('/')
102
+
103
+ begin
104
+ obj.move_to new_target
105
+ rescue Aws::S3::Errors::NoSuchKey
106
+ log :move_file, 'Aws::S3::Errors::NoSuchKey: The specified key does not exist: "%s"' % s3_old_file_name
107
+ return false
108
+ end
109
+
110
+ obj = Aws::S3::Object.new(bucket_name, s3_new_file_name, client: @s3.client)
111
+
112
+ begin
113
+ obj.acl.put({ acl: ACL_PUBLIC_READ })
114
+ rescue Aws::S3::Errors::NoSuchKey
115
+ log :move_file_acl_put, 'Aws::S3::Errors::NoSuchKey: The specified key does not exist: "%s"' % s3_old_file_name
116
+ return false
117
+ end
118
+
119
+ true
120
+ end
121
+
95
122
  private
96
123
 
97
124
  def log(method, output)
@@ -86,7 +86,7 @@ module PrelandsRails
86
86
  title_rx = /<title>[^<]+<\/title>/
87
87
  return if @string[title_rx]
88
88
 
89
- 'Tag <title> not found.'
89
+ 'Tag `title` not found.'
90
90
  end
91
91
 
92
92
  def check_a_hrefs
@@ -97,10 +97,10 @@ module PrelandsRails
97
97
  end
98
98
 
99
99
  def check_favicon
100
- favicon_rx = '<link rel="shortcut icon" href="icon.ico">'
100
+ favicon_rx = /<link.+href="icon.ico">/
101
101
  return if @string[favicon_rx]
102
102
 
103
- '<link rel="shortcut icon" href="icon.ico"> not found.'
103
+ '`link href="icon.ico"` not found.'
104
104
  end
105
105
 
106
106
  protected
@@ -16,13 +16,17 @@ module PrelandsRails
16
16
  end
17
17
 
18
18
  assures do
19
+ optional(:warnings).maybe(type?: Array)
20
+ end
19
21
 
22
+ before do
23
+ context.warnings ||= []
20
24
  end
21
25
 
22
26
  def act
23
27
  content = context[:files_content]['index.js']
24
28
  content = Js.new(content)
25
- fail! errors: {'index.js' => content.errors} unless content.valid?
29
+ context.warnings += content.errors unless content.valid?
26
30
  end
27
31
 
28
32
  on_breach { |breaches| bad_expects breaches }
@@ -15,7 +15,7 @@ module PrelandsRails
15
15
  end
16
16
 
17
17
  assures do
18
-
18
+ optional(:warnings).maybe(type?: Array)
19
19
  end
20
20
 
21
21
  organize ValidateIncomingFiles, ValidateHtml, ValidateJs, ValidateCss
@@ -34,6 +34,7 @@ module PrelandsRails
34
34
  required(:files_content).value(type?: Hash) # распакованные файлы из архива { file_name[String] => file_content[String], ... }
35
35
  required(:compiled_htmls).value(type?: Hash) # скомпилированные исходники { file_name[String] => compiled_content[String], ... }
36
36
  required(:preland_simple_source).filled
37
+ optional(:warnings).maybe(type?: Array)
37
38
  end
38
39
 
39
40
  organize DetectIncomingLocales,
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrelandsRails
4
+ class RecompileSimpleSource
5
+ #
6
+ # Скачает и разместит в cache-директории zip-архив с исходником лендинга.
7
+ #
8
+ class DownloadZip
9
+ include ::PrelandsRails::AbstractInteractor
10
+ include ::Interactor
11
+ include ::Interactor::Contracts
12
+ include ::PrelandsRails::Base
13
+
14
+ expects do
15
+ required(:prelanding).filled #.value(type?: ::Preland)
16
+ end
17
+
18
+ assures do
19
+ required(:archive).filled # Rack::Test::UploadedFile | ActionDispatch::Http::UploadedFile
20
+ required(:tmp_dest).value(type?: String) # путь к временному файлу
21
+ end
22
+
23
+ def act
24
+ archive_url = prelanding.preland_simple_source.archive.url
25
+ destination = make_tmp_path
26
+
27
+ IO.copy_stream(Kernel.open(archive_url), destination)
28
+
29
+ context.archive = ActionDispatch::Http::UploadedFile.new(tempfile: destination)
30
+ context.tmp_dest = destination
31
+ end
32
+
33
+ on_breach { |breaches| bad_expects breaches }
34
+
35
+ delegate :prelanding,
36
+ :to => :context
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrelandsRails
4
+ class RecompileSimpleSource
5
+ class RenameZipFile
6
+ include ::PrelandsRails::AbstractInteractor
7
+ include ::Interactor
8
+ include ::Interactor::Contracts
9
+
10
+ expects do
11
+ required(:aws_prefix).value(type?: String)
12
+ required(:old_aws_prefix).value(type?: String)
13
+ required(:s3_credentials).schema do
14
+ required(:access_key).filled(:str?)
15
+ required(:secret_key).filled(:str?)
16
+ required(:region).filled(:str?)
17
+ end
18
+ required(:zip_bucket_name).filled(:str?)
19
+ end
20
+
21
+ assures do
22
+ required(:is_zip_moved).filled(:bool?)
23
+ end
24
+
25
+ def act
26
+ old_name = '%s/src.zip' % context.old_aws_prefix
27
+ new_name = '%s/src.zip' % context.aws_prefix
28
+
29
+ context.is_zip_moved =
30
+ client.move_file old_name, new_name, context.zip_bucket_name
31
+ end
32
+
33
+ private
34
+
35
+ def client
36
+ return @client if defined?(@client)
37
+
38
+ creds = context.s3_credentials
39
+ @client = ::PrelandsRails::MyAwsClient.new creds[:access_key], creds[:secret_key], creds[:region]
40
+ end
41
+
42
+ on_breach { |breaches| bad_expects breaches }
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrelandsRails
4
+ class RecompileSimpleSource
5
+ #
6
+ # обновит запись ::Preland::SimpleSource
7
+ #
8
+ class UpdateRecord
9
+ include ::PrelandsRails::AbstractInteractor
10
+ include ::Interactor
11
+ include ::Interactor::Contracts
12
+ include ::PrelandsRails::Base
13
+
14
+ expects do
15
+ required(:aws_prefix).value(type?: String)
16
+ required(:old_aws_prefix).value(type?: String)
17
+ required(:model_preland_simple_source).filled
18
+ end
19
+
20
+ assures do
21
+ required(:preland_simple_source).filled
22
+ end
23
+
24
+ def act
25
+ source.aws_prefix = context.aws_prefix
26
+ source.save!
27
+ context.preland_simple_source = source
28
+ end
29
+
30
+ private
31
+
32
+ def source
33
+ @source ||= context.model_preland_simple_source.find_by aws_prefix: context.old_aws_prefix
34
+ end
35
+
36
+ on_breach { |breaches| bad_expects breaches }
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrelandsRails
4
+ class RecompileSimpleSource
5
+ #
6
+ # Сначала удалить файлы преленда из директории +aws_prefix+, затем загрузить.
7
+ #
8
+ class Upload
9
+ include ::PrelandsRails::AbstractInteractor
10
+ include ::Interactor
11
+ include ::Interactor::Contracts
12
+ include ::PrelandsRails::UpdateSimpleSource::Upload::DeleteCompiledFiles
13
+
14
+ expects do
15
+ required(:archive).filled # Входящий архив с исходниками от фрилансера. Rack::Test::UploadedFile | ActionDispatch::Http::UploadedFile
16
+ required(:compiled_htmls).value(type?: Hash) # скомпилированные исходники { file_name[String] => compiled_content[String], ... }
17
+ required(:s3_credentials).schema do
18
+ required(:access_key).filled(:str?)
19
+ required(:secret_key).filled(:str?)
20
+ required(:region).filled(:str?)
21
+ end
22
+ required(:bucket_names).value(type?: Array)
23
+ required(:aws_prefix).value(type?: String)
24
+ required(:files_content).value(type?: Hash) # { file_name => file_content, ... }
25
+ end
26
+
27
+ assures do
28
+ end
29
+
30
+ def act
31
+ delete_compiled_files
32
+ result = ::PrelandsRails::CreateSimpleSource::Upload.call context
33
+ fail! errors: result.errors unless result.success?
34
+ end
35
+
36
+ on_breach { |breaches| bad_expects breaches }
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrelandsRails
4
+ #
5
+ # Перекомпилировать преленд на основе уже загруженного в aws архива.
6
+ #
7
+ class RecompileSimpleSource
8
+ include ::PrelandsRails::AbstractInteractor
9
+ include ::Interactor::Organizer
10
+ include ::Interactor::Contracts
11
+
12
+ expects do
13
+ required(:prelanding).filled#.value(type?: ::Preland)
14
+ required(:static_js_path).value(type?: String)
15
+ required(:s3_credentials).schema do
16
+ required(:access_key).filled(:str?)
17
+ required(:secret_key).filled(:str?)
18
+ required(:region).filled(:str?)
19
+ end
20
+ required(:bucket_names).value(type?: Array)
21
+ required(:model_preland_simple_source).filled
22
+ end
23
+
24
+ before do
25
+ context.expected_locales = context.prelanding.locales.map(&:short_name)
26
+ context.aws_prefix = context.prelanding.preland_simple_source.aws_prefix
27
+ end
28
+
29
+ assures do
30
+ required(:expected_locales).filled(type?: Array)
31
+ required(:aws_prefix).value(type?: String)
32
+ required(:archive).filled
33
+ required(:tmp_dest).value(type?: String)
34
+ required(:incoming_locales).filled(type?: Array)
35
+ required(:files_content).value(type?: Hash)
36
+ required(:compiled_htmls).value(type?: Hash)
37
+ required(:old_aws_prefix).value(type?: String)
38
+ required(:is_zip_moved).filled(:bool?)
39
+ end
40
+
41
+ after do
42
+ clear_tmp_dest
43
+ context.prelanding.preland_simple_source.update_columns updated_at: Time.now
44
+ end
45
+
46
+ organize DownloadZip,
47
+ ::PrelandsRails::CreateSimpleSource::DetectIncomingLocales,
48
+ ::PrelandsRails::CreateSimpleSource::CheckZipFiles,
49
+ ::PrelandsRails::CreateSimpleSource::Compile,
50
+ ::PrelandsRails::UpdateSimpleSource::GenerateNewAwsPrefix,
51
+ Upload,
52
+ UpdateRecord, # update aws_prefix
53
+ ::PrelandsRails::UpdateSimpleSource::RemoveContentFromOldAwsPrefix,
54
+ RenameZipFile
55
+
56
+ on_breach { |breaches| bad_expects breaches }
57
+
58
+ private
59
+
60
+ def clear_tmp_dest
61
+ td = context.tmp_dest
62
+ File.delete(td) if File.exist?(td)
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrelandsRails
4
+ class UpdateSimpleSource
5
+
6
+ class GenerateNewAwsPrefix
7
+ include ::PrelandsRails::AbstractInteractor
8
+ include ::Interactor
9
+ include ::Interactor::Contracts
10
+
11
+ expects do
12
+ required(:aws_prefix).value(type?: String)
13
+ end
14
+
15
+ assures do
16
+ required(:old_aws_prefix).value(type?: String)
17
+ end
18
+
19
+ def act
20
+ context.old_aws_prefix = context.aws_prefix
21
+ context.aws_prefix = SecureRandom.uuid[0..7]
22
+ end
23
+
24
+ on_breach { |breaches| bad_expects breaches }
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrelandsRails
4
+ class UpdateSimpleSource
5
+
6
+ class RemoveContentFromOldAwsPrefix
7
+ include ::PrelandsRails::AbstractInteractor
8
+ include ::Interactor
9
+ include ::Interactor::Contracts
10
+ include ::PrelandsRails::UpdateSimpleSource::Upload::DeleteCompiledFiles
11
+
12
+ expects do
13
+ required(:old_aws_prefix).value(type?: String)
14
+ required(:s3_credentials).schema do
15
+ required(:access_key).filled(:str?)
16
+ required(:secret_key).filled(:str?)
17
+ required(:region).filled(:str?)
18
+ end
19
+ required(:bucket_names).value(type?: Array)
20
+ end
21
+
22
+ def act
23
+ delete_compiled_files(context.old_aws_prefix)
24
+ end
25
+
26
+ on_breach { |breaches| bad_expects breaches }
27
+ end
28
+ end
29
+ end
@@ -11,7 +11,8 @@ module PrelandsRails
11
11
 
12
12
  expects do
13
13
  required(:archive) # Входящий архив с исходниками от фрилансера. Rack::Test::UploadedFile | ActionDispatch::Http::UploadedFile
14
- required(:aws_prefix).value(type?: String) # имя директории в букете
14
+ required(:aws_prefix).value(type?: String)
15
+ required(:old_aws_prefix).value(type?: String)
15
16
  required(:preland_id).filled
16
17
  required(:preland_domain_ids).value(type?: Array)
17
18
  required(:incoming_locales).filled(type?: Array)
@@ -27,10 +28,11 @@ module PrelandsRails
27
28
  end
28
29
 
29
30
  def act
30
- source = context.model_preland_simple_source.find_by aws_prefix: context.aws_prefix
31
+ source = context.model_preland_simple_source.find_by aws_prefix: context.old_aws_prefix
31
32
  source.archive = context.archive if context.archive
32
33
  source.preland_domain_ids = context.preland_domain_ids
33
34
  source.locales = context.incoming_locales
35
+ source.aws_prefix = context.aws_prefix
34
36
  source.save!
35
37
  context.preland_simple_source = source
36
38
  end
@@ -7,16 +7,17 @@ module PrelandsRails
7
7
  # Удалить из +bucket_names+ файлы преленда из директории +aws_prefix+.
8
8
  #
9
9
  module DeleteCompiledFiles
10
- def delete_compiled_files
11
- creds = context.s3_credentials
12
- client = ::PrelandsRails::MyAwsClient.new creds[:access_key], creds[:secret_key], creds[:region]
10
+ def delete_compiled_files(that_aws_prefix = nil)
11
+ aws_prefix = that_aws_prefix || context.aws_prefix
12
+ creds = context.s3_credentials
13
+ client = ::PrelandsRails::MyAwsClient.new creds[:access_key], creds[:secret_key], creds[:region]
13
14
 
14
15
  context.bucket_names.each do |bucket_name|
15
16
  files = client.list_objects bucket_name
16
17
  next unless files
17
18
 
18
19
  files = files.map do |file_name|
19
- { key: file_name } if file_name.index(context.aws_prefix) == 0
20
+ { key: file_name } if file_name.index(aws_prefix) == 0
20
21
  end.compact
21
22
 
22
23
  next if files.empty?
@@ -13,17 +13,15 @@ module PrelandsRails
13
13
  end
14
14
 
15
15
  assures do
16
-
17
- end
18
-
19
- before do
20
-
16
+ optional(:warnings).maybe(type?: Array)
21
17
  end
22
18
 
23
19
  def act
24
20
  if context.files_content
25
21
  result = ::PrelandsRails::CreateSimpleSource::ValidateZipContent.call context
26
- unless result.success?
22
+ if result.success?
23
+ context.warnings = result.warnings
24
+ else
27
25
  fail!(errors: result.errors)
28
26
  end
29
27
  end
@@ -2,7 +2,11 @@
2
2
 
3
3
  module PrelandsRails
4
4
  #
5
- # Примет zip-файл с исходниками преленда и опубликует этот преленд.
5
+ # Примет zip-файл с исходниками преленда и обновит исходники этого преленда.
6
+ # В результате будет обновлён `aws_prefix` -- имя директории в букете,
7
+ # что позволит избежать кэширования статического контента. . Другими словами,
8
+ # при обновлении файлов не надо ждать, пока Amazon обновит кэш или
9
+ # очищать кэш Amazon-а.
6
10
  #
7
11
  class UpdateSimpleSource
8
12
  include ::PrelandsRails::AbstractInteractor
@@ -34,14 +38,17 @@ module PrelandsRails
34
38
  optional(:files_content).maybe(type?: Hash) # распакованные файлы из архива { file_name[String] => file_content[String], ... }
35
39
  optional(:compiled_htmls).value(type?: Hash) # скомпилированные исходники { file_name[String] => compiled_content[String], ... }
36
40
  required(:preland_simple_source).filled
41
+ optional(:warnings).maybe(type?: Array)
37
42
  end
38
43
 
39
44
  organize ::PrelandsRails::CreateSimpleSource::DetectIncomingLocales,
40
45
  CheckZipFiles,
41
46
  ValidateZipContent,
42
47
  Compile,
43
- Upload,
44
- UpdateRecord
48
+ GenerateNewAwsPrefix,
49
+ Upload, # to new aws_prefix
50
+ UpdateRecord, # also update aws_prefix
51
+ RemoveContentFromOldAwsPrefix
45
52
 
46
53
  on_breach { |breaches| bad_expects breaches }
47
54
  end
@@ -15,6 +15,7 @@ module PrelandsRails
15
15
  end
16
16
 
17
17
  assures do
18
+ optional(:warnings).maybe(type?: Array)
18
19
  end
19
20
 
20
21
  organize ::PrelandsRails::CreateSimpleSource::DetectIncomingLocales,
@@ -32,6 +32,8 @@ require 'prelands_rails/create_simple_source'
32
32
  require 'prelands_rails/update_simple_source/upload/delete_compiled_files'
33
33
  require 'prelands_rails/update_simple_source/check_zip_files'
34
34
  require 'prelands_rails/update_simple_source/compile'
35
+ require 'prelands_rails/update_simple_source/generate_new_aws_prefix'
36
+ require 'prelands_rails/update_simple_source/remove_content_from_old_aws_prefix'
35
37
  require 'prelands_rails/update_simple_source/update_record'
36
38
  require 'prelands_rails/update_simple_source/upload'
37
39
  require 'prelands_rails/update_simple_source/validate_zip_content'
@@ -39,5 +41,11 @@ require 'prelands_rails/update_simple_source'
39
41
 
40
42
  require 'prelands_rails/validate_simple_source'
41
43
 
44
+ require 'prelands_rails/recompile_simple_source/download_zip'
45
+ require 'prelands_rails/recompile_simple_source/rename_zip_file'
46
+ require 'prelands_rails/recompile_simple_source/update_record'
47
+ require 'prelands_rails/recompile_simple_source/upload'
48
+ require 'prelands_rails/recompile_simple_source'
49
+
42
50
  module PrelandsRails
43
51
  end
@@ -5,7 +5,7 @@ end
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "prelands_rails"
8
- spec.version = "0.1.2"
8
+ spec.version = "0.1.5"
9
9
  spec.authors = ["C80609A"]
10
10
  spec.email = ["c080609a@gmail.com"]
11
11
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prelands_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - C80609A
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-12-23 00:00:00.000000000 Z
11
+ date: 2022-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: interactor
@@ -312,9 +312,16 @@ files:
312
312
  - lib/prelands_rails/create_simple_source/validate_zip_content/validate_incoming_files/zip_archive.rb
313
313
  - lib/prelands_rails/create_simple_source/validate_zip_content/validate_js.rb
314
314
  - lib/prelands_rails/create_simple_source/validate_zip_content/validate_js/js.rb
315
+ - lib/prelands_rails/recompile_simple_source.rb
316
+ - lib/prelands_rails/recompile_simple_source/download_zip.rb
317
+ - lib/prelands_rails/recompile_simple_source/rename_zip_file.rb
318
+ - lib/prelands_rails/recompile_simple_source/update_record.rb
319
+ - lib/prelands_rails/recompile_simple_source/upload.rb
315
320
  - lib/prelands_rails/update_simple_source.rb
316
321
  - lib/prelands_rails/update_simple_source/check_zip_files.rb
317
322
  - lib/prelands_rails/update_simple_source/compile.rb
323
+ - lib/prelands_rails/update_simple_source/generate_new_aws_prefix.rb
324
+ - lib/prelands_rails/update_simple_source/remove_content_from_old_aws_prefix.rb
318
325
  - lib/prelands_rails/update_simple_source/update_record.rb
319
326
  - lib/prelands_rails/update_simple_source/upload.rb
320
327
  - lib/prelands_rails/update_simple_source/upload/delete_compiled_files.rb
@@ -343,7 +350,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
343
350
  - !ruby/object:Gem::Version
344
351
  version: '0'
345
352
  requirements: []
346
- rubygems_version: 3.1.6
353
+ rubygems_version: 3.1.4
347
354
  signing_key:
348
355
  specification_version: 4
349
356
  summary: Код, занимающийся валидацией и публикацией прелендов V4.