Module: BadgevilleBerlinJsonFormat
- Extended by:
- BadgevilleBerlinJsonFormat
- Included in:
- BadgevilleBerlinJsonFormat
- Defined in:
- lib/badgeville_berlin/formats/badgeville_berlin_json_format.rb
Overview
Handles the fact that a JSON formatted GET response does not meet the ActiveResource standard, and is instead preceded by the root key :data.
Instance Method Summary collapse
-
#decode(json) ⇒ Object
Converts a serialized string representation of a remote resource into a Ruby object, whether or not it has a root key :data.
-
#encode(hash, options = nil) ⇒ String
Identical to ActiveResource::Format::JsonFormat.encode.
-
#extension ⇒ String
Returns the extension ‘json’ to be added to the HTTP request URL for JSON endpoints.
-
#mime_type ⇒ String
Returns the mime_type.
Instance Method Details
#decode(json) ⇒ Object
Converts a serialized string representation of a remote resource into a Ruby object, whether or not it has a root key :data.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/badgeville_berlin/formats/badgeville_berlin_json_format.rb', line 39 def decode(json) return unless json json = ActiveResource::Formats.remove_root(ActiveSupport::JSON.decode(json)) if json.kind_of?(Array) json elsif json.kind_of?(Hash) json = ActiveSupport::HashWithIndifferentAccess.new(json) json.keys.first == "data" ? json["data"] : json end end |
#encode(hash, options = nil) ⇒ String
Identical to ActiveResource::Format::JsonFormat.encode. Returns the serialized string representation of the remote resource in the specified format (i.e. BadgevilleBerlinJsonFormat). Options depend on the configured format.
33 34 35 |
# File 'lib/badgeville_berlin/formats/badgeville_berlin_json_format.rb', line 33 def encode(hash, = nil) ActiveSupport::JSON.encode(hash, ) end |
#extension ⇒ String
Returns the extension ‘json’ to be added to the HTTP request URL for JSON endpoints.
14 15 16 |
# File 'lib/badgeville_berlin/formats/badgeville_berlin_json_format.rb', line 14 def extension "json" end |
#mime_type ⇒ String
Returns the mime_type.
21 22 23 |
# File 'lib/badgeville_berlin/formats/badgeville_berlin_json_format.rb', line 21 def mime_type "application/json" end |