How can I extract all urls / links from email using Ruby on Rails?

I am creating a bookmarking site. I want to extract all URIs / links from email. My site uses Ruby on Rails.

How to extract all urls of received email content?

+3
source share
2 answers

Ruby's built-in URI does this already:

From extractdocs:

require "uri"

URI.extract("text here http://foo.example.org/bla and here mailto:test@example.com and here also.")
# => ["http://foo.example.com/bla", "mailto:test@example.com"]
+11
source
require 'uri'

text = %{"test
<a href="http://www.a.com/">http://www.a.com/</a>, and be sure
to check http://www.a.com/blog/. Email me at <a href="mailto:b@a.com">b@a.com</a>.}


END_CHARS = %{.,'?!:;}
p URI.extract(text, ['http']).collect { |u| END_CHARS.index(u[-1]) ? u.chop : u }

Source: http://www.java2s.com/Code/Ruby/Network/ExtractURL.htm

+4
source

All Articles