Class: AMEE::Data::ItemValue

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

Instance Attribute Summary collapse

Attributes inherited from Object

#path

Attributes inherited from Object

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

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.



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

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

Instance Attribute Details

#start_dateObject

Returns the value of attribute start_date.



41
42
43
# File 'lib/amee/data_item_value.rb', line 41

def start_date
  @start_date
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

#uidObject

Returns the value of attribute uid.



42
43
44
# File 'lib/amee/data_item_value.rb', line 42

def uid
  @uid
end

Class Method Details

.create(data_item, options = {}) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/amee/data_item_value.rb', line 160

def self.create(data_item, options = {})

  # Do we want to automatically fetch the item afterwards?
  get_item = options.delete(:get_item)
  get_item = true if get_item.nil?
  # Store format if set
  format = options[:format]
  unless options.is_a?(Hash)
    raise AMEE::ArgumentError.new("Third argument must be a hash of options!")
  end

  # Set startDate
  if (options[:start_date])
    options[:startDate] = options[:start_date].xmlschema
    options.delete(:start_date)
  end

  response = data_item.connection.post(data_item.full_path, options)
  location = response['Location'].match("https??://.*?(/.*)")[1]
  if get_item == true
    get_options = {}
    get_options[:format] = format if format
    return AMEE::Data::ItemValue.get(data_item.connection, location)
  else
    return location
  end
rescue
  raise AMEE::BadData.new("Couldn't create DataItemValue. Check that your information is correct.")
end

.delete(connection, path) ⇒ Object



217
218
219
220
221
# File 'lib/amee/data_item_value.rb', line 217

def self.delete(connection, path)
  connection.delete(path)
  rescue
   raise AMEE::BadData.new("Couldn't delete DataItemValue. Check that your information is correct.")
end

.from_json(json, path) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/amee/data_item_value.rb', line 57

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

.from_xml(xml, path) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/amee/data_item_value.rb', line 79

def self.from_xml(xml, path)
  # Read XML
  @doc = xml.is_a?(String) ? load_xml_doc(xml) : xml
  data = {}
  if @doc.xpath("descendant-or-self::ItemValue").length>1
    raise AMEE::BadData.new("Couldn't load DataItemValue from XML. This is an item value history.\n#{xml}")
  end
  raise if @doc.xpath("descendant-or-self::ItemValue").length==0
  begin
    data[:uid] = x "@uid"
    data[:created] = DateTime.parse(x "@Created") rescue nil
    data[:modified] = DateTime.parse(x "@Modified") rescue nil
    data[:name] = x 'Name'
    data[:path] = path.gsub(/^\/data/, '')
    data[:value] = x 'Value'
    data[:type] = x 'ItemValueDefinition/ValueDefinition/ValueType'
    data[:from_profile] =  false
    data[:from_data] = true
    data[:start_date] = DateTime.parse(x "StartDate") rescue nil
    # Create object
    ItemValue.new(data)
  rescue
    raise AMEE::BadData.new("Couldn't load DataItemValue from XML. Check that your URL is correct.\n#{xml}")
  end
end

.get(connection, path) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/amee/data_item_value.rb', line 105

def self.get(connection, path)
  # Load data from path
  response = connection.get(path).body
  # Parse data from response
  data = {}
  value = ItemValue.parse(connection, response, path)
  # Done
  return value
rescue
  raise AMEE::BadData.new("Couldn't load DataItemValue. Check that your URL is correct.")
end

.parse(connection, response, path) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/amee/data_item_value.rb', line 146

def self.parse(connection, response, path)
  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 DataItemValue. Check that your URL is correct.\n#{response}")
end

.update(connection, path, options = {}) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/amee/data_item_value.rb', line 190

def self.update(connection, path, options = {})
  # Do we want to automatically fetch the item afterwards?
  get_item = options.delete(:get_item)
  get_item = true if get_item.nil?
  # Set startDate
  if (options[:start_date])
    options[:startDate] = options[:start_date].xmlschema if options[:start_date]!=Epoch
    options.delete(:start_date)
  end
  # Go
  response = connection.put(path, options)
  if get_item
    if response.body.empty?
      return AMEE::Data::ItemValue.get(connection, path)
    else
      return AMEE::Data::ItemValue.parse(connection, response.body)
    end
  end
rescue
  raise AMEE::BadData.new("Couldn't update DataItemValue. Check that your information is correct.\n#{response}")
end

.xmlpathpreambleObject



75
76
77
# File 'lib/amee/data_item_value.rb', line 75

def self.xmlpathpreamble
  "descendant-or-self::ItemValue/"
end

Instance Method Details

#create!Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/amee/data_item_value.rb', line 131

def create!
  data_item_path=path.split(/\//)[0..-2].join('/')
  data_item=AMEE::Data::Item.new
  data_item.path=data_item_path
  data_item.connection=connection
  data_item.connection or raise "No connection to AMEE available"
  if start_date
    ItemValue.create(data_item,:start_date=>start_date,
      @path.split(/\//).pop.to_sym => value,:get_item=>false)
  else
    ItemValue.create(data_item,
      @path.split(/\//).pop.to_sym => value,:get_item=>false)
  end
end

#delete!Object

Raises:



126
127
128
129
# File 'lib/amee/data_item_value.rb', line 126

def delete!
  raise AMEE::BadData.new("Cannot delete initial value for time series") if start_date==AMEE::Epoch
  ItemValue.delete @connection,full_uid_path
end

#from_data?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/amee/data_item_value.rb', line 37

def from_data?
  @from_data
end

#from_profile?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/amee/data_item_value.rb', line 33

def from_profile?
  @from_profile
end

#full_uid_pathObject



53
54
55
# File 'lib/amee/data_item_value.rb', line 53

def full_uid_path
  "/data#{uid_path}"
end

#save!Object



117
118
119
120
121
122
123
124
# File 'lib/amee/data_item_value.rb', line 117

def save!
  if start_date
    ItemValue.update(connection,full_uid_path,:value=>value,
      :start_date=>start_date,:get_item=>false)
  else
    ItemValue.update(connection,full_uid_path,:value=>value,:get_item=>false)
  end
end

#uid_pathObject



44
45
46
47
48
49
50
51
# File 'lib/amee/data_item_value.rb', line 44

def uid_path
  # create a path which is safe for DIVHs by using the UID if one is avai
  if uid
    @path.split(/\//)[0..-2].push(uid).join('/')
  else
    @path
  end
end

#update(options = {}) ⇒ Object



212
213
214
# File 'lib/amee/data_item_value.rb', line 212

def update(options = {})
  AMEE::Data::ItemValue.update(connection, full_path, options)
end

#valueObject



20
21
22
23
24
25
26
27
# File 'lib/amee/data_item_value.rb', line 20

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

#value=(val) ⇒ Object



29
30
31
# File 'lib/amee/data_item_value.rb', line 29

def value=(val)
  @value = val
end