Class: AMEE::Profile::Item
- Defined in:
- lib/amee/profile_item.rb
Instance Attribute Summary collapse
-
#amounts ⇒ Object
readonly
Returns the value of attribute amounts.
-
#data_item_uid ⇒ Object
readonly
Returns the value of attribute data_item_uid.
-
#end_date ⇒ Object
readonly
Returns the value of attribute end_date.
-
#notes ⇒ Object
readonly
Returns the value of attribute notes.
-
#start_date ⇒ Object
readonly
Returns the value of attribute start_date.
-
#total_amount ⇒ Object
readonly
Returns the value of attribute total_amount.
-
#total_amount_unit ⇒ Object
readonly
Returns the value of attribute total_amount_unit.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Attributes inherited from Object
Attributes inherited from Object
#connection, #created, #modified, #name, #path, #uid
Class Method Summary collapse
- .create(category, data_item_uid, options = {}) ⇒ Object
- .create_batch(category, items, options = {}) ⇒ Object
- .create_batch_without_category(connection, category_path, items, options = {}) ⇒ Object
- .create_without_category(connection, path, data_item_uid, options = {}) ⇒ Object
- .delete(connection, path) ⇒ Object
- .from_json(json) ⇒ Object
- .from_v2_atom(response) ⇒ Object
- .from_v2_json(json) ⇒ Object
- .from_v2_xml(xml) ⇒ Object
- .from_xml(xml) ⇒ Object
- .get(connection, path, options = {}) ⇒ Object
- .parse(connection, response) ⇒ Object
- .update(connection, path, options = {}) ⇒ Object
- .update_batch(category, items) ⇒ Object
- .update_batch_without_category(connection, category_path, items) ⇒ Object
Instance Method Summary collapse
- #duration ⇒ Object
- #end ⇒ Object
-
#initialize(data = {}) ⇒ Item
constructor
A new instance of Item.
- #update(options = {}) ⇒ Object
-
#valid_from ⇒ Object
V1 compatibility.
- #value(name_or_path) ⇒ Object
Methods inherited from Object
Methods included from ParseHelper
Constructor Details
#initialize(data = {}) ⇒ Item
Returns a new instance of Item.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/amee/profile_item.rb', line 5 def initialize(data = {}) @values = data ? data[:values] : [] @total_amount = data[:total_amount] @total_amount_unit = data[:total_amount_unit] @amounts = data[:amounts] || [] @notes = data[:notes] || [] @start_date = data[:start_date] || data[:valid_from] @end_date = data[:end_date] || (data[:end] == true ? @start_date : nil ) @data_item_uid = data[:data_item_uid] super end |
Instance Attribute Details
#amounts ⇒ Object (readonly)
Returns the value of attribute amounts.
20 21 22 |
# File 'lib/amee/profile_item.rb', line 20 def amounts @amounts end |
#data_item_uid ⇒ Object (readonly)
Returns the value of attribute data_item_uid.
24 25 26 |
# File 'lib/amee/profile_item.rb', line 24 def data_item_uid @data_item_uid end |
#end_date ⇒ Object (readonly)
Returns the value of attribute end_date.
23 24 25 |
# File 'lib/amee/profile_item.rb', line 23 def end_date @end_date end |
#notes ⇒ Object (readonly)
Returns the value of attribute notes.
21 22 23 |
# File 'lib/amee/profile_item.rb', line 21 def notes @notes end |
#start_date ⇒ Object (readonly)
Returns the value of attribute start_date.
22 23 24 |
# File 'lib/amee/profile_item.rb', line 22 def start_date @start_date end |
#total_amount ⇒ Object (readonly)
Returns the value of attribute total_amount.
18 19 20 |
# File 'lib/amee/profile_item.rb', line 18 def total_amount @total_amount end |
#total_amount_unit ⇒ Object (readonly)
Returns the value of attribute total_amount_unit.
19 20 21 |
# File 'lib/amee/profile_item.rb', line 19 def total_amount_unit @total_amount_unit end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
17 18 19 |
# File 'lib/amee/profile_item.rb', line 17 def values @values end |
Class Method Details
.create(category, data_item_uid, options = {}) ⇒ Object
278 279 280 |
# File 'lib/amee/profile_item.rb', line 278 def self.create(category, data_item_uid, = {}) create_without_category(category.connection, category.full_path, data_item_uid, ) end |
.create_batch(category, items, options = {}) ⇒ Object
325 326 327 |
# File 'lib/amee/profile_item.rb', line 325 def self.create_batch(category, items, = {}) create_batch_without_category(category.connection, category.full_path, items) end |
.create_batch_without_category(connection, category_path, items, options = {}) ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/amee/profile_item.rb', line 329 def self.create_batch_without_category(connection, category_path, items, = {}) if connection.format == :json .merge! :profileItems => items post_data = .to_json else .merge!({:ProfileItems => items}) post_data = .to_xml(:root => "ProfileCategory", :skip_types => true, :skip_nil => true) end # Post to category response = connection.raw_post(category_path, post_data).body # Send back a category object containing all the created items unless response.empty? return AMEE::Profile::Category.parse_batch(connection, response) else return true end end |
.create_without_category(connection, path, data_item_uid, options = {}) ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/amee/profile_item.rb', line 282 def self.create_without_category(connection, path, data_item_uid, = {}) # Do we want to automatically fetch the item afterwards? get_item = .delete(:get_item) get_item = true if get_item.nil? # Store format if set format = [:format] unless .is_a?(Hash) raise AMEE::ArgumentError.new("Third argument must be a hash of options!") end # Set dates if [:start_date] && connection.version < 2 [:validFrom] = [:start_date].amee1_date elsif [:start_date] && connection.version >= 2 [:startDate] = [:start_date].xmlschema end if [:end_date] && connection.version >= 2 [:endDate] = [:end_date].xmlschema end if [:duration] && connection.version >= 2 [:duration] = "PT#{[:duration] * 86400}S" end # Send data to path .merge! :dataItemUid => data_item_uid response = connection.post(path, ) if response['Location'] location = response['Location'].match("https??://.*?(/.*)")[1] else category = Category.parse(connection, response.body, nil) location = category.full_path + "/" + category.items[0][:path] end if get_item == true = {} [:returnUnit] = [:returnUnit] if [:returnUnit] [:returnPerUnit] = [:returnPerUnit] if [:returnPerUnit] [:format] = format if format return AMEE::Profile::Item.get(connection, location, ) else return location end rescue raise AMEE::BadData.new("Couldn't create ProfileItem. Check that your information is correct.\n#{response}") end |
.delete(connection, path) ⇒ Object
402 403 404 405 406 |
# File 'lib/amee/profile_item.rb', line 402 def self.delete(connection, path) connection.delete(path) rescue raise AMEE::BadData.new("Couldn't delete ProfileItem. Check that your information is correct.") end |
.from_json(json) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/amee/profile_item.rb', line 38 def self.from_json(json) # Parse json doc = JSON.parse(json) data = {} data[:profile_uid] = doc['profile']['uid'] data[:data_item_uid] = doc['profileItem']['dataItem']['uid'] data[:uid] = doc['profileItem']['uid'] data[:name] = doc['profileItem']['name'] data[:path] = doc['path'] data[:total_amount] = doc['profileItem']['amountPerMonth'] data[:total_amount_unit] = "kg/month" data[:valid_from] = DateTime.strptime(doc['profileItem']['validFrom'], "%Y%m%d") data[:end] = doc['profileItem']['end'] == "false" ? false : true data[:values] = [] doc['profileItem']['itemValues'].each do |item| value_data = {} item.each_pair do |key,value| case key when 'name', 'path', 'uid', 'value' value_data[key.downcase.to_sym] = value end end data[:values] << value_data end # Create object Item.new(data) rescue raise AMEE::BadData.new("Couldn't load ProfileItem from JSON data. Check that your URL is correct.\n#{json}") end |
.from_v2_atom(response) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/amee/profile_item.rb', line 205 def self.from_v2_atom(response) # Parse XML doc = REXML::Document.new(response) data = {} data[:profile_uid] = REXML::XPath.first(doc, "/entry/@xml:base").to_s.match("/profiles/(.*?)/")[1] #data[:data_item_uid] = REXML::XPath.first(doc, "/Resources/ProfileItemResource/DataItem/@uid").to_s data[:uid] = REXML::XPath.first(doc, "/entry/id").text.match("urn:item:(.*)")[1] data[:name] = REXML::XPath.first(doc, '/entry/title').text data[:path] = REXML::XPath.first(doc, "/entry/@xml:base").to_s.match("/profiles/.*?(/.*)")[1] data[:total_amount] = REXML::XPath.first(doc, '/entry/amee:amount').text.to_f rescue nil data[:total_amount_unit] = REXML::XPath.first(doc, '/entry/amee:amount/@unit').to_s rescue nil data[:start_date] = DateTime.parse(REXML::XPath.first(doc, "/entry/amee:startDate").text) data[:end_date] = DateTime.parse(REXML::XPath.first(doc, "/entry/amee:endDate").text) rescue nil data[:values] = [] REXML::XPath.each(doc, '/entry/amee:itemValue') do |item| value_data = {} value_data[:name] = item.elements['amee:name'].text value_data[:value] = item.elements['amee:value'].text unless item.elements['amee:value'].text == "N/A" value_data[:path] = item.elements['link'].attributes['href'].to_s value_data[:unit] = item.elements['amee:unit'].text rescue nil value_data[:per_unit] = item.elements['amee:perUnit'].text rescue nil data[:values] << value_data end # Create object Item.new(data) rescue raise AMEE::BadData.new("Couldn't load ProfileItem from V2 ATOM data. Check that your URL is correct.\n#{response}") end |
.from_v2_json(json) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/amee/profile_item.rb', line 68 def self.from_v2_json(json) # Parse json doc = JSON.parse(json) data = {} data[:profile_uid] = doc['profile']['uid'] data[:data_item_uid] = doc['profileItem']['dataItem']['uid'] data[:uid] = doc['profileItem']['uid'] data[:name] = doc['profileItem']['name'] data[:path] = doc['path'] data[:total_amount] = doc['profileItem']['amount']['value'].to_f data[:total_amount_unit] = doc['profileItem']['amount']['unit'] data[:start_date] = DateTime.parse(doc['profileItem']['startDate']) data[:end_date] = DateTime.parse(doc['profileItem']['endDate']) rescue nil data[:values] = [] doc['profileItem']['itemValues'].each do |item| value_data = {} item.each_pair do |key,value| case key when 'name', 'path', 'uid', 'value', 'unit' value_data[key.downcase.to_sym] = value when 'perUnit' value_data[:per_unit] = value end end data[:values] << value_data end if doc['profileItem']['amounts'] if doc['profileItem']['amounts']['amount'] data[:amounts] = doc['profileItem']['amounts']['amount'].map do |item| { :type => item['type'], :value => item['value'].to_f, :unit => item['unit'], :per_unit => item['perUnit'], :default => (item['default'] == 'true'), } end end if doc['profileItem']['amounts']['note'] data[:notes] = doc['profileItem']['amounts']['note'].map do |item| { :type => item['type'], :value => item['value'], } end end end # Create object Item.new(data) rescue raise AMEE::BadData.new("Couldn't load ProfileItem from V2 JSON data. Check that your URL is correct.\n#{json}") end |
.from_v2_xml(xml) ⇒ Object
154 155 156 157 158 159 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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/amee/profile_item.rb', line 154 def self.from_v2_xml(xml) # Parse XML doc = REXML::Document.new(xml) data = {} data[:profile_uid] = REXML::XPath.first(doc, "/Resources/ProfileItemResource/Profile/@uid").to_s data[:data_item_uid] = REXML::XPath.first(doc, "/Resources/ProfileItemResource/DataItem/@uid").to_s data[:uid] = REXML::XPath.first(doc, "/Resources/ProfileItemResource/ProfileItem/@uid").to_s data[:name] = REXML::XPath.first(doc, '/Resources/ProfileItemResource/ProfileItem/Name').text data[:path] = REXML::XPath.first(doc, '/Resources/ProfileItemResource/Path').text || "" data[:total_amount] = REXML::XPath.first(doc, '/Resources/ProfileItemResource/ProfileItem/Amount').text.to_f rescue nil data[:total_amount_unit] = REXML::XPath.first(doc, '/Resources/ProfileItemResource/ProfileItem/Amount/@unit').to_s rescue nil data[:start_date] = DateTime.parse(REXML::XPath.first(doc, "/Resources/ProfileItemResource/ProfileItem/StartDate").text) data[:end_date] = DateTime.parse(REXML::XPath.first(doc, "/Resources/ProfileItemResource/ProfileItem/EndDate").text) rescue nil data[:values] = [] REXML::XPath.each(doc, '/Resources/ProfileItemResource/ProfileItem/ItemValues/ItemValue') do |item| value_data = {} item.elements.each do |element| key = element.name value = element.text case key when 'Name', 'Path', 'Value', 'Unit' value_data[key.downcase.to_sym] = value when 'PerUnit' value_data[:per_unit] = value end end value_data[:uid] = item.attributes['uid'].to_s data[:values] << value_data end data[:amounts] = REXML::XPath.each(doc, '/Resources/ProfileItemResource/ProfileItem/Amounts/Amount').map do |item| x = { :type => item.attribute('type').value, :value => item.text.to_f, :unit => item.attribute('unit').value, } x[:per_unit] = item.attribute('perUnit').value if item.attribute('perUnit') x[:default] = (item.attribute('default').value == 'true') if item.attribute('default') x end data[:notes] = REXML::XPath.each(doc, '/Resources/ProfileItemResource/ProfileItem/Amounts/Note').map do |item| { :type => item.attribute('type').value, :value => item.text, } end # Create object Item.new(data) rescue raise AMEE::BadData.new("Couldn't load ProfileItem from V2 XML data. Check that your URL is correct.\n#{xml}") end |
.from_xml(xml) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/amee/profile_item.rb', line 121 def self.from_xml(xml) # Parse XML doc = REXML::Document.new(xml) data = {} data[:profile_uid] = REXML::XPath.first(doc, "/Resources/ProfileItemResource/Profile/@uid").to_s data[:data_item_uid] = REXML::XPath.first(doc, "/Resources/ProfileItemResource/DataItem/@uid").to_s data[:uid] = REXML::XPath.first(doc, "/Resources/ProfileItemResource/ProfileItem/@uid").to_s data[:name] = REXML::XPath.first(doc, '/Resources/ProfileItemResource/ProfileItem/Name').text data[:path] = REXML::XPath.first(doc, '/Resources/ProfileItemResource/Path').text || "" data[:total_amount] = REXML::XPath.first(doc, '/Resources/ProfileItemResource/ProfileItem/AmountPerMonth').text.to_f rescue nil data[:total_amount_unit] = "kg/month" data[:valid_from] = DateTime.strptime(REXML::XPath.first(doc, "/Resources/ProfileItemResource/ProfileItem/ValidFrom").text, "%Y%m%d") data[:end] = REXML::XPath.first(doc, '/Resources/ProfileItemResource/ProfileItem/End').text == "false" ? false : true data[:values] = [] REXML::XPath.each(doc, '/Resources/ProfileItemResource/ProfileItem/ItemValues/ItemValue') do |item| value_data = {} item.elements.each do |element| key = element.name value = element.text case key when 'Name', 'Path', 'Value' value_data[key.downcase.to_sym] = value end end value_data[:uid] = item.attributes['uid'].to_s data[:values] << value_data end # Create object Item.new(data) rescue raise AMEE::BadData.new("Couldn't load ProfileItem from XML data. Check that your URL is correct.\n#{xml}") end |
.get(connection, path, options = {}) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/amee/profile_item.rb', line 253 def self.get(connection, path, = {}) unless .is_a?(Hash) raise AMEE::ArgumentError.new("Third argument must be a hash of options!") end # Convert to AMEE options if [:start_date] && category.connection.version < 2 [:profileDate] = [:start_date].amee1_month elsif [:start_date] && category.connection.version >= 2 [:startDate] = [:start_date].xmlschema end .delete(:start_date) if [:end_date] && category.connection.version >= 2 [:endDate] = [:end_date].xmlschema end .delete(:end_date) if [:duration] && category.connection.version >= 2 [:duration] = "PT#{[:duration] * 86400}S" end # Load data from path response = connection.get(path, ).body return Item.parse(connection, response) rescue raise AMEE::BadData.new("Couldn't load ProfileItem. Check that your URL is correct.\n#{response}") end |
.parse(connection, response) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/amee/profile_item.rb', line 234 def self.parse(connection, response) # Parse data from response if response.is_v2_json? item = Item.from_v2_json(response) elsif response.is_json? item = Item.from_json(response) elsif response.is_v2_atom? item = Item.from_v2_atom(response) elsif response.is_v2_xml? item = Item.from_v2_xml(response) else item = Item.from_xml(response) end # Store connection in object for future use item.connection = connection # Done return item end |
.update(connection, path, options = {}) ⇒ Object
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/amee/profile_item.rb', line 347 def self.update(connection, path, = {}) # Do we want to automatically fetch the item afterwards? get_item = .delete(:get_item) get_item = true if get_item.nil? # Set dates if [:start_date] && connection.version < 2 [:validFrom] = [:start_date].amee1_date elsif [:start_date] && connection.version >= 2 [:startDate] = [:start_date].xmlschema end .delete(:start_date) if [:end_date] && connection.version >= 2 [:endDate] = [:end_date].xmlschema end .delete(:end_date) if [:duration] && connection.version >= 2 [:duration] = "PT#{[:duration] * 86400}S" end # Go response = connection.put(path, ) if get_item if response.body.empty? return Item.get(connection, path) else return Item.parse(connection, response.body) end end rescue raise AMEE::BadData.new("Couldn't update ProfileItem. Check that your information is correct.\n#{response}") end |
.update_batch(category, items) ⇒ Object
382 383 384 |
# File 'lib/amee/profile_item.rb', line 382 def self.update_batch(category, items) update_batch_without_category(category.connection, category.full_path, items) end |
.update_batch_without_category(connection, category_path, items) ⇒ Object
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/amee/profile_item.rb', line 386 def self.update_batch_without_category(connection, category_path, items) if connection.format == :json put_data = ({:profileItems => items}).to_json else put_data = ({:ProfileItems => items}).to_xml(:root => "ProfileCategory", :skip_types => true, :skip_nil => true) end # Post to category response = connection.raw_put(category_path, put_data).body # Send back a category object containing all the created items unless response.empty? return AMEE::Profile::Category.parse(connection, response, nil) else return true end end |
Instance Method Details
#duration ⇒ Object
34 35 36 |
# File 'lib/amee/profile_item.rb', line 34 def duration end_date.nil? ? nil : (end_date - start_date).to_f end |
#end ⇒ Object
30 31 32 |
# File 'lib/amee/profile_item.rb', line 30 def end end_date.nil? ? false : start_date == end_date end |
#update(options = {}) ⇒ Object
378 379 380 |
# File 'lib/amee/profile_item.rb', line 378 def update( = {}) AMEE::Profile::Item.update(connection, full_path, ) end |
#valid_from ⇒ Object
V1 compatibility
27 28 29 |
# File 'lib/amee/profile_item.rb', line 27 def valid_from start_date end |
#value(name_or_path) ⇒ Object
408 409 410 411 |
# File 'lib/amee/profile_item.rb', line 408 def value(name_or_path) val = values.find{ |x| x[:name] == name_or_path || x[:path] == name_or_path} val ? val[:value] : nil end |