Ruby,形成 API 请求,无需隐式声明每个参数

发布于 2024-11-07 15:40:47 字数 1609 浏览 0 评论 0原文

我正在尝试向网络服务(fwix)发出请求,并且在我的 Rails 应用程序中我'我创建了以下初始化程序,它可以工作...但是,我有两个问题:

  1. 出于某种原因,参数的值需要有 + 作为空格,这是一个我可以用红宝石完成标准的事情吗?另外,这是形成 url 的标准方法吗?我认为空格是 %20

  2. 在我的代码中,如何获取发送的任何选项并使用它们,而不必像 query_items << "api_key=#{options[:api_key]}" if options[:api_key]

以下是我的代码,我遇到的问题区域是每个以 query_items 开头的行最后一个方法中的参数,任何想法都很棒!

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

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:

  1. 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.

  2. 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可爱咩 2024-11-14 15:40:47

对于 1)
您的 API 提供者需要“+”,因为 API 需要 CGI 格式的字符串而不是 URL 格式的字符串。

require 'cgi'
my_query = "hel lo"
CGI.escape(my_query)

这应该给你

"hel+lo" 

正如你

对问题 2 的期望)我会做类似的事情

query_items = options.keys.collect { |key| "#{key.to_s}=#{options[key]}" }

For 1)
You API provider is expecting '+' because the API is expecting in a CGI formatted string instead of URL formatted string.

require 'cgi'
my_query = "hel lo"
CGI.escape(my_query)

this should give you

"hel+lo" 

as you expect

for Question 2) I would do something like

query_items = options.keys.collect { |key| "#{key.to_s}=#{options[key]}" }
彻夜缠绵 2024-11-14 15:40:47
def self.query_url(input_options = {})
  options = {
    :api_key => "my_api_key",
  }.merge(input_options)

  query_url = "/content.json?"
  query_items = []

  options.each { |k, v| query_items << "#{k}=#{v.gsub(/\s/, '+')}" }

  query_url += query_items.join('&')
end
def self.query_url(input_options = {})
  options = {
    :api_key => "my_api_key",
  }.merge(input_options)

  query_url = "/content.json?"
  query_items = []

  options.each { |k, v| query_items << "#{k}=#{v.gsub(/\s/, '+')}" }

  query_url += query_items.join('&')
end
素食主义者 2024-11-14 15:40:47

我是 Fwix 的开发人员,希望帮助您解决网址转义问题。但是,使用 %20 转义对我有用:

wget 'http://geoapi.fwix.com/content.xml?api_key=mark&province=ca&city=san%20francisco&query=gavin%20newsom'

我希望你可以向我提供您提出的具体请求,表明您无法使用 %20 逃脱。

I'm a developer at Fwix and wanted to help you with your url escaping issue. However, escaping with %20 works for me:

wget 'http://geoapi.fwix.com/content.xml?api_key=mark&province=ca&city=san%20francisco&query=gavin%20newsom'

I was hoping you could provide me with the specific request you're making that you're unable to escape with %20.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文