Skip to content

Commit

Permalink
Merge pull request #16 from rejeep/user-agent-parser-bin
Browse files Browse the repository at this point in the history
Add bin/user_agent_parser script.
  • Loading branch information
toolmantim committed Mar 13, 2014
2 parents ac46ab1 + a48d2a7 commit 858ccd9
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 1 deletion.
56 changes: 56 additions & 0 deletions bin/user_agent_parser
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
54 changes: 54 additions & 0 deletions lib/user_agent_parser/cli.rb
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
101 changes: 101 additions & 0 deletions spec/cli_spec.rb
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
2 changes: 1 addition & 1 deletion user_agent_parser.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |gem|
gem.description = gem.summary
gem.license = "MIT"

gem.files = %x{ git ls-files }.split("\n").select { |d| d =~ %r{^(MIT-LICENSE|Readme.md|lib/)} } + ['vendor/ua-parser/regexes.yaml']
gem.files = %x{ git ls-files }.split("\n").select { |d| d =~ %r{^(MIT-LICENSE|Readme.md|lib|bin/)} } + ['vendor/ua-parser/regexes.yaml']

gem.required_ruby_version = '>= 1.8.7'
end

0 comments on commit 858ccd9

Please sign in to comment.