class MSS::Core::Options::JSONSerializer

Given a hash of serialization rules, a JSONSerializer can convert a hash of request options into a JSON document. The request options are validated before returning JSON.

Attributes

namespace[R]

@return [String]

operation_name[R]

@return [String] Returns the name of the API operation.

rules[R]

@return [Hash]

Public Class Methods

new(rules, payload_param) click to toggle source

@param [Hash] rules A hash of option rules to validate against. @param [String,nil] payload_param

# File lib/mss/core/options/json_serializer.rb, line 27
def initialize rules, payload_param
  @payload_param = payload_param
  @rules = @payload_param ? rules[@payload_param][:members] : rules
end

Public Instance Methods

serialize(request_options) click to toggle source

@overload serialize!(request_options)

@param [Hash] request_options A hash of already validated
  request options with normalized values.
@return [String] Returns an string of the request parameters
  serialized into XML.
# File lib/mss/core/options/json_serializer.rb, line 46
def serialize request_options
  request_options = request_options[@payload_param] if @payload_param
  data = normalize_keys(request_options, rules)
  if rules.any?{|k,v| v[:location] == 'body' }
    data = data.values.first
  end
  JSON.pretty_generate(data)
end

Protected Instance Methods

normalize_keys(values, rules) click to toggle source
# File lib/mss/core/options/json_serializer.rb, line 57
def normalize_keys values, rules
  values.inject({}) do |h,(k,v)|
    child_rules = rules[k]
    child_name = child_rules[:name] || Inflection.class_name(k.to_s)
    h.merge(child_name => normalize_value(v, child_rules))
  end
end
normalize_value(value, rules) click to toggle source
# File lib/mss/core/options/json_serializer.rb, line 65
def normalize_value value, rules
  case rules[:type]
  when :hash then normalize_keys(value, rules[:members])
  when :array then value.map{|v| normalize_value(v, rules[:members]) }
  when :map
    value.inject({}) do |h,(k,v)|
      h.merge(k => normalize_value(v, rules[:members]))
    end
  when :blob then Base64.encode64(value.read).strip
  else value
  end
end