How to Detect User Agent in Rails 3.1

Is there a way to detect user agent in Rails 3.1

Yep, just use this

request.env["HTTP_USER_AGENT"]
#or
request.user_agent

Detect user agent in Rails 4 - read HTTP header

Check the request method, where you can get a ActionDispatch::Request where you have all the request parameters, including the user agent.

request.user_agent

how to autodetect the user agent in rails

can you post a copy of your config/environments/production.rb file?

Side-Note:
You probably want to add something like this if you don't want to break xml or json output, (with responds_to blocks):

def prepare_for_mobile
if request.format == 'text/html'
session[:mobile_param] = params[:mobile] if params[:mobile]
request.format = :mobile if mobile_device?
end
end

Ruby on Rails, How to determine if a request was made by a robot or search engine spider?

Robots are required (by common sense / courtesy more than any kind of law) to send along a User-Agent with their request. You can check for this using request.env["HTTP_USER_AGENT"] and filter as you please.

Is it possible to specify a user agent in a rails integration test or spec?

If you use request.user_agent in your application, you can write the following code:

get '/', {}, { "HTTP_USER_AGENT" => "Googlebot" }

how to set user agent in rspec route assertions

I'm not sure this will work, but here's something to try:

If you look at the source for the RouteToMatcher, it just parses a controller/action pair (given as a hash or in controller#action format) and any other parameters given along with them and then delegates to ActionDispatch::Assertions::RoutingAssertions#assert_recognizes. But that method builds the request object from the parameters you pass in as the argument to route_to. So there's no easy way to access the request object from your spec. However, to build this dummy request,
it calls

ActionController::TestRequest.new

So you might try

ActionController::TestRequest.any_instance.stub(:user_agent).
and_return "Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_3 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10B329 Safari/8536.25"

Which is a better way to detect a client's user-agent?

The short and correct answer is : do not use anything that relies on UserAgent sniffing.

To reliable be able to adjust code paths you should test for the specific 'thing' that the codepath is adjusted for, primarily features. This is called Feature Detection.

So if feature X is supported we do this, if not we do that.

Deducing if a feature is supported based on which UserAgent is present will rapidly fail, especially when new browsers come to the marked.

Take the following example, which can actually be found in several major libraries (!)

if (isIE8) {
// use new feature provided by IE8
} else if (isIE7) {
// use not so new feature provided by IE7 (and IE8)
} else {
// use fallback for all others (which also works in IE7 and IE8)
}

What do you think happens when IE9 comes along?

The correct pattern in this case would be

if ("addEventListener" in foo) {
// use DOM level 2 addEventListener to attach events
foo.addEventListener(...
} else if ("attachEvent" in foo) {
// use IE's proprietary attachEvent method
foo.attachEvent(...
} else {
// fall back to DOM 0
foo["on" + eventName] = ....
}

How to recognize a bot in rails

You can check HTTP headers, particularly the user agent string.

http://www.useragentstring.com/pages/Googlebot/

Most friendly bots have "bot" in their user agent.

Another suggestion is to use something like Google Analytics to track your visits. It's way better than implementing your own.



Related Topics



Leave a reply



Submit