tp_common 0.4.2 → 0.4.3.pre1

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
  SHA256:
3
- metadata.gz: 3c82f0ce642dc1cd99e759a8afa0fd7ef2ab84dfb63ee3e4907c27f28130ecf0
4
- data.tar.gz: 2af83acd05b645bc50f37808f6d8dbcb046da87f211b55bbec2c3bd4d69be9f3
3
+ metadata.gz: 025ffd9b6a300dba51d4e8edbae4a778d300835fa674ec52bcbca3a819d072c9
4
+ data.tar.gz: 651838404deaf1632f71078813c04b5e0f4486946471be743b9dfdf277669009
5
5
  SHA512:
6
- metadata.gz: e79880322a5417851ad894fb64e44fe64a65de46da4d89253d6be0ba092fead386cbed9655ada94bcd246decfe456bea4ad78f9d78e007ff52ccceb8d9390391
7
- data.tar.gz: 502054409c6606ced1ce284b798326451642505154bb329eb42f573682eb66a2d08036a0633e8c9825794e0154cba734046fc9f68c7064b582f9f3feff4f4b6d
6
+ metadata.gz: d0c929022ae62ef0e961548e79572e64fe1e9a6c01161b14b262e6316e1919b9f24653bb18cf4a10f2461f74ea1ad9550270833306909216afcf6c76ee170039
7
+ data.tar.gz: 752c51e003424ab2ca708bd617fd39c1d57987e19d71733e63046e54e5dc3a3cb3b3b56401da7e68602f3cc2d9369534310bd895ed7bfbf81779c18d5d19263c
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- tp_common (0.4.2)
4
+ tp_common (0.4.3.pre1)
5
5
  activesupport (>= 4.2.0, <= 5.2.1)
6
6
  aws-sdk-s3 (~> 1.19)
7
7
  fog-aws (~> 2.0)
@@ -51,8 +51,8 @@ GEM
51
51
  thor (>= 0.14.0)
52
52
  arel (6.0.4)
53
53
  aws-eventstream (1.0.1)
54
- aws-partitions (1.136.0)
55
- aws-sdk-core (3.46.0)
54
+ aws-partitions (1.140.0)
55
+ aws-sdk-core (3.46.2)
56
56
  aws-eventstream (~> 1.0)
57
57
  aws-partitions (~> 1.0)
58
58
  aws-sigv4 (~> 1.0)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- tp_common (0.4.2)
4
+ tp_common (0.4.3.pre1)
5
5
  activesupport (>= 4.2.0, <= 5.2.1)
6
6
  aws-sdk-s3 (~> 1.19)
7
7
  fog-aws (~> 2.0)
@@ -54,8 +54,8 @@ GEM
54
54
  thor (>= 0.14.0)
55
55
  arel (7.1.4)
56
56
  aws-eventstream (1.0.1)
57
- aws-partitions (1.136.0)
58
- aws-sdk-core (3.46.0)
57
+ aws-partitions (1.140.0)
58
+ aws-sdk-core (3.46.2)
59
59
  aws-eventstream (~> 1.0)
60
60
  aws-partitions (~> 1.0)
61
61
  aws-sigv4 (~> 1.0)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- tp_common (0.4.2)
4
+ tp_common (0.4.3.pre1)
5
5
  activesupport (>= 4.2.0, <= 5.2.1)
6
6
  aws-sdk-s3 (~> 1.19)
7
7
  fog-aws (~> 2.0)
@@ -58,8 +58,8 @@ GEM
58
58
  thor (>= 0.14.0)
59
59
  arel (9.0.0)
60
60
  aws-eventstream (1.0.1)
61
- aws-partitions (1.136.0)
62
- aws-sdk-core (3.46.0)
61
+ aws-partitions (1.140.0)
62
+ aws-sdk-core (3.46.2)
63
63
  aws-eventstream (~> 1.0)
64
64
  aws-partitions (~> 1.0)
65
65
  aws-sigv4 (~> 1.0)
@@ -17,5 +17,6 @@ end
17
17
  require "tp_common/timezones"
18
18
  require "tp_common/timezones/zone"
19
19
  require "tp_common/assets_loader"
20
+ require "tp_common/asset_loaders/remote_assets_loader"
20
21
  require "tp_common/file_storage"
