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
Post a Comment