Class: AMEE::Profile::ItemValue

Inherits:
Object show all
Defined in:
lib/amee/profile_item_value.rb

Instance Attribute Summary collapse

Attributes inherited from Object

#profile_date, #profile_uid

Attributes inherited from Object

#connection, #created, #modified, #name, #path, #uid

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Object

#full_path

Methods inherited from Object

#expire_cache, get_and_parse

Methods included from ParseHelper

#load_xml_doc, #node_value, #xmlpathpreamble

Constructor Details

#initialize(data = {}) ⇒ ItemValue

Returns a new instance of ItemValue.



8
9
10
11
12
13
14
15
16
# File 'lib/amee/profile_item_value.rb', line 8

def initialize(data = {})
  @value = data ? data[:value] : nil
  @type = data ? data[:type] : nil
  @unit = data ? data[:unit] : nil
  @per_unit = data ? data[:per_unit] : nil
  @from_profile = data ? data[:from_profile] : nil
  @from_data = data ? data[:from_data] : nil
  super
end

Instance Attribute Details

#per_unitObject

Returns the value of attribute per_unit.



20
21
22
# File 'lib/amee/profile_item_value.rb', line 20

def per_unit
  @per_unit
end

#typeObject (readonly)

Returns the value of attribute type.



18
19
20
# File 'lib/amee/profile_item_value.rb', line 18

def type
  @type
end

#unitObject

Returns the value of attribute unit.



19
20
21
# File 'lib/amee/profile_item_value.rb', line 19

def unit
  @unit
end

Class Method Details

.from_json(json, path) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/amee/profile_item_value.rb', line 43

def self.from_json(json, path)
  # Read JSON
  doc = JSON.parse(json)['itemValue']
  data = {}
  data[:uid] = doc['uid']
  data[:created] = DateTime.parse(doc['created'])
  data[:modified] = DateTime.parse(doc['modified'])
  data[:name] = doc['name']
  data[:path] = path.gsub(/^\/profiles/, '')
  data[:value] = doc['value']
  data[:unit] = doc['unit']
  data[:per_unit] = doc['perUnit']
  data[:type] = doc['itemValueDefinition']['valueDefinition']['valueType']
  # Create object
  ItemValue.new(data)
rescue 
  raise AMEE::BadData.new("Couldn't load ProfileItemValue from JSON. Check that your URL is correct.\n#{json}")
end

.from_xml(xml, path) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/amee/profile_item_value.rb', line 62

def self.from_xml(xml, path)
  # Read XML
  doc = REXML::Document.new(xml)
  data = {}
  data[:uid] = REXML::XPath.first(doc, "/Resources/ProfileItemValueResource/ItemValue/@uid").to_s
  data[:created] = DateTime.parse(REXML::XPath.first(doc, "/Resources/ProfileItemValueResource/ItemValue/@Created").to_s)
  data[:modified] = DateTime.parse(REXML::XPath.first(doc, "/Resources/ProfileItemValueResource/ItemValue/@Modified").to_s)
  data[:name] = REXML::XPath.first(doc, '/Resources/ProfileItemValueResource/ItemValue/Name').text
  data[:path] = path.gsub(/^\/profiles/, '')
  data[:value] = REXML::XPath.first(doc, '/Resources/ProfileItemValueResource/ItemValue/Value').text
  data[:unit] = REXML::XPath.first(doc, '/Resources/ProfileItemValueResource/ItemValue/Unit').text rescue nil
  data[:per_unit] = REXML::XPath.first(doc, '/Resources/ProfileItemValueResource/ItemValue/PerUnit').text rescue nil
  data[:type] = REXML::XPath.first(doc, '/Resources/ProfileItemValueResource/ItemValue/ItemValueDefinition/ValueDefinition/ValueType').text
  data[:from_profile] = REXML::XPath.first(doc, '/Resources/ProfileItemValueResource/ItemValue/ItemValueDefinition/FromProfile').text == "true" ? true : false
  data[:from_data] = REXML::XPath.first(doc, '/Resources/ProfileItemValueResource/ItemValue/ItemValueDefinition/FromData').text == "true" ? true : false
  # Create object
  ItemValue.new(data)
rescue
  raise AMEE::BadData.new("Couldn't load ProfileItemValue from XML. Check that your URL is correct.\n#{xml}")
end

.get(connection, path) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/amee/profile_item_value.rb', line 83

def self.get(connection, path)
  # Load Profile from path
  response = connection.get(path).body
  # Parse data from response
  if response.is_json?
    value = ItemValue.from_json(response, path)
  else
    value = ItemValue.from_xml(response, path)
  end
  # Store connection in object for future use
  value.connection = connection
  # Done
  return value
rescue
  raise AMEE::BadData.new("Couldn't load ProfileItemValue. Check that your URL is correct.\n#{response}")
end

Instance Method Details

#from_data?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/amee/profile_item_value.rb', line 39

def from_data?
  @from_data
end

#from_profile?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/amee/profile_item_value.rb', line 35

def from_profile?
  @from_profile
end

#save!Object



100
101
102
103
104
105
# File 'lib/amee/profile_item_value.rb', line 100

def save!
  options = {:value => value}
  options[:unit] = unit if unit
  options[:perUnit] = per_unit if per_unit
  response = @connection.put(full_path, options).body
end

#valueObject



22
23
24
25
26
27
28
29
# File 'lib/amee/profile_item_value.rb', line 22

def value
  case type
  when "DECIMAL", "DOUBLE"
    @value.to_f
  else
    @value
  end
end

#value=(val) ⇒ Object



31
32
33
# File 'lib/amee/profile_item_value.rb', line 31

def value=(val)
  @value = val
end