lib/amee/profile_item.rb in amee-2.0.35 vs lib/amee/profile_item.rb in amee-2.2.0
- old
+ new
@@ -4,19 +4,23 @@
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
attr_reader :values
attr_reader :total_amount
attr_reader :total_amount_unit
+ attr_reader :amounts
+ attr_reader :notes
attr_reader :start_date
attr_reader :end_date
attr_reader :data_item_uid
# V1 compatibility
@@ -85,10 +89,31 @@
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
@@ -112,11 +137,11 @@
item.elements.each do |element|
key = element.name
value = element.text
case key
when 'Name', 'Path', 'Value'
- value_data[key.downcase.to_sym] = value
+ value_data[key.downcase.to_sym] = value
end
end
value_data[:uid] = item.attributes['uid'].to_s
data[:values] << value_data
end
@@ -153,10 +178,26 @@
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
@@ -253,19 +294,19 @@
elsif options[:start_date] && connection.version >= 2
options[:startDate] = options[:start_date].xmlschema
end
if options[:end_date] && connection.version >= 2
options[:endDate] = options[:end_date].xmlschema
- end
+ end
if options[:duration] && connection.version >= 2
options[:duration] = "PT#{options[:duration] * 86400}S"
end
# Send data to path
options.merge! :dataItemUid => data_item_uid
response = connection.post(path, options)
if response['Location']
- location = response['Location'].match("http://.*?(/.*)")[1]
+ 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
@@ -314,10 +355,10 @@
options[:startDate] = options[:start_date].xmlschema
end
options.delete(:start_date)
if options[:end_date] && connection.version >= 2
options[:endDate] = options[:end_date].xmlschema
- end
+ end
options.delete(:end_date)
if options[:duration] && connection.version >= 2
options[:duration] = "PT#{options[:duration] * 86400}S"
end
# Go