diff --git a/bin/user_agent_parser b/bin/user_agent_parser new file mode 100755 index 0000000..5c83fd4 --- /dev/null +++ b/bin/user_agent_parser @@ -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 diff --git a/lib/user_agent_parser/cli.rb b/lib/user_agent_parser/cli.rb new file mode 100644 index 0000000..9ee01af --- /dev/null +++ b/lib/user_agent_parser/cli.rb @@ -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 diff --git a/spec/cli_spec.rb b/spec/cli_spec.rb new file mode 100644 index 0000000..9cea04b --- /dev/null +++ b/spec/cli_spec.rb @@ -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 diff --git a/user_agent_parser.gemspec b/user_agent_parser.gemspec index 22d8366..7e7c1a5 100644 --- a/user_agent_parser.gemspec +++ b/user_agent_parser.gemspec @@ -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