Skip to content

Instantly share code, notes, and snippets.

@peppyheppy
Created October 31, 2014 18:38
Show Gist options
  • Save peppyheppy/ef0d76fd27a113e9b137 to your computer and use it in GitHub Desktop.
Save peppyheppy/ef0d76fd27a113e9b137 to your computer and use it in GitHub Desktop.
class UserAgentConfig
def initiatize(user_agent_string)
@ua_string_to_cache = UserAgent.parse(user_agent_string)
end
def ua
@ua_string_to_cache
end
def self.ua=(user_agent_string)
Thread.current[:ua_instance] = UserAgentConfig/self.new(user_agent_string)
rescue
Thread.current[:ua_instance] = nil
end
def self.ua
Thread.current[:ua_instance].ua
rescue
nil
end
end
class ApplicationController < Blah
before_filter :set_user_agent_thingy
def show
# example
if UserAgentConfig.ua == 'ie-whatever'
exit
end
# do wahtever else
end
private
def set_user_agent_thingy
UserAgentConfig.ua = request.user_agent_method
end
end
class DoStuffInContextOfUA
include SidekiqJazz
def perform(session_id, blah, user_agent_string)
UserAgentConfig.ua = user_agent_string
# do other stuff within the context of
ClassThatUsesStuffThatNeedsUA.new(UserAgentConfig.ua)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment