Skip to content

Commit

Permalink
Merge 8c8b3fd into 9c4afe8
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengroat committed May 30, 2017
2 parents 9c4afe8 + 8c8b3fd commit e31e1b8
Show file tree
Hide file tree
Showing 15 changed files with 158 additions and 165 deletions.
16 changes: 16 additions & 0 deletions .rubocop.yml
@@ -0,0 +1,16 @@
Metrics/LineLength:
Enabled: false
Metrics/BlockLength:
Enabled: false
Metrics/MethodLength:
Enabled: false
Metrics/PerceivedComplexity:
Enabled: false
Metrics/CyclomaticComplexity:
Enabled: false
Metrics/AbcSize:
Enabled: false
Style/Documentation:
Enabled: false
Style/ClassAndModuleChildren:
Enabled: false
1 change: 0 additions & 1 deletion Gemfile
@@ -1,3 +1,2 @@
source 'https://rubygems.org'
gemspec

8 changes: 5 additions & 3 deletions Rakefile
@@ -1,12 +1,15 @@
require 'rake/testtask'
require 'rubocop/rake_task'

task :default => [:test]
task default: %w[test rubocop]

RuboCop::RakeTask.new

Rake::TestTask.new do |t|
t.libs << 'test'
t.libs << 'test/lib'
t.test_files = FileList['test/**/*_test.rb']
#t.warning = true
# t.warning = true
t.verbose = true
end

Expand All @@ -21,4 +24,3 @@ task :release do
gem push fuzzyurl-#{Fuzzyurl::VERSION}.gem
EOT
end

2 changes: 1 addition & 1 deletion fuzzyurl.gemspec
Expand Up @@ -22,5 +22,5 @@ Gem::Specification.new do |s|
s.add_development_dependency 'mocha'
s.add_development_dependency 'pry'
s.add_development_dependency 'coveralls'
s.add_development_dependency 'rubocop'
end

53 changes: 21 additions & 32 deletions lib/fuzzyurl.rb
Expand Up @@ -4,7 +4,6 @@
require 'fuzzyurl/match'
require 'fuzzyurl/strings'


# Fuzzyurl provides two related functions: non-strict parsing of URLs or
# URL-like strings into their component pieces (protocol, username, password,
# hostname, port, path, query, and fragment), and fuzzy matching of URLs
Expand Down Expand Up @@ -80,17 +79,17 @@
# #=> 1
#
class Fuzzyurl
FIELDS.each {|f| attr_accessor f}
FIELDS.each { |f| attr_accessor f }

# Creates a new Fuzzyurl object from the given params or URL string.
# Keys of `params` should be symbols.
#
# @param params [Hash|String|nil] URL string or parameter hash.
# @return [Fuzzyurl] New Fuzzyurl object.
def initialize(params={})
p = params.kind_of?(String) ? Fuzzyurl.from_string(params).to_hash : params
def initialize(params = {})
p = params.is_a?(String) ? Fuzzyurl.from_string(params).to_hash : params
(FIELDS & p.keys).each do |f|
self.send("#{f}=", p[f])
send("#{f}=", p[f])
end
end

Expand All @@ -99,20 +98,19 @@ def initialize(params={})
#
# @return [Hash] Hash representation of this Fuzzyurl.
def to_hash
FIELDS.reduce({}) do |hash, f|
val = self.send(f)
FIELDS.each_with_object({}) do |f, hash|
val = send(f)
val = val.to_s if val
hash[f] = val
hash
end
end

# Returns a new copy of this Fuzzyurl, with the given params changed.
#
# @param params [Hash|nil] New parameter values.
# @return [Fuzzyurl] Copy of `self` with the given parameters changed.
def with(params={})
fu = Fuzzyurl.new(self.to_hash)
def with(params = {})
fu = Fuzzyurl.new(to_hash)
(FIELDS & params.keys).each do |f|
fu.send("#{f}=", params[f].to_s)
end
Expand All @@ -128,24 +126,22 @@ def to_s

# @private
def ==(other)
self.to_hash == other.to_hash
to_hash == other.to_hash
end


class << self

# Returns a Fuzzyurl suitable for use as a URL mask, with the given
# values optionally set from `params` (Hash or String).
#
# @param params [Hash|String|nil] Parameters to set.
# @return [Fuzzyurl] Fuzzyurl mask object.
def mask(params={})
def mask(params = {})
params ||= {}
return from_string(params, default: "*") if params.kind_of?(String)
return from_string(params, default: '*') if params.is_a?(String)

