外部サービスへのアクセスを管理するために ActiveResource を使用しています。
外部サービスには次のような URL があります。
http://api.cars.com/v1/cars/car_id/range/range_num?filter=filter1,filter2
ここに私の車のクラスがあります:
class Car < ActiveResource::Base
class << self
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}/#{URI.parser.escape id.to_s}#{query_string(query_options)}"
end
def collection_path(prefix_options = {}, query_options = nil)
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
"#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}"
end end
self.site = "http://api.cars.com/"
self.prefix = "/v1/"
self.format = :json
end
Rails コンソールで特定の車を取得するようにオブジェクトを設定すると、次のようになります。
> car = car.new
> car.get('1234')
次のような URL を取得します。
http://api.cars.com/v1/cars//1234.json
URL に range 要素と range_num 要素を含めるにはどうすればよいですか?
また、URL の末尾に .json 拡張子を付けたくありません。ここで説明されているように、 element_name および collection_name メソッドをオーバーライドしようとしました: How to remove .xml and .json from url when using active resource but it doesn't like to work for me...
アイデアをお寄せいただきありがとうございます。