hotwire_native_version_gate 0.3.0 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 76b2d43b870dbf0099ed5a0c6e465a1853cc2ae96b2ab16f06570aa6397d71af
4
- data.tar.gz: '08393f721a6e48fc5193c5a34c6b4c76936a1aa923c1b04c209c0666a66e7ad7'
3
+ metadata.gz: ebc18b98c7d0f0ea4318e3b3d4fdcf49bbf8cf6b0fb2cc4eeaed8589e1fcb385
4
+ data.tar.gz: b182557f57ff23e9d27c210a81e1cf1a23150cc87b4e4ae4d16a9215f36531dc
5
5
  SHA512:
6
- metadata.gz: ea6c7456e14d5cdc4336c9ea872741087b203b01dd00b45ae36961182b589c83c260195d05b131445ba873c89b2423d31b79ff327ba2ce56ec535802734688e0
7
- data.tar.gz: a4345f8b1044668538d643242c8fc8fb67aa43291365794cf60c041ef7f1371570de93f25c4d23fb812011d477414cfefe7d119ccde86183396fb11ef5990ac7
6
+ metadata.gz: ada0778b182a271c578e9f9d6a7959e3361a4e5aa1691a156fb4dca2e851383e3a54aa193011b8be8495872f1ee5d617309f47c08f73ab9cc92ed5e742a55da4
7
+ data.tar.gz: 8b49531a19959472f9fa81ea2bea13675c811dc933a033eef56b3faea19ccb5a8aedf8dcd888906bf6c83edbfba674a5291808fd993b9524ae564bd62a7a7bbe
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -20,23 +20,23 @@ module HotwireNativeVersionGate
20
20
 
21
21
  included do
22
22
  if respond_to?(:helper_method)
23
- helper_method :native_feature_enabled?, :hotwire_native_ios?, :hotwire_native_android?
23
+ helper_method :native_feature?, :native_ios?, :native_android?
24
24
  end
25
25
  end
26
26
 
27
- def native_feature_enabled?(feature)
27
+ def native_feature?(feature)
28
28
  user_agent = respond_to?(:request) && request.respond_to?(:user_agent) ? request.user_agent : nil
29
29
  VersionGate.feature_enabled?(feature, user_agent, context: self)
30
30
  end
31
31
 
32
- def hotwire_native_ios?
32
+ def native_ios?(min_version = nil)
33
33
  user_agent = respond_to?(:request) && request.respond_to?(:user_agent) ? request.user_agent : nil
34
- VersionGate.ios?(user_agent)
34
+ VersionGate.ios?(user_agent, min_version)
35
35
  end
36
36
 
37
- def hotwire_native_android?
37
+ def native_android?(min_version = nil)
38
38
  user_agent = respond_to?(:request) && request.respond_to?(:user_agent) ? request.user_agent : nil
39
- VersionGate.android?(user_agent)
39
+ VersionGate.android?(user_agent, min_version)
40
40
  end
41
41
  end
42
42
  end
@@ -36,14 +36,12 @@ module HotwireNativeVersionGate
36
36
  handle_feature(feature_config, user_agent, context: context)
37
37
  end
38
38
 
39
- def ios?(user_agent)
40
- platform = match_platform(user_agent)
41
- platform&.downcase == 'ios'
39
+ def ios?(user_agent, min_version = nil)
40
+ platform_check?('ios', user_agent, min_version)
42
41
  end
43
42
 
44
- def android?(user_agent)
45
- platform = match_platform(user_agent)
46
- platform&.downcase == 'android'
43
+ def android?(user_agent, min_version = nil)
44
+ platform_check?('android', user_agent, min_version)
47
45
  end
48
46
 
49
47
  private
@@ -69,29 +67,34 @@ module HotwireNativeVersionGate
69
67
  nil
70
68
  end
71
69
 
70
+ def platform_check?(platform, user_agent, min_version = nil)
71
+ return false unless match_platform(user_agent)&.downcase == platform
72
+ return true unless min_version
73
+ compare_version(user_agent, min_version)
74
+ end
75
+
72
76
  def match_platform(user_agent)
73
77
  match_key(user_agent, :platform)
74
78
  end
75
79
 
80
+ def compare_version(user_agent, min_version)
81
+ version = match_key(user_agent, :version)
82
+ return false unless version
83
+
84
+ Gem::Version.new(version) >= Gem::Version.new(min_version)
85
+ end
86
+
76
87
  def handle_feature(feature_config, user_agent, context: nil)
77
88
  # if false or nil, return false
78
89
  return false unless feature_config
79
90
  # if true, return true
80
91
  return true if feature_config == true
81
92
  # if a string, compare the version
82
- if feature_config.is_a?(String)
83
- version = match_key(user_agent, :version)
84
- return false unless version
85
- return Gem::Version.new(feature_config) <= Gem::Version.new(version)
86
- end
93
+ return compare_version(user_agent, feature_config) if feature_config.is_a?(String)
87
94
  # if a symbol, call the method on the context (if provided) or self
88
- if feature_config.is_a?(Symbol)
89
- if context
90
- return context.send(feature_config)
91
- else
92
- return send(feature_config)
93
- end
94
- end
95
+ return context.send(feature_config) if feature_config.is_a?(Symbol) && context
96
+ return send(feature_config) if feature_config.is_a?(Symbol)
97
+
95
98
  # else, raise an error
96
99
  raise InvalidVersionGateError, "Invalid version gate: #{feature_config}"
97
100
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hotwire_native_version_gate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stuart Yamartino