Skip to content

Commit

Permalink
Fix rubocop and coverage (#43)
Browse files Browse the repository at this point in the history
* Fix HashSyntax

* fix Style/StringLiterals

* fix Layout/SpaceAfterComma

* fix Layout/IndentArray

* Fix more single offenses

* Configure code coverage
  • Loading branch information
wallin authored Oct 10, 2018
1 parent 90f213a commit d17bada
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 47 deletions.
5 changes: 1 addition & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
AllCops:
TargetRubyVersion: 1.9
TargetRubyVersion: 2.3

Style/FileName:
Exclude:
- "lib/castle-rb.rb"

Style/MethodMissing:
Enabled: false

Style/Documentation:
Enabled: false

Expand Down
34 changes: 20 additions & 14 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,26 @@ task :families do
root = Pathname(__FILE__).dirname
path = root.join('vendor', 'uap-core')

browser_families = paths_to_families([
# path.join('tests', 'test_ua.yaml'),
path.join('test_resources', 'firefox_user_agent_strings.yaml'),
path.join('test_resources', 'pgts_browser_list.yaml')
])

os_families = paths_to_families([
# path.join('tests', 'test_os.yaml'),
path.join('test_resources', 'additional_os_tests.yaml')
])

device_families = paths_to_families([
# path.join('tests', 'test_device.yaml'),
])
browser_families = paths_to_families(
[
# path.join('tests', 'test_ua.yaml'),
path.join('test_resources', 'firefox_user_agent_strings.yaml'),
path.join('test_resources', 'pgts_browser_list.yaml')
]
)

os_families = paths_to_families(
[
# path.join('tests', 'test_os.yaml'),
path.join('test_resources', 'additional_os_tests.yaml')
]
)

device_families = paths_to_families(
[
# path.join('tests', 'test_device.yaml'),
]
)

puts "\n\nBrowser Families"
puts browser_families.inspect
Expand Down
2 changes: 1 addition & 1 deletion bin/user_agent_parser
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require 'user_agent_parser/cli'

options = {}

optparse = OptionParser.new do|opts|
optparse = OptionParser.new do |opts|
opts.on('--family', 'Print family only') do
options[:family] = true
end
Expand Down
6 changes: 3 additions & 3 deletions lib/user_agent_parser/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def run!
minor
elsif @options[:os]
@user_agent.os.to_s
elsif format = @options[:format]
elsif (format = @options[:format])
format
.gsub('%f', @user_agent.family)
.gsub('%n', @user_agent.name)
Expand All @@ -47,8 +47,8 @@ def version
@version ||= @user_agent.version
end

def with_version(&block)
block.call(version) if version
def with_version
yield(version) if version
end
end
end
4 changes: 1 addition & 3 deletions lib/user_agent_parser/operating_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ def initialize(family = 'Other', version = nil)

def to_s
string = family
unless version.nil?
string += " #{version}"
end
string += " #{version}" unless version.nil?
string
end

Expand Down
24 changes: 13 additions & 11 deletions lib/user_agent_parser/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ def parse(user_agent)
parse_ua(user_agent, os, device)
end

private
private

def load_patterns(path)
yml = YAML.load_file(path)

# Parse all the regexs
yml.each_pair do |type, patterns|
yml.each_pair do |_type, patterns|
patterns.each do |pattern|
pattern[:regex] = Regexp.new(pattern['regex'], pattern['regex_flag'] == 'i')
end
Expand Down Expand Up @@ -92,7 +92,7 @@ def first_pattern_match(patterns, value)
else
def first_pattern_match(patterns, value)
patterns.each do |pattern|
if match = pattern[:regex].match(value)
if (match = pattern[:regex].match(value))
return [pattern, match]
end
end
Expand All @@ -119,19 +119,19 @@ def device_from_pattern_match(pattern, match)

if pattern['device_replacement']
family = pattern['device_replacement']
match.each_with_index { |m,i| family = family.sub("$#{i}", m) }
match.each_with_index { |m, i| family = family.sub("$#{i}", m) }
end
if pattern["model_replacement"]
model = pattern["model_replacement"]
match.each_with_index { |m,i| model = model.sub("$#{i}", m) }
if pattern['model_replacement']
model = pattern['model_replacement']
match.each_with_index { |m, i| model = model.sub("$#{i}", m) }
end
if pattern["brand_replacement"]
brand = pattern["brand_replacement"]
match.each_with_index { |m,i| brand = brand.sub("$#{i}", m) }
if pattern['brand_replacement']
brand = pattern['brand_replacement']
match.each_with_index { |m, i| brand = brand.sub("$#{i}", m) }
brand.strip!
end

model.strip! unless model.nil?
model&.strip!

Device.new(family.strip, model, brand)
end
Expand All @@ -153,12 +153,14 @@ def from_pattern_match(keys, pattern, match)
def interpolate(replacement, match)
group_idx = replacement.index('$')
return replacement if group_idx.nil?

group_nbr = replacement[group_idx + 1]
replacement.sub("$#{group_nbr}", match[group_nbr.to_i])
end

def version_from_segments(*segments)
return if segments.all?(&:nil?)

Version.new(*segments)
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/user_agent_parser/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

module UserAgentParser
class Version

# Private: Regex used to split string version string into major, minor,
# patch, and patch_minor.
SEGMENTS_REGEX = /\d+\-\d+|\d+[a-zA-Z]+$|\d+|[A-Za-z][0-9A-Za-z-]*$/
Expand Down
8 changes: 4 additions & 4 deletions spec/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
let(:cli) { UserAgentParser::Cli.new(user_agent, options) }
let(:options) { {} }
let(:parser) { UserAgentParser::Parser.new }
let(:user_agent) {
let(:user_agent) do
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')
}
end

it 'prints family and version when no options' do
cli.run!.must_equal('Mobile Safari 6.0')
end

describe 'invalid version' do
let(:user_agent) {
let(:user_agent) do
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')
}
end

describe '--version' do
let(:options) { { version: true } }
Expand Down
13 changes: 11 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
# frozen_string_literal: true

require 'minitest/autorun'
require 'coveralls'
Coveralls.wear!
require 'simplecov'

SimpleCov.formatter = Coveralls::SimpleCov::Formatter
SimpleCov.start do
add_filter '/.bundle/'
add_filter '/doc/'
add_filter '/spec/'
add_filter '/config/'
merge_timeout 600
end

require 'minitest/autorun'

$:.unshift File.expand_path('../../lib', __FILE__)
require 'user_agent_parser'
Expand Down
17 changes: 13 additions & 4 deletions spec/user_agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,25 @@
end

describe '#to_h' do
let(:expected) do
{
device: { family: 'iPhone', model: 'iPhone', brand: nil },
family: 'Mobile Safari',
os: {
version: { version: '4.2.1', major: '4', minor: '2', patch: '1', patch_minor: nil},
family: 'iOS'
},
version: { version: '5.0.2', major: '5', minor: '0', patch: '2', patch_minor: nil }
}
end

it 'returns everything' do
browser_version = UserAgentParser::Version.new('5.0.2')
os_version = UserAgentParser::Version.new('4.2.1')
os = UserAgentParser::OperatingSystem.new('iOS', os_version)
device = UserAgentParser::Device.new('iPhone')
agent = UserAgentParser::UserAgent.new('Mobile Safari', browser_version, os, device)
assert_equal(
{:device=>{:family=>"iPhone", :model=>"iPhone", :brand=>nil}, :family=>"Mobile Safari", :os=>{:version=>{:version=>"4.2.1", :major=>"4", :minor=>"2", :patch=>"1", :patch_minor=>nil}, :family=>"iOS"}, :version=>{:version=>"5.0.2", :major=>"5", :minor=>"0", :patch=>"2", :patch_minor=>nil}},
agent.to_h
)
assert_equal(expected, agent.to_h)
end
end
end

0 comments on commit d17bada

Please sign in to comment.