Created
May 24, 2014 13:00
-
-
Save ayumu83s/9539a777ca45f77e1cc7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Environment; | |
use strict; | |
use warnings; | |
use utf8; | |
use 5.010_001; | |
use Log::Minimal; | |
use Mouse; | |
has ua => ( | |
is => 'ro', | |
required => 1, | |
); | |
has client_os => ( | |
is => 'ro', | |
required => 1, | |
lazy => 1, | |
builder => '_build_client_os', | |
); | |
has client_browser => ( | |
is => 'ro', | |
required => 1, | |
lazy => 1, | |
builder => '_build_client_browser', | |
); | |
no Mouse; | |
# ユーザーのOS | |
# 対象外は情報を入れない | |
sub _build_client_os { | |
my ($self) = @_; | |
my $user_agent = $self->ua; | |
my $os_info = +{ | |
code => undef, | |
version => undef, | |
}; | |
my $version; | |
my $major; | |
my $minor; | |
if ($user_agent =~ qr/(?:iPhone|Darwin|iPad)/) { | |
$os_info->{code} = CLIENT_OS_IOS; | |
if ($user_agent =~ qr/OS\s(.*?)\slike Mac OS X/) { | |
my @versions = split /_/, $1; | |
$major = $versions[0]; | |
$minor = $versions[1]; | |
# iOS 4.3以降 | |
if ( $major >= 5 || | |
($major >= 4 && $minor >= 3)) { | |
$os_info->{version} = join('.', @versions); | |
} | |
} | |
} | |
if ($user_agent =~ qr/(?:Macintosh)/) { | |
$os_info->{code} = CLIENT_OS_OSX; | |
if ($user_agent =~ qr/Mac OS X\s(.*?)[;|\)]\s/) { | |
$version = $1; | |
my @versions; # OSバージョンが.区切りのと_区切りのがある・・・ | |
@versions = split /\./, $version if ($version =~ qr/\./); | |
@versions = split /_/, $version if ($version =~ qr/_/); | |
$major = $versions[0]; | |
$minor = $versions[1]; | |
# OS X 10.6 〜 10.8まで対象 | |
if ($major >= 10 && ($minor >= 6 && $minor <= 8)) { | |
$os_info->{version} = join('.', @versions); | |
} | |
} | |
} | |
if ($user_agent =~ qr/(?:Windows)/) { | |
$os_info->{code} = CLIENT_OS_WINDOWS; | |
if ($user_agent =~ qr/(?:NT\s*5\.[12])/) { | |
$os_info->{version} = 'XP'; | |
} | |
if ($user_agent =~ qr/(?:NT\s*6\.1)/) { | |
$os_info->{version} = '7'; | |
} | |
if ($user_agent =~ qr/(?:NT\s*6\.2)/) { | |
$os_info->{version} = '8'; | |
} | |
} | |
if ($user_agent =~ qr/(?:Android)/) { | |
$os_info->{code} = CLIENT_OS_ANDROID; | |
if ($user_agent =~ qr/\sAndroid\s(.*?);/) { | |
my @versions = split /\./, $1; | |
$major = $versions[0]; | |
$minor = $versions[1]; | |
# android 2.3以降 | |
if ( $major >= 3 || | |
($major >= 2 && $minor >= 3)) { | |
$os_info->{version} = join('.', @versions); | |
} | |
} | |
} | |
$os_info; | |
} | |
# ユーザーのブラウザ | |
sub _build_client_browser { | |
my ($self) = @_; | |
my $user_agent = $self->ua; | |
my $browser = undef; | |
if ($user_agent =~ qr/(?!.*Chrome.*)Safari/) { | |
$browser = 'safari'; | |
} | |
if ($user_agent =~ qr/(?:Chrome)/) { | |
$browser = 'chrome'; | |
} | |
if ($user_agent =~ qr/(?:Firefox)/) { | |
$browser = 'firefox'; | |
} | |
if ($user_agent =~ qr/(?:MSIE 10)/) { | |
$browser = 'ie10'; | |
} | |
#if ($user_agent =~ qr/(?:Trident)/) { | |
# $browser = 'ie11'; | |
#} | |
$browser; | |
} | |
# タブレットかどうか | |
sub is_tablet { | |
my ($self) = @_; | |
my $user_agent = $self->ua; | |
# Androidは、基本的にMobileがついてないものがtablet | |
# http://googlewebmastercentral-ja.blogspot.jp/2012/11/giving-tablet-users-full-sized-web.html | |
my $is_tablet = ($user_agent =~ m/Android.*SC-01C.*Mobile/ ) ? 1 # タブレット(特殊) | |
: ($user_agent =~ m/Android.*N-06D.*Mobile/ ) ? 1 # タブレット(特殊) | |
: ($user_agent =~ m/Android.*A1_07.*Mobile/ ) ? 1 # タブレット(特殊) | |
: ($user_agent =~ m/iPad|Android(?!.*Mobile)/) ? 1 # タブレット | |
: 0 # タブレットではない | |
; | |
return $is_tablet; | |
} | |
# デバイス | |
sub user_device { | |
my ($self) = @_; | |
if ($self->is_tablet) { | |
return DEVICE_TABLET; | |
} | |
if ($self->client_os->{code} == CLIENT_OS_IOS || | |
$self->client_os->{code} == CLIENT_OS_ANDROID) { | |
return DEVICE_SMARTPHONE; | |
} | |
if ($self->client_os->{code} == CLIENT_OS_OSX || | |
$self->client_os->{code} == CLIENT_OS_WINDOWS) { | |
return DEVICE_PC; | |
} | |
} | |
=pod | |
my $env = Environment->new(''); | |
=cut | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment