Reputation: 45941
Given url = 'http://www.foo.com/bar?u=http://example.com/yyy/zzz.jpg&aaa=bbb&ccc=ddd'
What is a good way to extract http://example.com/yyy/zzz.jpg
?
EDIT: I would like to extract the second url.
Upvotes: 0
Views: 66
Reputation: 160631
Using Ruby 2.0+:
require 'uri'
url = 'http://www.foo.com/bar?u=http://example.com/yyy/zzz.jpg&aaa=bbb&ccc=ddd'
uri = URI.parse(url)
URI.decode_www_form(uri.query).to_h['u'] # => "http://example.com/yyy/zzz.jpg"
For Ruby < 2.0:
require 'uri'
url = 'http://www.foo.com/bar?u=http://example.com/yyy/zzz.jpg&aaa=bbb&ccc=ddd'
uri = URI.parse(url)
Hash[URI.decode_www_form(uri.query)]['u'] # => "http://example.com/yyy/zzz.jpg"
The Addressable gem is very full-featured, and follows the specs better than URI. The same thing can be done using:
require 'addressable/uri'
url = 'http://www.foo.com/bar?u=http://example.com/yyy/zzz.jpg&aaa=bbb&ccc=ddd'
uri = Addressable::URI.parse(url)
uri.query_values['u'] # => "http://example.com/yyy/zzz.jpg"
Upvotes: 0
Reputation: 99061
require "uri"
URI.extract("text here http://foo.example.org/bla and here mailto:[email protected] and here also.")
# => ["http://foo.example.org/bla", "mailto:[email protected]"]
http://www.ruby-doc.org/stdlib-2.1.1/libdoc/uri/rdoc/URI.html
Upvotes: 3
Reputation: 118299
I'd do :-
require 'uri'
url = 'http://www.foo.com/bar?u=http://example.com/yyy/zzz.jpg&aaa=bbb&ccc=ddd'
uri = URI(url)
URI.decode_www_form(uri.query).select { |_,b| b[/^http(s)?/] }.map(&:last)
# => ["http://example.com/yyy/zzz.jpg"]
# or something like
Hash[URI.decode_www_form(uri.query)]['u'] # => "http://example.com/yyy/zzz.jpg"
Upvotes: 3