m = Fuzzyurl.new
FIELDS.each do |f|
m.send("#{f}=", params.has_key?(f) ? params[f].to_s : "*")
m.send("#{f}=", params.key?(f) ? params[f].to_s : '*')
end
m
end
Expand All @@ -165,7 +161,7 @@ def to_string(fuzzyurl)
# @param str [String] String URL to convert to Fuzzyurl.
# @param opts [Hash|nil] Options.
# @return [Fuzzyurl] Fuzzyurl representation of `str`.
def from_string(str, opts={})
def from_string(str, opts = {})
Fuzzyurl::Strings.from_string(str, opts)
end

Expand All @@ -178,8 +174,8 @@ def from_string(str, opts={})
# @param url [Fuzzyurl|String] URL.
# @return [Integer|nil] 0 for wildcard match, 1 for perfect match, or nil.
def match(mask, url)
m = mask.kind_of?(Fuzzyurl) ? mask : Fuzzyurl.mask(mask)
u = url.kind_of?(Fuzzyurl) ? url : Fuzzyurl.from_string(url)
m = mask.is_a?(Fuzzyurl) ? mask : Fuzzyurl.mask(mask)
u = url.is_a?(Fuzzyurl) ? url : Fuzzyurl.from_string(url)
Fuzzyurl::Match.match(m, u)
end

Expand All @@ -191,10 +187,8 @@ def match(mask, url)
# @param url [Fuzzyurl|String] URL.
# @return [Boolean] Whether `mask` matches `url`.
def matches?(mask, url)
m = mask.kind_of?(Fuzzyurl) ? m : Fuzzyurl.mask(m)
u = url.kind_of?(Fuzzyurl) ? u : Fuzzyurl.from_string(u)
m = mask.kind_of?(Fuzzyurl) ? mask : Fuzzyurl.mask(mask)
u = url.kind_of?(Fuzzyurl) ? url : Fuzzyurl.from_string(url)
m = mask.is_a?(Fuzzyurl) ? mask : Fuzzyurl.mask(mask)
u = url.is_a?(Fuzzyurl) ? url : Fuzzyurl.from_string(url)
Fuzzyurl::Match.matches?(m, u)
end

Expand All @@ -208,10 +202,8 @@ def matches?(mask, url)
# @param mask [Fuzzyurl|String] URL mask.
# @param url [Fuzzyurl|String] URL.
def match_scores(mask, url)
m = mask.kind_of?(Fuzzyurl) ? m : Fuzzyurl.mask(m)
u = url.kind_of?(Fuzzyurl) ? u : Fuzzyurl.from_string(u)
m = mask.kind_of?(Fuzzyurl) ? mask : Fuzzyurl.mask(mask)
u = url.kind_of?(Fuzzyurl) ? url : Fuzzyurl.from_string(url)
m = mask.is_a?(Fuzzyurl) ? mask : Fuzzyurl.mask(mask)
u = url.is_a?(Fuzzyurl) ? url : Fuzzyurl.from_string(url)
Fuzzyurl::Match.match_scores(m, u)
end

Expand All @@ -224,8 +216,8 @@ def match_scores(mask, url)
# @param url [Fuzzyurl|String] URL.
# @return [Integer|nil] Array index of best-matching mask, or nil for no match.
def best_match_index(masks, url)
ms = masks.map {|m| m.kind_of?(Fuzzyurl) ? m : Fuzzyurl.mask(m)}
u = url.kind_of?(Fuzzyurl) ? url : Fuzzyurl.from_string(url)
ms = masks.map { |m| m.is_a?(Fuzzyurl) ? m : Fuzzyurl.mask(m) }
u = url.is_a?(Fuzzyurl) ? url : Fuzzyurl.from_string(url)
Fuzzyurl::Match.best_match_index(ms, u)
end

Expand Down Expand Up @@ -262,8 +254,5 @@ def best_match(masks, url)
def fuzzy_match(mask, value)
Fuzzyurl::Match.fuzzy_match(mask, value)
end

end # class << self

end

21 changes: 10 additions & 11 deletions lib/fuzzyurl/fields.rb
@@ -1,13 +1,12 @@
class Fuzzyurl
FIELDS = [
:protocol,
:username,
:password,
:hostname,
:port,
:path,
:query,
:fragment
]
FIELDS = %i[
protocol
username
password
hostname
port
path
query
fragment
].freeze
end

17 changes: 5 additions & 12 deletions lib/fuzzyurl/match.rb
Expand Up @@ -2,7 +2,6 @@

class Fuzzyurl::Match
class << self

# If `mask` (which may contain * wildcards) matches `url` (which may not),
# returns an integer representing how closely they match (higher is closer).
# If `mask` does not match `url`, returns null.
Expand All @@ -16,7 +15,6 @@ def match(mask, url)
scores.values.reduce(:+)
end


# If `mask` (which may contain * wildcards) matches `url` (which may not),
# returns true; otherwise returns false.
#
Expand All @@ -27,7 +25,6 @@ def matches?(mask, url)
match(mask, url) != nil
end


