Anu
Anu

Reputation: 39

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

I am building a bookmarking site. I want to extract all URIs/links from an email. My site is in using Ruby on Rails.

How can I extract all the URLs of received email content?

Upvotes: 3

Views: 1769

Answers (2)

the Tin Man
the Tin Man

Reputation: 160601

Ruby's built-in URI module does this already:

From the extract docs:

require "uri"

URI.extract("text here http://foo.example.org/bla and here mailto:[email protected] and here also.")
# => ["http://foo.example.com/bla", "mailto:[email protected]"]

Upvotes: 11

thekindofme
thekindofme

Reputation: 3866

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:[email protected]">[email protected]</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

Upvotes: 4

Related Questions