Class: RestApi::OpenshiftJsonFormat

Inherits:
Object
  • Object
show all
Includes:
ActiveResource::Formats::JsonFormat
Defined in:
app/models/rest_api/base.rb

Overview

The OpenShift REST API wraps the root resource element which must be unwrapped.

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ OpenshiftJsonFormat

Returns a new instance of OpenshiftJsonFormat.



85
86
87
# File 'app/models/rest_api/base.rb', line 85

def initialize(*args)
  @root_attrs = args
end

Instance Method Details

#decode(json) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/rest_api/base.rb', line 89

def decode(json)
  decoded = ActiveSupport::JSON.decode(json)
  if decoded.is_a?(Hash) and decoded.has_key?('data')
    attrs = root_attributes(decoded)
    decoded = decoded['data'] || {}
  end
  if decoded.is_a? Array
    decoded.map!{ |obj| delink(obj, attrs) }
  else
    delink(decoded, attrs)
  end
end


102
103
104
105
106
107
108
109
# File 'app/models/rest_api/base.rb', line 102

def delink(obj, attrs)
  obj = obj.values.first if obj.is_a?(Hash) && obj.length == 1 && obj.values.first.is_a?(Hash)
  if obj.is_a? Hash
    obj.delete 'links'
    obj.merge!(attrs) if attrs
  end
  obj
end

#root_attributes(hash) ⇒ Object



111
112
113
# File 'app/models/rest_api/base.rb', line 111

def root_attributes(hash)
  hash.slice('messages', *@root_attrs)
end