class UrlExtract
Finds each URL in a plain text, with its place in the text:
UrlExtract.scan("See https://example.com/x and https://example.com/x").map(&:url) # => ["https://example.com/x", "https://example.com/x"]
The URLs are raw: each URL as the text writes it, at each place, so a URL can come more than one time. UrlExtract.scan describes the options, and UrlExtract::Match describes a match.
Constants
- DEFAULTS
-
The default options of an extractor.
UrlExtract.scandescribes each option. - Rules
-
The rules of an extractor, built from its options: - prefix_regexp: the regexp of each prefix, or nil when no prefix can start a URL - max_bytesize: the largest size of a URL, in bytes, or infinity - trim: the
Trimming::Trimof the trim options - on_encoding_error: what a text that the extractor can not read doesThe rules are frozen, as each part of them, so threads and Ractors can share them.
- VERSION
-
Version of the gem
Public Class Methods
(String text, **untyped options) → UrlExtract
(String text, **untyped options) → void
Source
# File lib/url_extract.rb, line 139 def initialize(text, **options) @reader = Input::Reader.new(text, Rules.from(options)) end
The same as UrlExtract.scan.
(String text, **untyped options) → UrlExtract
Source
# File lib/url_extract.rb, line 126 def self.scan(text, **) new(text, **) end
Returns an extractor of the URLs of the text: a UrlExtract, which is enumerable. It reads the text only as far as a method of Enumerable asks: first(2) reads it up to the second URL:
extractor = UrlExtract.scan("See https://example.com/x, https://example.org/y and https://example.net/z") extractor.map(&:url) # => ["https://example.com/x", "https://example.org/y", "https://example.net/z"] extractor.first(2).map(&:url) # => ["https://example.com/x", "https://example.org/y"]
UrlExtract.(text) is the same:
UrlExtract.("See https://example.com/x").map(&:url) # => ["https://example.com/x"]
Each option has its default in DEFAULTS.
With prefixes:, what starts a URL. A prefix with :// starts a URL at each place. Another prefix, such as www., starts a URL only at the start of a word, before a letter or a digit:
text = "See www.example.com, localhost:3000/x and https://example.com/x" UrlExtract.scan(text, prefixes: %w[www. localhost: https://]).map(&:url) # => ["www.example.com", "localhost:3000/x", "https://example.com/x"]
With max_bytesize:, the largest size of a URL, in bytes. A longer URL gives no match, and no match for a part of it. nil sets no limit:
text = "See https://example.com/x and https://example.com/very/long/path" UrlExtract.scan(text, max_bytesize: 25).map(&:url) # => ["https://example.com/x"] UrlExtract.scan(text, max_bytesize: nil).map(&:url) # => ["https://example.com/x", "https://example.com/very/long/path"]
With trim:, the strings to remove from the end of a URL. The default list holds punctuation and closing quotes, and [] removes nothing:
UrlExtract.scan("See 'https://example.com/x'").map(&:url) # => ["https://example.com/x"] UrlExtract.scan("See https://example.com/x.", trim: []).map(&:url) # => ["https://example.com/x."] UrlExtract.scan("See https://example.com/x...ยป", trim: ["...", "ยป"]).map(&:url) # => ["https://example.com/x"]
With trim_html_entities: true, removes an HTML entity from the end of a URL, such as ", ' or '. An entity is one unit: the ; of the trim list does not cut its end. false keeps it:
text = "<p>"https://example.com/x"</p>" UrlExtract.scan(text).map(&:url) # => ["https://example.com/x"] UrlExtract.scan(text, trim_html_entities: false).map(&:url) # => ["https://example.com/x""]
With trim_brackets: true, removes from the end of a URL a closing bracket that the URL does not open, and an opening bracket that the URL does not close. false removes no bracket:
UrlExtract.scan("(See https://example.com/x) and https://en.wikipedia.org/wiki/Ruby_(programming_language)").map(&:url) # => ["https://example.com/x", "https://en.wikipedia.org/wiki/Ruby_(programming_language)"] UrlExtract.scan("(See https://example.com/x)", trim_brackets: false).map(&:url) # => ["https://example.com/x)"]
With on_encoding_error:, what a text that the extractor can not read as UTF-8 does. It gives no match, and :warn warns at the line of the caller, :skip says nothing, and :raise raises UrlExtract::EncodingError instead:
UrlExtract.scan("See https://example.com/x \xFF").to_a # => [] # warning: url_extract: Invalid text: can not be read as UTF-8 (encoding UTF-8, index 26) UrlExtract.scan("See https://example.com/x \xFF", on_encoding_error: :skip).to_a # => []
Public Instance Methods
# File lib/url_extract.rb, line 162 def each return to_enum(:each) unless block_given? text = @reader.text @reader.each_url { |url, index| yield Match.new(url, index, text) } self end
With a block given, calls the block with each match, when the extractor finds it; returns self:
UrlExtract.scan("See https://example.com/x and https://example.org/y").each { |match| p match.url } # => "https://example.com/x" # => "https://example.org/y"
With no block given, returns an enumerator:
UrlExtract.scan("See https://example.com/x and https://example.org/y").each.next.url # => "https://example.com/x"
Each call reads the text again: call to_a to keep the matches.
() → String
Source
# File lib/url_extract.rb, line 184 def inspect shown = @reader.text.inspect shown = "#{shown[0, INSPECTED_TEXT_SIZE]}..." if shown.length > INSPECTED_TEXT_SIZE "#<#{self.class} text=#{shown}>" end
Returns a string that shows the start of the text, and not the rules of the options:
UrlExtract.scan("See https://example.com/x") # => #<UrlExtract text="See https://example.com/x">
A text that the extractor can not read shows as nil:
UrlExtract.scan("\xFF", on_encoding_error: :skip) # => #<UrlExtract text=nil>