ActiveResource in Ruby on Rails
When an HTTP response is in a different format (XML and JSON) than an existing one, you need to parse some additional formats to create a common format and use it in the category. The following methods should be implemented in the conventional format: extension, mime_type,
Encode and decode.
Module ActiveResource module Formats module Extend module CSVFormat extend self def extension 'csv' end def mime_type 'text/csv' end def encode (hash, options = nil) # The data is encoded in the new format and the end def decode (csv) is returned. # The data is decoded in the new format and the end class User is returned. <ActiveResource: Base self. format = ActiveResource: Formats: Extend: CSVFormat... end
If the HTTP request should not be extended, overwrite the ActiveResource: Base element_path and collection_path methods, and remove the extended part.
class User < ActiveResource::Base ... def self.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 def self.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 end
These methods can also be overwritten if you need to modify the website.