【问题标题】:Remove .xml extension from ActiveResource request从 ActiveResource 请求中删除 .xml 扩展名
【发布时间】:2011-03-19 01:59:06
【问题描述】:

我正在尝试使用 ActiveResource 来使用来自第三方 API 的 xml 数据。我可以使用 RESTClient 应用程序成功进行身份验证并发出请求。我编写了我的应用程序,当我提出请求时,我收到 404 错误。我补充说:

ActiveResource::Base.logger = Logger.new(STDERR) 

到我的 development.rb 文件并找出问题所在。 API 以 xml 数据响应不以 xml 结尾的请求。 EG,这在 RESTClient 中有效:

https://api.example.com/contacts

但 ActiveResource 正在发送此请求

https://api.example.com/contacts.xml

有没有“好”的方法从 ActiveResource 生成的请求中去除扩展?

谢谢

【问题讨论】:

    标签: ruby-on-rails xml api rest activeresource


    【解决方案1】:

    您可能需要覆盖模型中的element_path 方法。

    根据 API,当前定义如下:

    def element_path(id, prefix_options = {}, query_options = nil)
      prefix_options, query_options = split_options(prefix_options) if query_options.nil?  
      "#{prefix(prefix_options)}#{collection_name}/#{id}.#{format.extension}#{query_string(query_options)}"
    end
    

    删除 .#{format.extension} 部分可能会满足您的需要。

    【讨论】:

    【解决方案2】:

    您可以覆盖 ActiveResource::Base 的方法

    将此库添加到 /lib/active_resource/extend/ 目录中,不要忘记取消注释
    "config.autoload_paths += %W(#{config.root}/lib)" in config/application.rb

    module ActiveResource #:nodoc:
      module Extend
        module WithoutExtension
          module ClassMethods
            def element_path_with_extension(*args)
              element_path_without_extension(*args).gsub(/.json|.xml/,'')
            end
            def new_element_path_with_extension(*args)
              new_element_path_without_extension(*args).gsub(/.json|.xml/,'')
            end
            def collection_path_with_extension(*args)
              collection_path_without_extension(*args).gsub(/.json|.xml/,'')
            end
          end
    
          def self.included(base)
            base.class_eval do
              extend ClassMethods
              class << self
                alias_method_chain :element_path, :extension
                alias_method_chain :new_element_path, :extension
                alias_method_chain :collection_path, :extension
              end
            end
          end  
        end
      end  
    end
    

    在模型中

    class MyModel < ActiveResource::Base
      include ActiveResource::Extend::WithoutExtension
    end
    

    【讨论】:

    • 注意,从 rails 3 开始,您可能希望将其放在应用程序顶级目录中的 /extras 下(而不是 /lib 下),因为现在如果您取消注释,目录 rails 将自动加载config/application.rb 中的正确类似。
    • 这是一个相当严厉的解决方案,它修改了 ActiveResource 应用程序范围的行为。更本地化的解决方案可能会更好,具体取决于要求。
    【解决方案3】:

    在逐个类的基础上覆盖this answer 中提到的_path 访问器要简单得多,而不是在应用程序范围内对 ActiveResource 进行猴子修补,这可能会干扰依赖于 ActiveResource 的其他资源或 gem。

    只需将方法直接添加到您的类中:

    class Contact < ActiveResource::Base
    
      def element_path
        super.gsub(/\.xml/, "")
      end
    
      def new_element_path
        super.gsub(/\.xml/, "")
      end
    
      def collection_path
        super.gsub(/\.xml/, "")
      end
    end
    

    如果您在同一个 API 中访问多个 RESTful 资源,您应该定义自己的基类来存放通用配置。对于自定义 _path 方法,这是一个更好的地方:

    # app/models/api/base.rb
    class Api::Base < ActiveResource::Base
      self.site     = "http://crazy-apis.com"
      self.username = "..."
      self.password = "..."
      self.prefix   = "/my-api/"
    
      # Strip .xml extension off generated URLs
      def element_path
        super.gsub(/\.xml/, "")
      end
      # def new_element_path...
      # def collection_path...
    end
    
    # app/models/api/contact.rb
    class Api::Contact < Api::Base
    
    end
    
    # app/models/api/payment.rb
    class Api::Payment < Api::Base
    
    end
    
    # Usage:
    
    Api::Contact.all()      # GET  http://crazy-apis.com/my-api/contacts
    Api::Payment.new().save # POST http://crazy-apis.com/my-api/payments
    

    【讨论】:

      【解决方案4】:

      您可以使用以下命令从路径中排除格式字符串:

      class MyModel < ActiveResource::Base
        self.include_format_in_path = false
      end
      

      【讨论】:

      • 请注意,这仅适用于 ActiveResource 4。如果您卡在 3.2.x 或更早版本,它将引发异常。
      猜你喜欢
      • 1970-01-01
      • 2012-06-29
      • 2015-03-20
      • 2012-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多