Class: Fuzzyurl

Inherits:
Object
  • Object
show all
Defined in:
lib/fuzzyurl.rb,
lib/fuzzyurl/fields.rb,
lib/fuzzyurl/version.rb

Defined Under Namespace

Classes: Match, Protocols, Strings

Constant Summary collapse

FIELDS =
[
  :protocol,
  :username,
  :password,
  :hostname,
  :port,
  :path,
  :query,
  :fragment
]
VERSION =
"0.8.0"
VERSION_DATE =
"2015-12-25"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Fuzzyurl

Creates a new Fuzzyurl object from the given params or URL string. Keys of ‘params` should be symbols.

Parameters:

  • params (Hash|String|nil) (defaults to: {})

    URL string or parameter hash.



15
16
17
18
19
20
# File 'lib/fuzzyurl.rb', line 15

def initialize(params={})
  p = params.kind_of?(String) ? Fuzzyurl.from_string(params).to_hash : params
  (FIELDS & p.keys).each do |f|
    self.send("#{f}=", p[f])
  end
end

Class Method Details

.best_match(masks, url) ⇒ Integer|nil

Given an array of URL masks, returns the one which most closely matches ‘url`, or nil if none match.

‘url` and each element of `masks` may be Fuzzyurl or String format.

Parameters:

  • masks (Array)

    Array of URL masks.

  • url (Fuzzyurl|String)

    URL.

Returns:

  • (Integer|nil)

    Best-matching given mask, or nil for no match.



165
166
167
168
# File 'lib/fuzzyurl.rb', line 165

def best_match(masks, url)
  index = best_match_index(masks, url)
  index && masks[index]
end

.best_match_index(masks, url) ⇒ Integer|nil

Given an array of URL masks, returns the array index of the one which most closely matches ‘url`, or nil if none match.

‘url` and each element of `masks` may be Fuzzyurl or String format.

Parameters:

  • masks (Array)

    Array of URL masks.

  • url (Fuzzyurl|String)

    URL.

Returns:

  • (Integer|nil)

    Array index of best-matching mask, or nil for no match.



151
152
153
154
155
# File 'lib/fuzzyurl.rb', line 151

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)
  Fuzzyurl::Match.best_match_index(ms, u)
end

.from_string(str, opts = {}) ⇒ Fuzzyurl

Returns a Fuzzyurl representation of the given URL string. Any fields not present in ‘str` will be assigned the value of `opts` (defaults to nil).

Parameters:

  • str (String)

    String URL to convert to Fuzzyurl.

  • opts (Hash|nil) (defaults to: {})

    Options.

Returns:

  • (Fuzzyurl)

    Fuzzyurl representation of ‘str`.



93
94
95
# File 'lib/fuzzyurl.rb', line 93

def from_string(str, opts={})
  Fuzzyurl::Strings.from_string(str, opts)
end

.fuzzy_match(mask, value) ⇒ Object

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.

Wildcard language:

*              matches anything
foo/*          matches "foo/" and "foo/bar/baz" but not "foo"
foo/**         matches "foo/" and "foo/bar/baz" and "foo"
*.example.com  matches "api.v1.example.com" but not "example.com"
**.example.com matches "api.v1.example.com" and "example.com"

Any other form is treated as a literal match.

Parameters:

  • mask (String)

    String mask to match with (may contain wildcards).

  • value (String)

    String value to match.



187
188
189
# File 'lib/fuzzyurl.rb', line 187

def fuzzy_match(mask, value)
  Fuzzyurl::Match.fuzzy_match(mask, value)
end

.mask(params = {}) ⇒ Fuzzyurl

Returns a Fuzzyurl suitable for use as a URL mask, with the given values optionally set from ‘params` (Hash or String).

Parameters:

  • params (Hash|String|nil) (defaults to: {})

    Parameters to set.

Returns:



67
68
69
70
71
72
73
74
75
76
# File 'lib/fuzzyurl.rb', line 67

