hotwire_native_version_gate 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0a28510bb2899f691c79be862996ac02742b107db5bc52d22ee719a7a20f10fe
4
+ data.tar.gz: 55475a89e88294e1e26432c8cdc16cb0db8c9eba448e5359c8218d837f23ed75
5
+ SHA512:
6
+ metadata.gz: 7ca0c3871d000b4d5f42c3ebcfa77b888b036d5159739be890f9c98a2b725d2a67883d49e8379def5708db5916453191f120082be6231484c6d69619685d0cf3
7
+ data.tar.gz: 88a94760a3c596121ca665951f20444a69e4e66791f29414abeeaa41fc94de848aea8e6e0c4c5a4ef660c6299598e61559e96a5e69c328567a2c542c95399409
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HotwireNativeVersionGate
4
+ module Concern
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do
8
+ def native_feature(feature, ios: false, android: false)
9
+ VersionGate.native_feature(feature, ios: ios, android: android)
10
+ end
11
+
12
+ def native_version_regex=(regex)
13
+ VersionGate.native_version_regex = regex
14
+ end
15
+ end
16
+
17
+ included do
18
+ if respond_to?(:helper_method)
19
+ helper_method :native_feature_enabled?
20
+ end
21
+ end
22
+
23
+ def native_feature_enabled?(feature)
24
+ user_agent = if respond_to?(:request) && request.respond_to?(:user_agent)
25
+ request.user_agent
26
+ else
27
+ nil
28
+ end
29
+
30
+ VersionGate.feature_enabled?(feature, user_agent, context: self)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HotwireNativeVersionGate
4
+ VERSION = File.read(
5
+ File.expand_path('../../VERSION', __dir__)
6
+ ).strip.freeze
7
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HotwireNativeVersionGate
4
+ class VersionGate
5
+ # Default regex example: Hotwire Native App iOS/1.0.0;
6
+ # Expected capture groups: platform = (iOS|Android), version = semantic version
7
+ DEFAULT_NATIVE_VERSION_REGEX = /\bHotwire Native App (?<platform>iOS|Android)\/(?<version>\d+(?:\.\d+)*)\b/
8
+
9
+ @native_features = {}
10
+ @native_version_regex = DEFAULT_NATIVE_VERSION_REGEX
11
+
12
+ class << self
13
+ attr_reader :native_features, :native_version_regex
14
+
15
+ def native_version_regex=(regex)
16
+ unless regex.is_a?(Regexp)
17
+ raise ArgumentError, "native_version_regex must be a Regexp"
18
+ end
19
+ @native_version_regex = regex
20
+ end
21
+
22
+ def native_feature(feature, ios: false, android: false)
23
+ @native_features ||= {}
24
+ @native_features[feature] = { ios: ios, android: android }
25
+ end
26
+
27
+ def feature_enabled?(feature, user_agent, context: nil)
28
+ @native_features ||= {}
29
+ return false unless @native_features.key?(feature)
30
+
31
+ platform = match_platform(user_agent)
32
+ return false if platform.nil?
33
+
34
+ platform_key = platform.downcase.to_sym
35
+ feature_config = @native_features[feature][platform_key]
36
+ handle_feature(feature_config, user_agent, context: context)
37
+ end
38
+
39
+ private
40
+
41
+ def match_platform(user_agent)
42
+ match = user_agent.to_s.match(@native_version_regex)
43
+ return match[:platform] if match
44
+ nil
45
+ end
46
+
47
+ def handle_feature(feature_config, user_agent, context: nil)
48
+ # if false or nil, return false
49
+ return false unless feature_config
50
+ # if true, return true
51
+ return true if feature_config == true
52
+ # if a string, compare the version
53
+ if feature_config.is_a?(String)
54
+ match = user_agent.to_s.match(@native_version_regex)
55
+ return false unless match
56
+ return Gem::Version.new(feature_config) <= Gem::Version.new(match[:version])
57
+ end
58
+ # if a symbol, call the method on the context (if provided) or self
59
+ if feature_config.is_a?(Symbol)
60
+ if context
61
+ return context.send(feature_config)
62
+ else
63
+ return send(feature_config)
64
+ end
65
+ end
66
+ # else, raise an error
67
+ raise InvalidVersionGateError, "Invalid version gate: #{feature_config}"
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+ require_relative "hotwire_native_version_gate/version"
5
+ require_relative "hotwire_native_version_gate/version_gate"
6
+ require_relative "hotwire_native_version_gate/concern"
7
+
8
+ module HotwireNativeVersionGate
9
+ class Error < StandardError; end
10
+ class InvalidVersionGateError < StandardError; end
11
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hotwire_native_version_gate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Stuart Yamartino
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: activesupport
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: 6.0.0
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: 6.0.0
26
+ description: HotwireNativeVersionGate provides functionality to version gate Hotwire
27
+ Native apps in Rails.
28
+ email:
29
+ - stu@stuyam.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - VERSION
35
+ - lib/hotwire_native_version_gate.rb
36
+ - lib/hotwire_native_version_gate/concern.rb
37
+ - lib/hotwire_native_version_gate/version.rb
38
+ - lib/hotwire_native_version_gate/version_gate.rb
39
+ homepage: https://github.com/stuyam/hotwire_native_version_gate
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ homepage_uri: https://github.com/stuyam/hotwire_native_version_gate
44
+ source_code_uri: https://github.com/stuyam/hotwire_native_version_gate
45
+ changelog_uri: https://github.com/stuyam/hotwire_native_version_gate/blob/main/CHANGELOG.md
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.7.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.6.9
61
+ specification_version: 4
62
+ summary: A gem for version gating Hotwire Native apps in Rails
63
+ test_files: []