21
22
  require "tp_common/app_config"
@@ -0,0 +1,8 @@
1
+ module TpCommon
2
+ module AssetLoaders
3
+ module Errors
4
+ class AssetNotFound < ::StandardError; end
5
+ class PackageIsNotLoaded < ::StandardError; end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,38 @@
1
+ require 'net/http'
2
+
3
+ module TpCommon
4
+ module AssetLoaders
5
+ module PackagePathProviders
6
+ class Development < Production
7
+ DEVELOPMENT_CDN = 'http://localhost:3001'
8
+
9
+ def asset_url(package_name, version, asset)
10
+ @development ||= head("#{DEVELOPMENT_CDN}/_ping")
11
+ if @development
12
+ return "#{cdn(package_name, version)}/#{asset}"
13
+ end
14
+
15
+ super(package_name, version, asset)
16
+ end
17
+
18
+ private
19
+
20
+ def head(url_string)
21
+ url = URI.parse(url_string)
22
+ req = Net::HTTP.new(url.host, url.port)
23
+ req.use_ssl = (url.scheme == 'https')
24
+
25
+ path = url.path unless url.path.nil?
26
+
27
+ res = begin
28
+ req.request_head(path || '/')
29
+ rescue StandardError
30
+ nil
31
+ end
32
+
33
+ res.is_a?(Net::HTTPSuccess) || res.is_a?(Net::HTTPRedirection)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,15 @@
1
+ module TpCommon
2
+ module AssetLoaders
3
+ module PackagePathProviders
4
+ class Production
5
+ def initialize(default_cdn)
6
+ @default_cdn = default_cdn
7
+ end
8
+
9
+ def asset_url(package_name, version, asset)
10
+ "#{cdn(package_name, version)}/#{asset.to_s.split('.').insert(-2, 'min').join('.')}"
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,115 @@
1
+ require 'tp_common/asset_loaders/errors'
2
+ require 'tp_common/asset_loaders/package_path_providers/production'
3
+ TpCommon::AssetLoaders::ProviderClass = if defined?(Rails) && (Rails.env.development? || Rails.env.test?)
4
+ require 'tp_common/asset_loaders/package_path_providers/development'
5
+ TpCommon::AssetLoaders::PackagePathProviders::Development
6
+ else
7
+ TpCommon::AssetLoaders::PackagePathProviders::Production
8
+ end
9
+
10
+ module TpCommon
11
+ module AssetLoaders
12
+ # This class supports switching between development env and production env in loading assets
13
+ # Follow TINYpulse development convention.
14
+ # - Use minified assets on cdn for production
15
+ # - On development, prefer development spa on local, if not, fallback to production behaviors
16
+ #
17
+ # To config:
18
+ # ```
19
+ # TpCommon::AssetLoaders::RemoteAssetsLoader.configure do |config|
20
+ # config.cdn = 'https://other.hotter.cdn'
21
+ # end
22
+ # # Load a package. This is required.
23
+ # TpCommon::AssetLoaders::RemoteAssetsLoader.load(:'any-spa', 'v1.0.0')
24
+ # ```
25
+ #
26
+ # Use in template:
27
+ # ```
28
+ # <%= javascript_include_tag TpCommon::AssetLoaders::RemoteAssetsLoader[:'any-spa']['main.js'] %>
29
+ # ```
30
+ class RemoteAssetsLoader
31
+ DEFAULT_CDN = 'https://cdn.tinypulse.com/spa'
32
+ LoaderConfiguration = Struct.new(:cdn, :package_path_provider)
33
+
34
+ class << self
35
+ # Configure RemoteAssetsLoader, for now, only :cdn is supported
36
+ # ```
37
+ # TpCommon::AssetLoaders::RemoteAssetsLoader.configure do |config|
38
+ # config.cdn = 'https://other.hotter.cdn'
39
+ # end
40
+ # ```
41
+ def configure
42
+ yield(config)
43
+
44
+ config.package_path_provider = TpCommon::AssetLoaders::ProviderClass.new(config.cdn)
45
+ end
46
+
47
+ # Load a package (SPA) with specific version to use.
48
+ # ```
49
+ # TpCommon::AssetLoaders::RemoteAssetsLoader.load(:'tinypulse-spa-1', 'v1.0.0')
50
+ # ```
51
+ #
52
+ # To prioritize version from env variable, we dont provide this magic here
53
+ # So declare it explicitly when load:
54
+ # ```
55
+ # TpCommon::AssetLoaders::RemoteAssetsLoader.load(:'tinypulse-spa-1', ENV['SPA_VERSION_TINYPULSE_SPA_1_HOT_CHANGE'] || 'v1.0.0')
56
+ # ```
57
+ def load(package_name, version)
58
+ package_name = package_name.to_sym
59
+ packages[package_name] = new(package_name, version)
60
+ packages
61
+ end
62
+
63
+ # Get package:
64
+ # ```
65
+ # TpCommon::AssetLoaders::RemoteAssetsLoader[:'tinypulse-spa-1'][:'main.js'] # 'https://some.cdn/spa/tinypulse-spa-1/v1.0.0/main.js'
66
+ # ```
67
+ # Raise `PackageIsNotLoaded` exception if package is not found.
68
+ #
69
+ # - In Rails development environment, Asset loader will try to ping and access assets at localhost:3001
70
+ # Which assets are served without minified, so file name is keep the same.
71
+ # - In other environments, assets are minified and served through cdn
72
+ # File name will be append `.min` before extension. i.e. 'main.min.js'
73
+ #
74
+ def [](package_name)
75
+ packages[package_name].tap do |package|
76
+ raise TpCommon::AssetLoaders::Errors::PackageIsNotLoaded.new("Package #{package_name} is not loaded yet.") if package.nil?
77
+ end
78
+ end
79
+
80
+ def asset_url(package_name, version, asset)
81
+ config.package_path_provider.asset_url(package_name, version, asset)
82
+ end
83
+
84
+ private
85
+
86
+ def new_config
87
+ LoaderConfiguration.new(DEFAULT_CDN, nil)
88
+ end
89
+
90
+ def config
91
+ @config ||= new_config
92
+ end
93
+
94
+ def packages
95
+ @packages ||= {}
96
+ end
97
+ end
98
+
99
+ def initialize(package_name, version)
100
+ @package_name = package_name
101
+ @version = version
102
+
103
+ @cached_assets = Hash.new { |hash, key| hash[key] = RemoteAssetsLoader.asset_url(@package_name, @version, key.to_s) }
104
+ end
105
+
106
+ def [](asset_name)
107
+ # TODO: [AV] Do performance test and cache the string in system level
108
+ # NOTE: [AV] It's secure to check this asset url exists by Net::HTTP then try more than 1 cdn.
109
+ # but DONT DO IT. Using unavailable spa version/cdn is a SERIOUS problem need to solve, not auto-recovery
110
+ # beside, this url will be called every page hit, so any milisecond are counted.
111
+ @cached_assets[asset_name]
112
+ end
113
+ end
114
+ end
115
+ end
@@ -1,3 +1,3 @@
1
1
  module TpCommon
