I’m trying to make a request to a web service (fwix), and in my rails app I’ve created the following initializer, which works… sorta, I have two problems however:
-
For some reason the values of the parameters need to have
+‘s as the spaces, is this a standard thing that I can accomplish with ruby? Additionally is this a standard way to form a url? I thought that spaces were%20. -
In my code how can I take any of the options sent in and just use them instead of having to state each one like
query_items << "api_key=#{options[:api_key]}" if options[:api_key]
The following is my code, the trouble area I’m having are the lines starting with query_items for each parameter in the last method, any ideas would be awesome!
require 'httparty'
module Fwix
class API
include HTTParty
class JSONParser < HTTParty::Parser
def json
JSON.parse(body)
end
end
parser JSONParser
base_uri "http://geoapi.fwix.com"
def self.query(options = {})
begin
query_url = query_url(options)
puts "querying: #{base_uri}#{query_url}"
response = get( query_url )
rescue
raise "Connection to Fwix API failed" if response.nil?
end
end
def self.query_url(input_options = {})
@defaults ||= {
:api_key => "my_api_key",
}
options = @defaults.merge(input_options)
query_url = "/content.json?"
query_items = []
query_items << "api_key=#{options[:api_key]}" if options[:api_key]
query_items << "province=#{options[:province]}" if options[:province]
query_items << "city=#{options[:city]}" if options[:city]
query_items << "address=#{options[:address]}" if options[:address]
query_url += query_items.join('&')
query_url
end
end
end
1 Answer