When an HTTP response is a format that is different from the one in which it exists (XML and JSON), some additional formatting needs to be parsed, creating a format that you are accustomed to, and using it in a category. Idiomatic formats should be implemented in the following ways: 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)
# Data encoded in a new format and returned
End
def decode (CSV)
# data is decoded in a new format and returns the end-end-end
class User < activeresource::base
Self.format = Activeresource::formats::extend::csvformat
...
End
If the HTTP request should not extend the send, overwrite the activeresource::base Element_path and Collection_path method, 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_op Tions.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 there is any need to change the URL.