2
- VERSION = "0.4.2"
2
+ VERSION = "0.4.3.pre1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tp_common
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3.pre1
5
5
  platform: ruby
6
6
  authors:
7
7
  - TINYpulse Devops
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-01 00:00:00.000000000 Z
11
+ date: 2019-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -212,6 +212,10 @@ files:
212
212
  - lib/tp_common.rb
213
213
  - lib/tp_common/app_config.rb
214
214
  - lib/tp_common/app_config/environment.rb
215
+ - lib/tp_common/asset_loaders/errors.rb
216
+ - lib/tp_common/asset_loaders/package_path_providers/development.rb
217
+ - lib/tp_common/asset_loaders/package_path_providers/production.rb
218
+ - lib/tp_common/asset_loaders/remote_assets_loader.rb
215
219
  - lib/tp_common/assets_loader.rb
216
220
  - lib/tp_common/file_storage.rb
217
221
  - lib/tp_common/file_storage/base.rb
@@ -247,9 +251,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
247
251
  version: '0'
248
252
  required_rubygems_version: !ruby/object:Gem::Requirement
249
253
  requirements:
250
- - - ">="
254
+ - - ">"
251
255
  - !ruby/object:Gem::Version
252
- version: '0'
256
+ version: 1.3.1
253
257
  requirements: []
254
258
  rubygems_version: 3.0.2
255
259
  signing_key: