Class: AMEE::Data::Item
Instance Attribute Summary collapse
-
#choices ⇒ Object
readonly
Returns the value of attribute choices.
-
#item_definition ⇒ Object
readonly
Returns the value of attribute item_definition.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#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
#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.
- #update(options = {}) ⇒ Object
- #value(name_or_path_or_uid) ⇒ Object
Methods inherited from Object
Constructor Details
#initialize(data = {}) ⇒ Item
Returns a new instance of Item.
5 6 7 8 9 10 11 12 13 |
# File 'lib/amee/data_item.rb', line 5 def initialize(data = {}) @values = data[:values] @choices = data[:choices] @label = data[:label] @item_definition = data[:item_definition] @total_amount = data[:total_amount] @total_amount_unit = data[:total_amount_unit] super end |
Instance Attribute Details
#choices ⇒ Object (readonly)
Returns the value of attribute choices.
16 17 18 |
# File 'lib/amee/data_item.rb', line 16 def choices @choices end |
#item_definition ⇒ Object (readonly)
Returns the value of attribute item_definition.
18 19 20 |
# File 'lib/amee/data_item.rb', line 18 def item_definition @item_definition end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
17 18 19 |
# File 'lib/amee/data_item.rb', line 17 def label @label end |
#total_amount ⇒ Object (readonly)
Returns the value of attribute total_amount.
19 20 21 |
# File 'lib/amee/data_item.rb', line 19 def total_amount @total_amount end |
#total_amount_unit ⇒ Object (readonly)
Returns the value of attribute total_amount_unit.
20 21 22 |
# File 'lib/amee/data_item.rb', line 20 def total_amount_unit @total_amount_unit end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
15 16 17 |
# File 'lib/amee/data_item.rb', line 15 def values @values end |
Class Method Details
.create(category, options = {}) ⇒ Object
150 151 152 |
# File 'lib/amee/data_item.rb', line 150 def self.create(category, = {}) create_without_category(category.connection, category.full_path, ) end |
.create_batch_without_category(connection, category_path, items, options = {}) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/amee/data_item.rb', line 132 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
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 |
# File 'lib/amee/data_item.rb', line 154 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['Location'] location = response['Location'].match("http://.*?(/.*)")[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
205 206 207 208 209 |
# File 'lib/amee/data_item.rb', line 205 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
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 |
# File 'lib/amee/data_item.rb', line 22 def self.from_json(json) # Read JSON doc = JSON.parse(json) 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'] # 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 # 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 # 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 |
.from_xml(xml) ⇒ Object
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 |
# File 'lib/amee/data_item.rb', line 66 def self.from_xml(xml) # Parse data from response into hash doc = REXML::Document.new(xml) 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 # 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 # 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 # 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 |
.get(connection, path, options = {}) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/amee/data_item.rb', line 111 def self.get(connection, path, = {}) # Load data from path response = connection.get(path, ).body AMEE::Data::Item.parse(connection, response) rescue raise AMEE::BadData.new("Couldn't load DataItem. Check that your URL is correct.\n#{response}") end |
.parse(connection, response) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/amee/data_item.rb', line 119 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
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/amee/data_item.rb', line 184 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
#update(options = {}) ⇒ Object
201 202 203 |
# File 'lib/amee/data_item.rb', line 201 def update( = {}) AMEE::Data::Item.update(connection, full_path, ) end |
#value(name_or_path_or_uid) ⇒ Object
211 212 213 214 |
# File 'lib/amee/data_item.rb', line 211 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 |