Class: Distelli::JsonMarshaller
- Inherits:
-
Marshaller
- Object
- Marshaller
- Distelli::JsonMarshaller
- Defined in:
- lib/distelli/servicemarshallers.rb
Overview
JSON Marshaller
Instance Method Summary collapse
- #encode(obj) ⇒ Object
-
#initialize ⇒ JsonMarshaller
constructor
A new instance of JsonMarshaller.
- #marshall(request) ⇒ Object
- #marshall_error(error) ⇒ Object
- #unmarshall(json_data) ⇒ Object
- #unmarshall_error(json_data) ⇒ Object
Methods inherited from Marshaller
Constructor Details
#initialize ⇒ JsonMarshaller
Returns a new instance of JsonMarshaller.
28 29 30 |
# File 'lib/distelli/servicemarshallers.rb', line 28 def initialize() super end |
Instance Method Details
#encode(obj) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/distelli/servicemarshallers.rb', line 32 def encode(obj) data = Hash.new if obj.is_a?(Integer) or obj.is_a?(Float) or obj.is_a?(String) or obj.is_a?(TrueClass) or obj.is_a?(FalseClass) return obj elsif obj.is_a?(DateTime) return obj.strftime("%Y-%m-%dT%H:%M:%S%z") end type_map_name = '_'+obj.class.name.split('::').last+'__type_map' obj.instance_variables.each do |var| var_name = var.to_s.delete('@') if var_name == type_map_name next end val = obj.instance_variable_get(var) if val.is_a?(Integer) or val.is_a?(Float) or val.is_a?(String) or val.is_a?(TrueClass) or val.is_a?(FalseClass) data[var_name] = val elsif val.is_a?(DateTime) data[var_name] = val.strftime("%Y-%m-%dT%H:%M:%S%z") elsif val.instance_of?(Array) array = Array.new val.each do |elem| encoded = encode(elem) array.push(encoded) end data[var_name] = array else val.kind_of?(Object) encoded = encode(val) data[var_name] = encoded end end return data end |
#marshall(request) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/distelli/servicemarshallers.rb', line 66 def marshall(request) data = encode(request) request_wrapper = Hash.new request_wrapper[request.class.name.split('::').last] = data return request_wrapper.to_json end |
#marshall_error(error) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/distelli/servicemarshallers.rb', line 73 def marshall_error(error) if not error.is_a?(Distelli::BaseException) raise StandardError.new("Cannot marshall error: "+error.inspect) end err_msg = error.err_msg err_code = error.err_code return '{"Error":{"code":"'+err_code.to_s+'", "message":"'+err_msg.to_s+'"}}' end |
#unmarshall(json_data) ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/distelli/servicemarshallers.rb', line 94 def unmarshall(json_data) data_hash = JSON.load(json_data) data_hash.each_pair do |k,v| if v.is_a?(Hash) return unmarshall_obj(k, v) else raise MarshallException.new("Invalid data "+json_data) end end end |
#unmarshall_error(json_data) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/distelli/servicemarshallers.rb', line 83 def unmarshall_error(json_data) data_hash = JSON.load(json_data) err_obj = data_hash[ServiceConstants::ERROR_KEY] if err_obj == nil return nil end err_code = err_obj[ServiceConstants::ERR_CODE_KEY] err_msg = err_obj[ServiceConstants::ERR_MSG_KEY] return [err_code, err_msg] end |