Class: OpenSRF::ClassAndData

Inherits:
Object
  • Object
show all
Defined in:
lib/opensrf/class_and_data.rb

Overview

OpenSRF requests and responses often take the following form “clasname”, “__p”: [“all”, “the”, “data”]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass:, data:) ⇒ ClassAndData

Returns a new instance of ClassAndData.



10
11
12
13
# File 'lib/opensrf/class_and_data.rb', line 10

def initialize(klass:, data:)
  @klass = klass
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/opensrf/class_and_data.rb', line 8

def data
  @data
end

#klassObject (readonly)

Returns the value of attribute klass.



8
9
10
# File 'lib/opensrf/class_and_data.rb', line 8

def klass
  @klass
end

Class Method Details

.parse(hash, path = []) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/opensrf/class_and_data.rb', line 15

def self.parse(hash, path = [])
  small_json = new(klass: hash['__c'], data: hash['__p'])
  return small_json if path.empty?

  key = path.shift
  parse(small_json.data[key], path)
end

Instance Method Details

#to_hObject



23
24
25
26
# File 'lib/opensrf/class_and_data.rb', line 23

def to_h
  { '__c' => @klass,
    '__p' => @data }
end