Module: Sinatra::SinatraMobileFu

Defined in:
lib/sinatra/sinatra_mobile_fu.rb

Constant Summary collapse

VERSION =
'0.0.7'
MOBILE_USER_AGENTS =

These are various strings that can be found in mobile devices. Please feel free to add on to this list.

'palm|palmos|palmsource|iphone|blackberry|nokia|phone|midp|mobi|pda|' +
'wap|java|nokia|hand|symbian|chtml|wml|ericsson|lg|audiovox|motorola|' +
'samsung|sanyo|sharp|telit|tsm|mobile|mini|windows ce|smartphone|' +
'240x320|320x320|mobileexplorer|j2me|sgh|portable|sprint|vodafone|' +
'docomo|kddi|softbank|pdxgw|j-phone|astel|minimo|plucker|netfront|' +
'xiino|mot-v|mot-e|portalmmm|sagem|sie-s|sie-m|android|ipod'

Instance Method Summary collapse

Instance Method Details

#force_mobile_formatObject

Forces the request format to be :mobile



52
53
54
55
# File 'lib/sinatra/sinatra_mobile_fu.rb', line 52

def force_mobile_format
  session[:format] = :mobile
  session[:mobile_view] = true if session[:mobile_view].nil?
end

#has_sinatra_mobile_fu(test_mode = false) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sinatra/sinatra_mobile_fu.rb', line 38

def has_sinatra_mobile_fu(test_mode = false)

  before {
    if test_mode 
      force_mobile_format
    else
      set_mobile_format
    end
  }

end

#in_mobile_view?Boolean

Returns either true or false depending on whether or not the format of the request is either :mobile or not.

Returns:

  • (Boolean)


72
73
74
# File 'lib/sinatra/sinatra_mobile_fu.rb', line 72

def in_mobile_view?
  session[:format].to_sym == :mobile
end

#is_device?(type) ⇒ Boolean

Can check for a specific user agent e.g., is_device?(‘iphone’) or is_device?(‘mobileexplorer’)

Returns:

  • (Boolean)


86
87
88
# File 'lib/sinatra/sinatra_mobile_fu.rb', line 86

def is_device?(type)
  request.user_agent.to_s.downcase.include?(type.to_s.downcase)
end

#is_mobile_device?Boolean

Returns either true or false depending on whether or not the user agent of the device making the request is matched to a device in our regex.

Returns:

  • (Boolean)


79
80
81
# File 'lib/sinatra/sinatra_mobile_fu.rb', line 79

def is_mobile_device?
  request.user_agent.to_s.downcase =~ Regexp.new(Sinatra::SinatraMobileFu::MOBILE_USER_AGENTS)
end

#set_mobile_formatObject

Determines the request format based on whether the device is mobile or if the user has opted to use either the ‘Standard’ view or ‘Mobile’ view.



60
61
62
63
64
65
66
67
# File 'lib/sinatra/sinatra_mobile_fu.rb', line 60

def set_mobile_format
  if is_mobile_device?
    session[:format] = session[:mobile_view] == false ? :html : :mobile
    session[:mobile_view] = true if session[:mobile_view].nil?
  else
    session[:format] = :html
  end
end