Module: RubyNative::NativeDetection

Extended by:
ActiveSupport::Concern
Defined in:
lib/ruby_native/native_detection.rb

Constant Summary collapse

SIGNATURE =

Matches "Ruby Native iOS/5.2/35 iOS/26.5.2 RubyNative/0.12.5". The build and OS groups are optional: apps built before the UA carried them send only "Ruby Native iOS/5.2", and the optional group keeps the OS capture from swallowing that app version.

%r{Ruby Native (?:iOS|Android)/([\d.]+)(?:/([\d.]+) (?:iOS|Android)/([\d.]+))?}

Instance Method Summary collapse

Instance Method Details

#native_app?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/ruby_native/native_detection.rb', line 18

def native_app?
  request.user_agent.to_s.include?("Ruby Native")
end

#native_app_buildObject

The app's build number (CFBundleVersion / versionCode), like "35". Nil for web browsers and apps built before the User-Agent carried it.



43
44
45
# File 'lib/ruby_native/native_detection.rb', line 43

def native_app_build
  request.user_agent.to_s[SIGNATURE, 2]
end

#native_app_versionObject

The app's marketing version, like "5.2". Nil for web browsers.



37
38
39
# File 'lib/ruby_native/native_detection.rb', line 37

def native_app_version
  request.user_agent.to_s[SIGNATURE, 1]
end

#native_os_versionObject

The device's OS version, like "26.5.2". Nil for web browsers and apps built before the User-Agent carried it.



49
50
51
# File 'lib/ruby_native/native_detection.rb', line 49

def native_os_version
  request.user_agent.to_s[SIGNATURE, 3]
end

#native_platformObject

Returns "ios" or "android" for native requests, nil for web browsers. Used by view helpers to pick the right icon from icons: { ios:, android: }.



29
30
31
32
33
34
# File 'lib/ruby_native/native_detection.rb', line 29

def native_platform
  ua = request.user_agent.to_s
  return "ios" if ua.include?("Ruby Native iOS")
  return "android" if ua.include?("Ruby Native Android")
  nil
end

#native_versionObject



22
23
24
25
# File 'lib/ruby_native/native_detection.rb', line 22

def native_version
  match = request.user_agent.to_s.match(/RubyNative\/([\d.]+)/)
  NativeVersion.new(match ? match[1] : "0")
end