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