Class: Fuzzyurl
- Inherits:
-
Object
- Object
- Fuzzyurl
- 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
-
.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.
-
.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.
-
.from_string(str, opts = {}) ⇒ Fuzzyurl
Returns a Fuzzyurl representation of the given URL string.
-
.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.
-
.mask(params = {}) ⇒ Fuzzyurl
Returns a Fuzzyurl suitable for use as a URL mask, with the given values optionally set from ‘params` (Hash or String).
-
.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.
-
.match_scores(mask, url) ⇒ Object
Returns a Hash of match scores for each field of ‘mask` and `url`, indicating the closeness of the match.
-
.matches?(mask, url) ⇒ Boolean
Returns true if ‘mask` matches `url`, false otherwise.
-
.to_string(fuzzyurl) ⇒ String
Returns a string representation of ‘fuzzyurl`.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(params = {}) ⇒ Fuzzyurl
constructor
Creates a new Fuzzyurl object from the given params or URL string.
-
#to_hash ⇒ Hash
Returns a hash representation of this Fuzzyurl, with one key/value pair for each of ‘Fuzzyurl::FIELDS`.
-
#to_s ⇒ String
Returns a string representation of this Fuzzyurl.
-
#with(params = {}) ⇒ Fuzzyurl
Returns a new copy of this Fuzzyurl, with the given params changed.
Constructor Details
#initialize(params = {}) ⇒ Fuzzyurl
Creates a new Fuzzyurl object from the given params or URL string. Keys of ‘params` should be symbols.
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.
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.
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).
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.
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).
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.
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.
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.
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 |
Instance Method Details
#==(other) ⇒ Object
55 56 57 |
# File 'lib/fuzzyurl.rb', line 55 def ==(other) self.to_hash == other.to_hash end |
#to_hash ⇒ Hash
Returns a hash representation of this Fuzzyurl, with one key/value pair for each of ‘Fuzzyurl::FIELDS`.
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 |