14

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 によって生成されるリクエストから拡張機能を削除する「いい」方法はありますか?

ありがとう

4

4 に答える 4

13

次を使用して、パスからフォーマット文字列を除外できます。

class MyModel < ActiveResource::Base
  self.include_format_in_path = false
end
于 2013-12-06T22:04:53.907 に答える
6

おそらく、モデルの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}の部分を削除すると、必要な処理が実行される場合があります。

于 2010-07-22T04:08:36.467 に答える
6

ActiveResource::Base のメソッドをオーバーライドできます

この lib を /lib/active_resource/extend/ ディレクトリに追加します config/application.rbの
"config.autoload_paths += %W(#{config.root}/lib)"のコメントを外すことを忘れないでください

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
于 2011-05-25T14:16:35.950 に答える
5

この回答_pathで言及されているアクセサーをクラスごとにオーバーライドする方が、ActiveResource に依存する他のリソースや gem に干渉する可能性があるアプリケーション全体で ActiveResource にモンキー パッチを適用するよりもはるかに簡単です。

メソッドをクラスに直接追加するだけです。

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
于 2012-08-03T22:02:33.807 に答える