I am trying to post coordinates for origin and destination to bing api for getting directions. But I am unable to send the data.
def get_info_from_bing(params)
base_url="http://dev.virtualearth.net/REST/v1/Routes/"
query_params = "?" + {
"wayPoint.1" => params[:origin],
"waypoint.2" => params[:destination],
"dateTime" => params[:time] || Time.now.strftime("%H:%M"),
"timeType" => "Arrival",
"key" => ENV['BING_KEY']
}.map {|k,v| "#{k}=#{CGI.escape(v)}"}*"&"
modes=%w{driving walking transit}
Error:
NoMethodError - undefined method `gsub' for nil:NilClass:
/Users/anirvan/.rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/cgi/util.rb:7:in `escape'
/Users/anirvan/codeforamerica-transpochoices-1ccac48/choices.rb:15:in `block in get_info_from_bing'
/Users/anirvan/codeforamerica-transpochoices-1ccac48/choices.rb:15:in `each'
/Users/anirvan/codeforamerica-transpochoices-1ccac48/choices.rb:15:in `map'
/Users/anirvan/codeforamerica-transpochoices-1ccac48/choices.rb:15:in `get_info_from_bing'
/Users/anirvan/codeforamerica-transpochoices-1ccac48/choices.rb:120:in `block in <top (required)>'
/Users/anirvan/.rvm/gems/ruby-1.9.2-p318/gems/sinatra-1.3.1/lib/sinatra/base.rb:1212:in `call'
/Users/anirvan/.rvm/gems/ruby-1.9.2-p318/gems/sinatra-1.3.1/lib/sinatra/base.rb:1212:in `block in compile!'
/Users/anirvan/.rvm/gems/ruby-1.9.2-p318/gems/sinatra-1.3.1/lib/sinatra/base.rb:772:in `[]'
/Users/anirvan/.rvm/gems/ruby-1.9.2-p318/gems/sinatra-1.3.1/lib/sinatra/base.rb:772:in `block (3 levels) in route!'
I am new to sinatra and ruby. Does anyone know what the problem is?
Use
params.inspectto print the content of yourparamarray. One of the values is certainlynil, that whyCGI.escape(v)throws an exception.If you can fix the
nilvalue, use the following line as a workaround: