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

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.

Parameters:

  • hash (Hash)

    the data hash of key-value pairs representing a remote resource to be converted to the specified encoding format.

  • options (defaults to: nil)

    options may be applicable depending on the format.

Returns:

  • (String)

    representation of the remote resource



33
34
35
# File 'lib/badgeville_berlin/formats/badgeville_berlin_json_format.rb', line 33

def encode(hash, options = nil)
  ActiveSupport::JSON.encode(hash, options)
end

#extensionString

Returns the extension ‘json’ to be added to the HTTP request URL for JSON endpoints.

Returns:

  • (String)

    the URL extension ‘json’



14
15
16
# File 'lib/badgeville_berlin/formats/badgeville_berlin_json_format.rb', line 14

def extension
  "json"
end

#mime_typeString

Returns the mime_type.

Returns:

  • (String)

    the MIME type for JSON



21
22
23
# File 'lib/badgeville_berlin/formats/badgeville_berlin_json_format.rb', line 21

def mime_type
  "application/json"
end