| | 4 | class << self |
|---|
| | 5 | # Provides the currently implemented mime types as a hash |
|---|
| | 6 | def available_mime_types |
|---|
| | 7 | ResponderMixin::Rest::TYPES |
|---|
| | 8 | end |
|---|
| | 9 | |
|---|
| | 10 | # Any specific outgoing headers should be included here. These are not |
|---|
| | 11 | # the content-type header but anything in addition to it. |
|---|
| | 12 | # +tranform_method+ should be set to a symbol of the method used to |
|---|
| | 13 | # transform a resource into this mime type. |
|---|
| | 14 | # For example for the :xml mime type an object might be transformed by |
|---|
| | 15 | # calling :to_xml, or for the :js mime type, :to_json. |
|---|
| | 16 | # If there is no transform method, use nil. |
|---|
| | 17 | def add_mime_type(key,transform_method, values,new_response_headers = {}) |
|---|
| | 18 | raise ArgumentError unless key.is_a?(Symbol) && values.is_a?(Array) |
|---|
| | 19 | ResponderMixin::Rest::TYPES.update(key => values) |
|---|
| | 20 | add_response_headers!(key, new_response_headers) |
|---|
| | 21 | ResponderMixin::Rest::TRANSFORM_METHODS.merge!(key => transform_method) |
|---|
| | 22 | end |
|---|
| | 23 | |
|---|
| | 24 | def remove_mime_type(key) |
|---|
| | 25 | key == :all ? false : ResponderMixin::Rest::TYPES.delete(key) |
|---|
| | 26 | end |
|---|
| | 27 | |
|---|
| | 28 | def mime_transform_method(key) |
|---|
| | 29 | ResponderMixin::Rest::TRANSFORM_METHODS[key] |
|---|
| | 30 | end |
|---|
| | 31 | |
|---|
| | 32 | |
|---|
| | 33 | # Adds outgoing headers to a mime type. This can be done with the Merb.add_mime_type method |
|---|
| | 34 | # or directly here. |
|---|
| | 35 | # ===Example |
|---|
| | 36 | # {{[ |
|---|
| | 37 | # Merb.outgoing_headers!(:xml => { :Encoding => "UTF-8" }) |
|---|
| | 38 | # ]}} |
|---|
| | 39 | # |
|---|
| | 40 | # This method is destructive on any already defined outgoing headers |
|---|
| | 41 | def add_response_headers!(key, values = {}) |
|---|
| | 42 | raise ArgumentError unless key.is_a?(Symbol) && values.is_a?(Hash) |
|---|
| | 43 | response_headers[key] = values |
|---|
| | 44 | end |
|---|
| | 45 | |
|---|
| | 46 | def response_headers |
|---|
| | 47 | ResponderMixin::Rest::RESPONSE_HEADERS |
|---|
| | 48 | end |
|---|
| | 49 | |
|---|
| | 50 | # Completely removes any headers set that are additional to the content-type header. |
|---|
| | 51 | def remove_response_headers!(key) |
|---|
| | 52 | raise ArgumentError unless key.is_a?(Symbol) |
|---|
| | 53 | response_headers[key] = {} |
|---|
| | 54 | end |
|---|
| | 55 | |
|---|
| | 56 | # Sets the mime types and outgoing headers to their original states |
|---|
| | 57 | def reset_default_mime_types! |
|---|
| | 58 | available_mime_types.clear |
|---|
| | 59 | response_headers.clear |
|---|
| | 60 | Merb.add_mime_type(:all,nil,%w[*/*]) |
|---|
| | 61 | Merb.add_mime_type(:yaml,:to_yaml,%w[application/x-yaml text/yaml]) |
|---|
| | 62 | Merb.add_mime_type(:text,:to_text,%w[text/plain]) |
|---|
| | 63 | Merb.add_mime_type(:html,nil,%w[text/html application/xhtml+xml application/html]) |
|---|
| | 64 | Merb.add_mime_type(:xml,:to_xml,%w[application/xml text/xml application/x-xml], :Encoding => "UTF-8") |
|---|
| | 65 | Merb.add_mime_type(:js,:to_json,%w[ text/javascript application/javascript application/x-javascript]) |
|---|
| | 66 | Merb.add_mime_type(:json,:to_json,%w[application/json text/x-json ]) |
|---|
| | 67 | end |
|---|
| 9 | | |
|---|
| 10 | | # Any specific outgoing headers should be included here. These are not |
|---|
| 11 | | # the content-type header but anything in addition to it. |
|---|
| 12 | | # +tranform_method+ should be set to a symbol of the method used to |
|---|
| 13 | | # transform a resource into this mime type. |
|---|
| 14 | | # For example for the :xml mime type an object might be transformed by |
|---|
| 15 | | # calling :to_xml, or for the :js mime type, :to_json. |
|---|
| 16 | | # If there is no transform method, use nil. |
|---|
| 17 | | def self.add_mime_type(key,transform_method, values,new_response_headers = {}) |
|---|
| 18 | | raise ArgumentError unless key.is_a?(Symbol) && values.is_a?(Array) |
|---|
| 19 | | ResponderMixin::Rest::TYPES.update(key => values) |
|---|
| 20 | | add_response_headers!(key, new_response_headers) |
|---|
| 21 | | ResponderMixin::Rest::TRANSFORM_METHODS.merge!(key => transform_method) |
|---|
| 22 | | end |
|---|
| 23 | | |
|---|
| 24 | | def self.remove_mime_type(key) |
|---|
| 25 | | key == :all ? false : ResponderMixin::Rest::TYPES.delete(key) |
|---|
| 26 | | end |
|---|
| 27 | | |
|---|
| 28 | | def self.mime_transform_method(key) |
|---|
| 29 | | ResponderMixin::Rest::TRANSFORM_METHODS[key] |
|---|
| 30 | | end |
|---|
| 31 | | |
|---|
| 32 | | |
|---|
| 33 | | # Adds outgoing headers to a mime type. This can be done with the Merb.add_mime_type method |
|---|
| 34 | | # or directly here. |
|---|
| 35 | | # ===Example |
|---|
| 36 | | # {{[ |
|---|
| 37 | | # Merb.outgoing_headers!(:xml => { :Encoding => "UTF-8" }) |
|---|
| 38 | | # ]}} |
|---|
| 39 | | # |
|---|
| 40 | | # This method is destructive on any already defined outgoing headers |
|---|
| 41 | | def self.add_response_headers!(key, values = {}) |
|---|
| 42 | | raise ArgumentError unless key.is_a?(Symbol) && values.is_a?(Hash) |
|---|
| 43 | | response_headers[key] = values |
|---|
| 44 | | end |
|---|
| 45 | | |
|---|
| 46 | | def self.response_headers |
|---|
| 47 | | ResponderMixin::Rest::RESPONSE_HEADERS |
|---|
| 48 | | end |
|---|
| 49 | | |
|---|
| 50 | | # Completely removes any headers set that are additional to the content-type header. |
|---|
| 51 | | def self.remove_response_headers!(key) |
|---|
| 52 | | raise ArgumentError unless key.is_a?(Symbol) |
|---|
| 53 | | response_headers[key] = {} |
|---|
| 54 | | end |
|---|
| 55 | | |
|---|
| 56 | | # Sets the mime types and outgoing headers to their original states |
|---|
| 57 | | def self.reset_default_mime_types! |
|---|
| 58 | | available_mime_types.clear |
|---|
| 59 | | response_headers.clear |
|---|
| 60 | | Merb.add_mime_type(:all,nil,%w[*/*]) |
|---|
| 61 | | Merb.add_mime_type(:yaml,:to_yaml,%w[application/x-yaml text/yaml]) |
|---|
| 62 | | Merb.add_mime_type(:text,:to_text,%w[text/plain]) |
|---|
| 63 | | Merb.add_mime_type(:html,nil,%w[text/html application/xhtml+xml application/html]) |
|---|
| 64 | | Merb.add_mime_type(:xml,:to_xml,%w[application/xml text/xml application/x-xml], :Encoding => "UTF-8") |
|---|
| 65 | | Merb.add_mime_type(:js,:to_json,%w[ text/javascript application/javascript application/x-javascript]) |
|---|
| 66 | | Merb.add_mime_type(:json,:to_json,%w[application/json text/x-json ]) |
|---|
| 67 | | end |
|---|
| 68 | | |
|---|