Class: AMEE::Data::ItemValueHistory
- Inherits:
-
Object
- Object
- AMEE::Data::ItemValueHistory
- Defined in:
- lib/amee/data_item_value_history.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#path ⇒ Object
the IV path corresponding to the series, without the /data.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#values ⇒ Object
Returns the value of attribute values.
Class Method Summary collapse
- .from_json(json, path) ⇒ Object
- .from_xml(xml, path) ⇒ Object
- .get(connection, path) ⇒ Object
- .parse(connection, response, path) ⇒ Object
Instance Method Summary collapse
- #compare(origin) ⇒ Object
- #create! ⇒ Object
- #delete! ⇒ Object
- #full_path ⇒ Object
-
#initialize(data = {}) ⇒ ItemValueHistory
constructor
A new instance of ItemValueHistory.
- #save! ⇒ Object
- #series ⇒ Object
- #series=(newseries) ⇒ Object
- #times ⇒ Object
- #value_at(time) ⇒ Object
- #values_at(times) ⇒ Object
Constructor Details
#initialize(data = {}) ⇒ ItemValueHistory
Returns a new instance of ItemValueHistory.
7 8 9 10 11 12 13 |
# File 'lib/amee/data_item_value_history.rb', line 7 def initialize(data = {}) @type = data ? data[:type] : nil @path = data ? data[:path] : nil @connection = data ? data[:connection] : nil @values=data&&data[:values] ? data[:values] : [] self.series=data[:series] if data[:series] end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
64 65 66 |
# File 'lib/amee/data_item_value_history.rb', line 64 def connection @connection end |
#path ⇒ Object
the IV path corresponding to the series, without the /data
16 17 18 |
# File 'lib/amee/data_item_value_history.rb', line 16 def path @path end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
65 66 67 |
# File 'lib/amee/data_item_value_history.rb', line 65 def type @type end |
#values ⇒ Object
Returns the value of attribute values.
15 16 17 |
# File 'lib/amee/data_item_value_history.rb', line 15 def values @values end |
Class Method Details
.from_json(json, path) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/amee/data_item_value_history.rb', line 67 def self.from_json(json, path) # Read JSON data = {} data[:path] = path.gsub(/^\/data/, '') doc = JSON.parse(json)['itemValues'] doc=[JSON.parse(json)['itemValue']] unless doc data[:values]=doc.map do |json_item_value| ItemValue.from_json(json_item_value,path) end data[:type]=data[:values][0].type # Create object ItemValueHistory.new(data) rescue raise AMEE::BadData.new("Couldn't load DataItemValueHistory from JSON. Check that your URL is correct.\n#{json}") end |
.from_xml(xml, path) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/amee/data_item_value_history.rb', line 83 def self.from_xml(xml, path) # Read XML data = {} data[:path] = path.gsub(/^\/data/, '') doc = REXML::Document.new(xml) valuedocs=REXML::XPath.match(doc, '//ItemValue') raise if valuedocs.length==0 data[:values] = valuedocs.map do |xml_item_value| ItemValue.from_xml(xml_item_value,path) end data[:type]=data[:values][0].type # Create object ItemValueHistory.new(data) rescue raise AMEE::BadData.new("Couldn't load DataItemValueHistory from XML. Check that your URL is correct.\n#{xml}") end |
.get(connection, path) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/amee/data_item_value_history.rb', line 100 def self.get(connection, path) # Load data from path response = connection.get(path,:valuesPerPage=>2).body # Parse data from response data = {} value = ItemValueHistory.parse(connection, response, path) # Done return value rescue raise AMEE::BadData.new("Couldn't load DataItemValueHistory. Check that your URL is correct.") end |
.parse(connection, response, path) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/amee/data_item_value_history.rb', line 141 def self.parse(connection, response, path) if response.is_json? history = ItemValueHistory.from_json(response, path) else history = ItemValueHistory.from_xml(response, path) end # Store connection in object for future use history.connection = connection # Done return history rescue raise AMEE::BadData.new("Couldn't load DataItemValueHistory. Check that your URL is correct.\n#{response}") end |
Instance Method Details
#compare(origin) ⇒ Object
155 156 157 158 159 160 161 162 163 |
# File 'lib/amee/data_item_value_history.rb', line 155 def compare(origin) new=Set.new(times) old=Set.new(origin.times) { :insertions=>values_at(new-old), :deletions=>origin.values_at(old-new), :updates=>values_at(old&new) } end |
#create! ⇒ Object
136 137 138 139 |
# File 'lib/amee/data_item_value_history.rb', line 136 def create! # deprecated, as DI cannot exist without at least one point raise AMEE::NotSupported.new("Cannot create a Data Item Value History from scratch: at least one data point must exist when the DI is created") end |
#delete! ⇒ Object
131 132 133 134 |
# File 'lib/amee/data_item_value_history.rb', line 131 def delete! # deprecated, as DI cannot exist without at least one point raise AMEE::NotSupported.new("Cannot delete all of history: at least one data point must always exist.") end |
#full_path ⇒ Object
18 19 20 |
# File 'lib/amee/data_item_value_history.rb', line 18 def full_path "/data#{path}" end |
#save! ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/amee/data_item_value_history.rb', line 112 def save! raise AMEE::BadData.new("Can't save without a path") unless @path raise AMEE::BadData.new("Can't save without a connection") unless @connection origin=ItemValueHistory.get(connection,full_path) changes=compare(origin) changes[:updates].each do |update| # we've decided to identify these, but the version in the thing to be # saved is probably home made, so copy over the uid update.uid=origin.value_at(update.start_date).uid update.save! end changes[:insertions].each do |insertion| insertion.create! end changes[:deletions].each do |deletion| deletion.delete! end end |
#series ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/amee/data_item_value_history.rb', line 22 def series values.map {|x| [x.start_date.utc,x.value] }.sort {|x,y| x[0]<=>y[0] } end |
#series=(newseries) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/amee/data_item_value_history.rb', line 36 def series=(newseries) raise AMEE::BadData.new("Series must have initial (Epoch) value") unless newseries.any?{|x| x[0]==Epoch} @values=newseries.map{|x| AMEE::Data::ItemValue.new(:value=>x[1], :start_date=>x[0], :path=>path, :connection=>connection, :type=>type ) } end |
#times ⇒ Object
30 31 32 33 34 |
# File 'lib/amee/data_item_value_history.rb', line 30 def times values.map {|x| x.start_date.utc } end |
#value_at(time) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/amee/data_item_value_history.rb', line 48 def value_at(time) selected=values.select{|x| x.start_date==time} raise AMEE::BadData.new("Multiple data item values matching one time.") if selected.length >1 raise AMEE::BadData.new("No data item value for that time #{time}.") if selected.length ==0 selected[0] end |
#values_at(times) ⇒ Object
55 56 57 |
# File 'lib/amee/data_item_value_history.rb', line 55 def values_at(times) times.map{|x| value_at(x)} end |