1

i am trying to get a value of "token" from this url

token=EC-6HV41821M6960550G

url = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-6HV41821M6960550G"

i know it can be done by regex but i think it can be done by some URI method.

Bloomberg
  • 2,317
  • 2
  • 25
  • 47

3 Answers3

4

Do can get it like this:

require 'uri'

url = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-xxx"

uri    = URI.parse(url)
params = CGI::parse(uri.query)

params['token'] # => 'EC-xxx'
spickermann
  • 100,941
  • 9
  • 101
  • 131
2

Just to show, some other ways also exist :-

#!/usr/bin/env ruby

require 'uri'

url = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-xxx"
uri = URI(url)
URI.decode_www_form(uri.query).to_h['token']
# => "EC-xxx"

Read the method ::decode_www_form docomentation.

Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
0
require 'cgi'

url = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-6HV41821M6960550G'

CGI.parse(url)['token']
Igor Guzak
  • 2,155
  • 1
  • 13
  • 20
javiyu
  • 1,336
  • 6
  • 9