Class: AMEE::Data::Item
Instance Attribute Summary collapse
-
#amounts ⇒ Object
readonly
Returns the value of attribute amounts.
-
#category_uid ⇒ Object
readonly
Returns the value of attribute category_uid.
-
#choices ⇒ Object
readonly
Returns the value of attribute choices.
-
#item_definition_uid ⇒ Object
readonly
Returns the value of attribute item_definition_uid.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#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, options = {}) ⇒ Object
- .create_batch_without_category(connection, category_path, items, options = {}) ⇒ Object
- .create_without_category(connection, path, options = {}) ⇒ Object
- .delete(connection, path) ⇒ Object
- .from_json(json) ⇒ Object
- .from_xml(xml) ⇒ Object
- .get(connection, path, options = {}) ⇒ Object
- .parse(connection, response) ⇒ Object
- .update(connection, path, options = {}) ⇒ Object
Instance Method Summary collapse
-
#initialize(data = {}) ⇒ Item
constructor
A new instance of Item.
- #item_definition ⇒ Object
- #update(options = {}) ⇒ Object
- #value(name_or_path_or_uid) ⇒ Object
Methods inherited from Object
Methods inherited from Object
Methods included from ParseHelper
#load_xml_doc, #node_value, #xmlpathpreamble
Constructor Details
#initialize(data = {}) ⇒ Item
Returns a new instance of Item.
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/amee/data_item.rb', line 10 def initialize(data = {}) @values = data[:values] @choices = data[:choices] @label = data[:label] @item_definition_uid = data[:item_definition] @total_amount = data[:total_amount] @total_amount_unit = data[:total_amount_unit] @amounts = data[:amounts] || [] @notes = data[:notes] || [] @start_date = data[:start_date] @category_uid = data[:category_uid] super end |
Instance Attribute Details
#amounts ⇒ Object (readonly)
Returns the value of attribute amounts.
29 30 31 |
# File 'lib/amee/data_item.rb', line 29 def amounts @amounts end |
#category_uid ⇒ Object (readonly)
Returns the value of attribute category_uid.
32 33 34 |
# File 'lib/amee/data_item.rb', line 32 def category_uid @category_uid end |
#choices ⇒ Object (readonly)
Returns the value of attribute choices.
25 26 27 |
# File 'lib/amee/data_item.rb', line 25 def choices @choices end |
#item_definition_uid ⇒ Object (readonly)
Returns the value of attribute item_definition_uid.
33 34 35 |
# File 'lib/amee/data_item.rb', line 33 def item_definition_uid @item_definition_uid end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
26 27 28 |
# File 'lib/amee/data_item.rb', line 26 def label @label end |
#notes ⇒ Object (readonly)
Returns the value of attribute notes.
30 31 32 |
# File 'lib/amee/data_item.rb', line 30 def notes @notes end |
#start_date ⇒ Object (readonly)
Returns the value of attribute start_date.
31 32 33 |
# File 'lib/amee/data_item.rb', line 31 def start_date @start_date end |
#total_amount ⇒ Object (readonly)
Returns the value of attribute total_amount.
27 28 29 |
# File 'lib/amee/data_item.rb', line 27 def total_amount @total_amount end |
#total_amount_unit ⇒ Object (readonly)
Returns the value of attribute total_amount_unit.
28 29 30 |
# File 'lib/amee/data_item.rb', line 28 def total_amount_unit @total_amount_unit end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
24 25 26 |
# File 'lib/amee/data_item.rb', line 24 def values @values end |
Class Method Details
.create(category, options = {}) ⇒ Object
217 218 219 |
# File 'lib/amee/data_item.rb', line 217 def self.create(category, = {}) create_without_category(category.connection, category.full_path, ) end |
.create_batch_without_category(connection, category_path, items, options = {}) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/amee/data_item.rb', line 199 def self.create_batch_without_category(connection, category_path, items, = {}) if connection.format == :json .merge! :profileItems => items post_data = .to_json else .merge!({:DataItems => items}) post_data = .to_xml(:root => "DataCategory", :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::Data::Category.parse(connection, response) else return true end end |
.create_without_category(connection, path, options = {}) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/amee/data_item.rb', line 221 def self.create_without_category(connection, path, = {}) # 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 # Create a data item! [:newObjectType] = "DI" # Send data to path response = connection.post(path, ) if response.headers_hash.has_key?('Location') && response.headers_hash['Location'] location = response.headers_hash['Location'].match("https??://.*?(/.*)")[1] else category = Category.parse(connection, response.body) location = category.full_path + "/" + category.items[0][:path] end if get_item == true = {} [:format] = format if format return AMEE::Data::Item.get(connection, location, ) else return location end rescue raise AMEE::BadData.new("Couldn't create DataItem. Check that your information is correct.\n#{response}") end |
.delete(connection, path) ⇒ Object
272 273 274 275 276 |
# File 'lib/amee/data_item.rb', line 272 def self.delete(connection, path) connection.delete(path) rescue raise AMEE::BadData.new("Couldn't delete DataItem. Check that your information is correct.") end |
.from_json(json) ⇒ Object
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 67 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 |
# File 'lib/amee/data_item.rb', line 41 def self.from_json(json) # Read JSON doc = JSON.parse(json) begin data = {} data[:uid] = doc['dataItem']['uid'] data[:created] = DateTime.parse(doc['dataItem']['created']) data[:modified] = DateTime.parse(doc['dataItem']['modified']) data[:name] = doc['dataItem']['name'] data[:path] = doc['path'] data[:label] = doc['dataItem']['label'] data[:item_definition] = doc['dataItem']['itemDefinition']['uid'] data[:category_uid] = doc['dataItem']['dataCategory']['uid'] # Read v2 total data[:total_amount] = doc['amount']['value'] rescue nil data[:total_amount_unit] = doc['amount']['unit'] rescue nil # Read v1 total if data[:total_amount].nil? data[:total_amount] = doc['amountPerMonth'] rescue nil data[:total_amount_unit] = "kg/month" end # Read amounts if doc['amounts'] if doc['amounts']['amount'] data[:amounts] = doc['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['amounts']['note'] data[:notes] = doc['amounts']['note'].map do |item| { :type => item['type'], :value => item['value'], } end end end # Get values data[:values] = [] doc['dataItem']['itemValues'].each do |value| value_data = {} value_data[:name] = value['name'] value_data[:path] = value['path'] value_data[:value] = value['value'] value_data[:drill] = value['itemValueDefinition']['drillDown'] rescue nil value_data[:uid] = value['uid'] data[:values] << value_data end # Get choices data[:choices] = [] doc['userValueChoices']['choices'].each do |choice| choice_data = {} choice_data[:name] = choice['name'] choice_data[:value] = choice['value'] data[:choices] << choice_data end data[:start_date] = DateTime.parse(doc['dataItem']['startDate']) rescue nil # Create object Item.new(data) rescue raise AMEE::BadData.new("Couldn't load DataItem from JSON. Check that your URL is correct.\n#{json}") end end |
.from_xml(xml) ⇒ Object
111 112 113 114 115 116 117 118 119 120 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/amee/data_item.rb', line 111 def self.from_xml(xml) # Parse data from response into hash doc = REXML::Document.new(xml) begin data = {} data[:uid] = REXML::XPath.first(doc, "/Resources/DataItemResource/DataItem/@uid").to_s data[:created] = DateTime.parse(REXML::XPath.first(doc, "/Resources/DataItemResource/DataItem/@created").to_s) data[:modified] = DateTime.parse(REXML::XPath.first(doc, "/Resources/DataItemResource/DataItem/@modified").to_s) data[:name] = (REXML::XPath.first(doc, '/Resources/DataItemResource/DataItem/Name') || REXML::XPath.first(doc, '/Resources/DataItemResource/DataItem/name')).text data[:path] = (REXML::XPath.first(doc, '/Resources/DataItemResource/Path') || REXML::XPath.first(doc, '/Resources/DataItemResource/DataItem/path')).text data[:label] = (REXML::XPath.first(doc, '/Resources/DataItemResource/DataItem/Label') || REXML::XPath.first(doc, '/Resources/DataItemResource/DataItem/label')).text data[:item_definition] = REXML::XPath.first(doc, '/Resources/DataItemResource/DataItem/ItemDefinition/@uid').to_s data[:category_uid] = REXML::XPath.first(doc, '/Resources/DataItemResource/DataItem/DataCategory/@uid').to_s # Read v2 total data[:total_amount] = REXML::XPath.first(doc, '/Resources/DataItemResource/Amount').text.to_f rescue nil data[:total_amount_unit] = REXML::XPath.first(doc, '/Resources/DataItemResource/Amount/@unit').to_s rescue nil # Read v1 total if data[:total_amount].nil? data[:total_amount] = REXML::XPath.first(doc, '/Resources/DataItemResource/AmountPerMonth').text.to_f rescue nil data[:total_amount_unit] = "kg/month" end data[:amounts] = [] REXML::XPath.each(doc,'/Resources/DataItemResource/Amounts/Amount') do |amount| amount_data = {} amount_data[:type] = amount.attribute('type').value amount_data[:value] = amount.text.to_f amount_data[:unit] = amount.attribute('unit').value amount_data[:per_unit] = amount.attribute('perUnit').value if amount.attribute('perUnit') amount_data[:default] = (amount.attribute('default').value == 'true') if amount.attribute('default') data[:amounts] << amount_data end data[:notes] = [] REXML::XPath.each(doc,'/Resources/DataItemResource/Amounts/Note') do |note| note_data = {} note_data[:type] = note.attribute('type').value note_data[:value] = note.text data[:notes] << note_data end # Get values data[:values] = [] REXML::XPath.each(doc, '/Resources/DataItemResource/DataItem/ItemValues/ItemValue') do |value| value_data = {} value_data[:name] = (value.elements['Name'] || value.elements['name']).text value_data[:path] = (value.elements['Path'] || value.elements['path']).text value_data[:value] = (value.elements['Value'] || value.elements['value']).text value_data[:drill] = value.elements['ItemValueDefinition'].elements['DrillDown'].text == "false" ? false : true rescue nil value_data[:uid] = value.attributes['uid'].to_s data[:values] << value_data end # Get choices data[:choices] = [] REXML::XPath.each(doc, '/Resources/DataItemResource/Choices/Choices/Choice') do |choice| choice_data = {} choice_data[:name] = (choice.elements['Name']).text choice_data[:value] = (choice.elements['Value']).text || "" data[:choices] << choice_data end data[:start_date] = DateTime.parse(REXML::XPath.first(doc, "/Resources/DataItemResource/DataItem/StartDate").text) rescue nil # Create object Item.new(data) rescue raise AMEE::BadData.new("Couldn't load DataItem from XML. Check that your URL is correct.\n#{xml}") end end |
.get(connection, path, options = {}) ⇒ Object
177 178 179 180 181 182 183 184 |
# File 'lib/amee/data_item.rb', line 177 def self.get(connection, path, = {}) # Load data from path item= get_and_parse(connection, path, ) # Store connection in object for future use item.connection = connection # Done return item end |
.parse(connection, response) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/amee/data_item.rb', line 186 def self.parse(connection, response) # Parse data from response if response.is_json? item = Item.from_json(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
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/amee/data_item.rb', line 251 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? # 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 DataItem. Check that your information is correct.\n#{response}") end |
Instance Method Details
#item_definition ⇒ Object
35 36 37 38 |
# File 'lib/amee/data_item.rb', line 35 def item_definition return nil unless item_definition_uid @item_definition ||= AMEE::Admin::ItemDefinition.load(connection,item_definition_uid) end |
#update(options = {}) ⇒ Object
268 269 270 |
# File 'lib/amee/data_item.rb', line 268 def update( = {}) AMEE::Data::Item.update(connection, full_path, ) end |
#value(name_or_path_or_uid) ⇒ Object
278 279 280 281 |
# File 'lib/amee/data_item.rb', line 278 def value(name_or_path_or_uid) val = values.find{ |x| x[:name] == name_or_path_or_uid || x[:path] == name_or_path_or_uid || x[:uid] == name_or_path_or_uid} val ? val[:value] : nil end |