ruby on rails - Youtube gdata and search term having "&" -


i have following code let program search youtube gdata.

class youtube    def search_url(term)      url = "https://gdata.youtube.com/feeds/api/videos"      url += "?q=#{term}&alt=json&restriction=us&max-results=50&orderby=viewcount"  url += "&fields=entry(id,title,yt:noembed,media:group(media:description),author(name),yt:statistics(@viewcount))"      url += "&key=#{dev_key}" 

however, when tested program, seems fails search when search term contains "&", popular duo artist "macklemore & ryan lewis".

"&" might not cause of failure. suspect it. if think "&" not cause, think cause of failure? if think "&" cause, how can fix it?

you need escape term before sending url parameter:

require 'cgi'  def search_url(term)   term = cgi.escape(term)   url = "https://gdata.youtube.com/feeds/api/videos"   url += "?q=#{term}&alt=json&restriction=us&max-results=50&orderby=viewcount"   url += "&fields=entry(id,title,yt:noembed,media:group(media:description),author(name),yt:statistics(@viewcount))"   url += "&key=#{dev_key}" 

escaping using cgi.escape results in uri-safe parameter:

cgi.escape('macklemore & ryan lewis') # => "macklemore+%26+ryan+lewis" 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -