-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from rejeep/user-agent-parser-bin
Add bin/user_agent_parser script.
- Loading branch information
Showing
4 changed files
with
212 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env ruby | ||
|
||
$: << File.expand_path('../../lib', File.readlink(__FILE__)) | ||
|
||
require 'optparse' | ||
|
||
require 'user_agent_parser' | ||
require 'user_agent_parser/cli' | ||
|
||
options = {} | ||
|
||
optparse = OptionParser.new do|opts| | ||
opts.on('--name', 'Print name only') do | ||
options[:name] = true | ||
end | ||
|
||
opts.on('--version', 'Print version only') do | ||
options[:version] = true | ||
end | ||
|
||
opts.on('--major', 'Print major version only') do | ||
options[:major] = true | ||
end | ||
|
||
opts.on('--minor', 'Print minor version only') do | ||
options[:minor] = true | ||
end | ||
|
||
opts.on('--os', 'Print operating system only') do | ||
options[:os] = true | ||
end | ||
|
||
opts.on('--format format', | ||
'Print output in specified format. The available formatters are:', | ||
' - %n: name', | ||
' - %v: version', | ||
' - %M: major version', | ||
' - %m: minor version', | ||
' - %o: operating system' | ||
) do |format| | ||
options[:format] = format | ||
end | ||
|
||
opts.on('-h', '--help', 'Display this screen') do | ||
puts opts | ||
exit | ||
end | ||
end | ||
|
||
optparse.parse! | ||
|
||
parser = UserAgentParser::Parser.new | ||
|
||
ARGF.each do |line| | ||
puts UserAgentParser::Cli.new(parser.parse(line), options).run! | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module UserAgentParser | ||
class Cli | ||
def initialize(user_agent, options = {}) | ||
@user_agent = user_agent | ||
@options = options | ||
end | ||
|
||
def run! | ||
if @options[:name] | ||
@user_agent.name | ||
elsif @options[:version] | ||
with_version do |version| | ||
version.to_s | ||
end | ||
elsif @options[:major] | ||
major | ||
elsif @options[:minor] | ||
minor | ||
elsif @options[:os] | ||
@user_agent.os.to_s | ||
elsif format = @options[:format] | ||
format.gsub('%n', @user_agent.name). | ||
gsub('%v', version.to_s). | ||
gsub('%M', major.to_s). | ||
gsub('%m', minor.to_s). | ||
gsub('%o', @user_agent.os.to_s) | ||
else | ||
@user_agent.to_s | ||
end | ||
end | ||
|
||
private | ||
|
||
def major | ||
with_version do |version| | ||
version.major | ||
end | ||
end | ||
|
||
def minor | ||
with_version do |version| | ||
version.minor | ||
end | ||
end | ||
|
||
def version | ||
@version ||= @user_agent.version | ||
end | ||
|
||
def with_version(&block) | ||
block.call(version) if version | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
require File.expand_path(File.dirname(__FILE__) + '/spec_helper') | ||
require 'user_agent_parser/cli' | ||
|
||
describe UserAgentParser::Cli do | ||
let(:cli) { UserAgentParser::Cli.new(user_agent, options) } | ||
let(:options) { {} } | ||
let(:parser) { UserAgentParser::Parser.new } | ||
let(:user_agent) { | ||
parser.parse('Mozilla/5.0 (iPad; CPU OS 6_0_1 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A523 Safari/8536.25') | ||
} | ||
|
||
it 'prints name and version when no options' do | ||
cli.run!.must_equal('Mobile Safari 6.0') | ||
end | ||
|
||
describe 'invalid version' do | ||
let(:user_agent) { | ||
parser.parse('Mozilla/5.0 (iPad; CPU OS like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/XYZ Mobile/10A523 Safari/8536.25') | ||
} | ||
|
||
describe '--version' do | ||
let(:options) { { :version => true } } | ||
|
||
it 'returns nil' do | ||
cli.run!.must_be_nil | ||
end | ||
end | ||
|
||
describe '--major' do | ||
let(:options) { { :major => true } } | ||
|
||
it 'returns nil' do | ||
cli.run!.must_be_nil | ||
end | ||
end | ||
|
||
describe '--minor' do | ||
let(:options) { { :minor => true } } | ||
|
||
it 'returns nil' do | ||
cli.run!.must_be_nil | ||
end | ||
end | ||
|
||
describe '--format' do | ||
let(:options) { { :format => '%n|%v|%M|%m|%o' } } | ||
|
||
it 'returns string without versions' do | ||
cli.run!.must_equal('Mobile Safari||||Other') | ||
end | ||
end | ||
end | ||
|
||
describe '--name' do | ||
let(:options) { { :name => true } } | ||
|
||
it 'returns name only' do | ||
cli.run!.must_equal('Mobile Safari') | ||
end | ||
end | ||
|
||
describe '--version' do | ||
let(:options) { { :version => true } } | ||
|
||
it 'returns version only' do | ||
cli.run!.must_equal('6.0') | ||
end | ||
end | ||
|
||
describe '--major' do | ||
let(:options) { { :major => true } } | ||
|
||
it 'returns major version only' do | ||
cli.run!.must_equal('6') | ||
end | ||
end | ||
|
||
describe '--minor' do | ||
let(:options) { { :minor => true } } | ||
|
||
it 'returns minor version only' do | ||
cli.run!.must_equal('0') | ||
end | ||
end | ||
|
||
describe '--os' do | ||
let(:options) { { :os => true } } | ||
|
||
it 'returns operating system only' do | ||
cli.run!.must_equal('iOS 6.0.1') | ||
end | ||
end | ||
|
||
describe '--format' do | ||
let(:options) { { :format => '%n|%v|%M|%m|%o' } } | ||
|
||
it 'return string with correct replacements' do | ||
cli.run!.must_equal('Mobile Safari|6.0|6|0|iOS 6.0.1') | ||
end | ||
end | ||
end |
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