GithubHelp home page GithubHelp logo

yasuoy / rack-user_agent Goto Github PK

View Code? Open in Web Editor NEW

This project forked from k0kubun/rack-user_agent

0.0 1.0 0.0 62 KB

Rack::Request extension for handling User-Agent

License: MIT License

Ruby 83.67% JavaScript 1.72% CSS 1.77% HTML 12.84%

rack-user_agent's Introduction

Rack::UserAgent Build Status

Rack::Request extension for handling User-Agent.
Thanks to woothee, Rack::UserAgent supports various User-Agents.

Installation

Add this line to your application's Gemfile:

gem 'rack-user_agent'

Usage

Rails

If you bundle "rack-user_agent", Rack::UserAgent will be automatically inserted when started.
Additional methods for request will be available with no cost.

class ApplicationController < ActionController::Base
  before_action :set_request_variant

  def index
    request.user_agent       #=> "Mozilla/5.0 (Macintosh; ..."
    request.device_type      #=> :pc
    request.os               #=> "Mac OSX"
    request.browser          #=> "Chrome"
    request.from_pc?         #=> true
    request.from_smartphone? #=> false
  end

  private

  # It is useful for Action Pack variants, which is new feature from Rails 4.1.
  # You can switch view templates by +pc or +smartphone in file name.
  # http://guides.rubyonrails.org/4_1_release_notes.html#action-pack-variants
  def set_request_variant
    request.variant = request.device_variant # :pc, :smartphone
  end
end

Sinatra

You can also manually use Rack::UserAgent for any Rack apps.

require "sinatra"
require "rack/user_agent"

configure do
  use Rack::UserAgent
end

get "/" do
  request.user_agent       #=> "Mozilla/5.0 (Linux; Android 4.3; Nexus 7 ..."
  request.device_type      #=> :smartphone
  request.os               #=> "Android"
  request.os_version       #=> "4.3"
  request.browser          #=> "Chrome"
  request.browser_version  #=> "29.0.1547.72"
  request.from_smartphone? #=> true
end

Available Methods

Attributes

  • request.device_type
  • request.os
  • request.os_version
  • request.browser
  • request.browser_version
  • request.browser_vendor

Helpers

  • request.from_pc?
  • request.from_smartphone?
  • request.from_mobilephone?
  • request.from_appliance?
  • request.from_crawler?

Detectors

APIs compatible with rack-smartphone_detector

  • request.smartphone_version
  • request.from_iphone?
  • request.from_ipad?
  • request.from_android?
  • request.from_android_tablet?
  • request.from_windows_phone?

License

MIT License

rack-user_agent's People

Contributors

k0kubun avatar amatsuda avatar osdakira avatar yasuoy avatar knjko avatar matsuhisa avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.