lib/amee/profile_item.rb in Floppy-amee-2.0.18 vs lib/amee/profile_item.rb in Floppy-amee-2.0.19

- old
+ new

@@ -56,11 +56,11 @@ 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.") + raise AMEE::BadData.new("Couldn't load ProfileItem from JSON data. Check that your URL is correct.\n#{json}") end def self.from_v2_json(json) # Parse json doc = JSON.parse(json) @@ -88,11 +88,11 @@ data[:values] << value_data 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.") + raise AMEE::BadData.new("Couldn't load ProfileItem from V2 JSON data. Check that your URL is correct.\n#{json}") end def self.from_xml(xml) # Parse XML doc = REXML::Document.new(xml) @@ -121,11 +121,11 @@ 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.") + raise AMEE::BadData.new("Couldn't load ProfileItem from XML data. Check that your URL is correct.\n#{xml}") end def self.from_v2_xml(xml) # Parse XML doc = REXML::Document.new(xml) @@ -156,11 +156,11 @@ data[:values] << value_data 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.") + raise AMEE::BadData.new("Couldn't load ProfileItem from V2 XML data. Check that your URL is correct.\n#{xml}") end def self.from_v2_atom(response) # Parse XML doc = REXML::Document.new(response) @@ -185,11 +185,11 @@ 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.") + raise AMEE::BadData.new("Couldn't load ProfileItem from V2 ATOM data. Check that your URL is correct.\n#{response}") end def self.parse(connection, response) # Parse data from response if response.is_v2_json? @@ -229,11 +229,11 @@ end # Load data from path response = connection.get(path, options).body return Item.parse(connection, response) rescue - raise AMEE::BadData.new("Couldn't load ProfileItem. Check that your URL is correct.") + raise AMEE::BadData.new("Couldn't load ProfileItem. Check that your URL is correct.\n#{response}") end def self.create(category, data_item_uid, options = {}) create_without_category(category.connection, category.full_path, data_item_uid, options) end @@ -251,25 +251,23 @@ if options[:start_date] && connection.version < 2 options[:validFrom] = options[:start_date].amee1_date elsif options[:start_date] && connection.version >= 2 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 - options.delete(:end_date) 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] else - category = Category.parse(connection, response.body) + category = Category.parse(connection, response.body, nil) location = category.full_path + "/" + category.items[0][:path] end if get_item == true get_options = {} get_options[:returnUnit] = options[:returnUnit] if options[:returnUnit] @@ -278,11 +276,11 @@ return AMEE::Profile::Item.get(connection, location, get_options) else return location end rescue - raise AMEE::BadData.new("Couldn't create ProfileItem. Check that your information is correct.") + raise AMEE::BadData.new("Couldn't create ProfileItem. Check that your information is correct.\n#{response}") end def self.create_batch(category, items, options = {}) create_batch_without_category(category.connection, category.full_path, items) end @@ -307,21 +305,35 @@ def self.update(connection, path, options = {}) # Do we want to automatically fetch the item afterwards? get_item = options.delete(:get_item) get_item = true if get_item.nil? + # Set dates + if options[:start_date] && connection.version < 2 + options[:validFrom] = options[:start_date].amee1_date + elsif options[:start_date] && connection.version >= 2 + 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 + options.delete(:end_date) + if options[:duration] && connection.version >= 2 + options[:duration] = "PT#{options[:duration] * 86400}S" + end # Go response = connection.put(path, options) 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.") + raise AMEE::BadData.new("Couldn't update ProfileItem. Check that your information is correct.\n#{response}") end def update(options = {}) AMEE::Profile::Item.update(connection, full_path, options) end @@ -338,10 +350,10 @@ 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) + return AMEE::Profile::Category.parse(connection, response, nil) else return true end end