def mask(params={})
  params ||= {}
  return from_string(params, default: "*") if params.kind_of?(String)

  m = Fuzzyurl.new
  FIELDS.each do |f|
    m.send("#{f}=", params.has_key?(f) ? params[f].to_s : "*")
  end
  m
end

.match(mask, url) ⇒ Integer|nil

Returns an integer representing how closely ‘mask` matches `url` (0 means wildcard match, higher is closer), or nil for no match.

‘mask` and `url` may each be Fuzzyurl or String format.

Parameters:

Returns:

  • (Integer|nil)

    0 for wildcard match, 1 for perfect match, or nil.



105
106
107
108
109
# File 'lib/fuzzyurl.rb', line 105

def match(mask, url)
  m = mask.kind_of?(Fuzzyurl) ? mask : Fuzzyurl.mask(mask)
  u = url.kind_of?(Fuzzyurl) ? url : Fuzzyurl.from_string(url)
  Fuzzyurl::Match.match(m, u)
end

.match_scores(mask, url) ⇒ Object

Returns a Hash of match scores for each field of ‘mask` and `url`, indicating the closeness of the match. Values are from `fuzzy_match`: 0 indicates wildcard match, 1 indicates perfect match, and nil indicates no match.

‘mask` and `url` may each be Fuzzyurl or String format.

Parameters:



135
136
137
138
139
140
141
# File 'lib/fuzzyurl.rb', line 135

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)
  Fuzzyurl::Match.match_scores(m, u)
end

.matches?(mask, url) ⇒ Boolean

Returns true if ‘mask` matches `url`, false otherwise.

‘mask` and `url` may each be Fuzzyurl or String format.

Parameters:

Returns:

  • (Boolean)

    Whether ‘mask` matches `url`.



118
119
120
121
122
123
124
# File 'lib/fuzzyurl.rb', line 118

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)
  Fuzzyurl::Match.matches?(m, u)
end

.to_string(fuzzyurl) ⇒ String

Returns a string representation of ‘fuzzyurl`.

Parameters:

  • fuzzyurl (Fuzzyurl)

    Fuzzyurl to convert to string.

Returns:

  • (String)

    String representation of ‘fuzzyurl`.



82
83
84
# File 'lib/fuzzyurl.rb', line 82

def to_string(fuzzyurl)
  Fuzzyurl::Strings.to_string(fuzzyurl)
end

Instance Method Details

#==(other) ⇒ Object



55
56
57
# File 'lib/fuzzyurl.rb', line 55

def ==(other)
  self.to_hash == other.to_hash
end

#to_hashHash

Returns a hash representation of this Fuzzyurl, with one key/value pair for each of ‘Fuzzyurl::FIELDS`.

Returns:

  • (Hash)

    Hash representation of this Fuzzyurl.



26
27
28
29
30
31
32
33
# File 'lib/fuzzyurl.rb', line 26

def to_hash
  FIELDS.reduce({}) do |hash, f|
    val = self.send(f)
    val = val.to_s if val
    hash[f] = val
    hash
  end
end

#to_sString

Returns a string representation of this Fuzzyurl.

Returns:

  • (String)

    String representation of this Fuzzyurl.



50
51
52
# File 'lib/fuzzyurl.rb', line 50

def to_s
  Fuzzyurl::Strings.to_string(self)
end

#with(params = {}) ⇒ Fuzzyurl

Returns a new copy of this Fuzzyurl, with the given params changed.

Parameters:

  • params (Hash|nil) (defaults to: {})

    New parameter values.

Returns:

  • (Fuzzyurl)

    Copy of ‘self` with the given parameters changed.



39
40
41
42
43
44
45
# File 'lib/fuzzyurl.rb', line 39

def with(params={})
  fu = Fuzzyurl.new(self.to_hash)
  (FIELDS & params.keys).each do |f|
    fu.send("#{f}=", params[f].to_s)
  end
  fu
end