# Returns a Fuzzyurl-like object containing values representing how well
# different parts of `mask` and `url` match. Values are integers for
# matches or null for no match; higher integers indicate a better match.
Expand All @@ -50,7 +47,6 @@ def match_scores(mask, url)
}
end


# From a list of Fuzzyurl `masks`, returns the index of the one which best
# matches `url`. Returns null if none of `masks` match.
#
Expand All @@ -70,7 +66,6 @@ def best_match_index(masks, url)
best_index
end


# If `mask` (which may contain * wildcards) matches `url` (which may not),
# returns 1 if `mask` and `url` match perfectly, 0 if `mask` and `url`
# are a wildcard match, or null otherwise.
Expand All @@ -89,39 +84,37 @@ def best_match_index(masks, url)
# @param value [String] String value to match.
# @returns [Integer|nil] 0 for wildcard match, 1 for perfect match, else nil.
def fuzzy_match(mask, value)
return 0 if mask == "*"
return 0 if mask == '*'
return 1 if mask == value
return nil if !mask || !value

if mask.index("**.") == 0
if !mask.index('**.').nil? && mask.index('**.').zero?
mask_value = mask[3..-1]
return 0 if value.end_with?(".#{mask_value}")
return 0 if mask_value == value
return nil
end
if mask.index("*") == 0
if !mask.index('*').nil? && mask.index('*').zero?
return 0 if value.end_with?(mask[1..-1])
return nil
end

rev_mask = mask.reverse
rev_value = value.reverse

if rev_mask.index("**/") == 0
if !rev_mask.index('**/').nil? && rev_mask.index('**/').zero?
rev_mask_value = rev_mask[3..-1]
return 0 if rev_value.end_with?("/#{rev_mask_value}")
return 0 if rev_mask_value == rev_value
return nil
end

if rev_mask.index("*") == 0
if !rev_mask.index('*').nil? && rev_mask.index('*').zero?
return 0 if rev_value.end_with?(rev_mask[1..-1])
return nil
end

nil
end

end
end

5 changes: 2 additions & 3 deletions lib/fuzzyurl/protocols.rb
Expand Up @@ -3,13 +3,13 @@ class Fuzzyurl::Protocols
'ssh' => '22',
'http' => '80',
'https' => '443'
}
}.freeze

PROTOCOLS_BY_PORT = {
'22' => 'ssh',
'80' => 'http',
'443' => 'https'
}
}.freeze

class << self
def get_port(protocol)
Expand All @@ -23,4 +23,3 @@ def get_protocol(port)
end
end
end

34 changes: 15 additions & 19 deletions lib/fuzzyurl/strings.rb
Expand Up @@ -17,40 +17,36 @@ class Fuzzyurl::Strings
}x

class << self

def from_string(str, opts={})
return nil unless str.kind_of?(String)
def from_string(str, opts = {})
return nil unless str.is_a?(String)

default = opts[:default]
if m = REGEX.match(str)
fu = Fuzzyurl.new
Fuzzyurl::FIELDS.each do |f|
fu.send("#{f}=", m[f] || default)
end
fu
else
raise ArgumentError, "Couldn't parse url string: #{str}"
m = REGEX.match(str)
raise ArgumentError, "Couldn't parse url string: #{str}" unless m
fu = Fuzzyurl.new
Fuzzyurl::FIELDS.each do |f|
fu.send("#{f}=", m[f] || default)
end
fu
end

def to_string(fuzzyurl)
if !fuzzyurl.kind_of?(Fuzzyurl)
raise ArgumentError, "`fuzzyurl` must be a Fuzzyurl"
unless fuzzyurl.is_a?(Fuzzyurl)
raise ArgumentError, '`fuzzyurl` must be a Fuzzyurl'
end

fu = fuzzyurl
str = ""
str = ''
str << "#{fu.protocol}://" if fu.protocol
str << "#{fu.username}" if fu.username
str << fu.username.to_s if fu.username
str << ":#{fu.password}" if fu.password
str << "@" if fu.username
str << "#{fu.hostname}" if fu.hostname
str << '@' if fu.username
str << fu.hostname.to_s if fu.hostname
str << ":#{fu.port}" if fu.port
str << "#{fu.path}" if fu.path
str << fu.path.to_s if fu.path
str << "?#{fu.query}" if fu.query
str << "##{fu.fragment}" if fu.fragment
str
end

end
end
5 changes: 2 additions & 3 deletions lib/fuzzyurl/version.rb
@@ -1,5 +1,4 @@
class Fuzzyurl
VERSION = "0.9.0"
VERSION_DATE = "2016-06-28"
VERSION = '0.9.0'.freeze
VERSION_DATE = '2016-06-28'.freeze
end

0 comments on commit e31e1b8

Please sign in to comment.