Class: AMEE::Admin::ItemDefinition
- Includes:
- ParseHelper
- Defined in:
- lib/amee/item_definition.rb,
lib/amee/v3/item_definition.rb
Instance Attribute Summary collapse
-
#algorithms ⇒ Object
Returns the value of attribute algorithms.
-
#drill_downs ⇒ Object
readonly
Returns the value of attribute drill_downs.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#usages ⇒ Object
Returns the value of attribute usages.
Attributes inherited from Object
#connection, #created, #modified, #path, #uid
Class Method Summary collapse
- .create(connection, options = {}) ⇒ Object
- .delete(connection, item_definition) ⇒ Object
- .from_json(json) ⇒ Object
- .from_xml(xml, is_list = true) ⇒ Object
- .from_xml_with_v3(xml, is_list = true) ⇒ Object
- .get(connection, path, options = {}) ⇒ Object
- .list(connection) ⇒ Object
- .load(connection, uid, options = {}) ⇒ Object
- .metaxmlpathpreamble ⇒ Object
- .parse(connection, response, is_list = true) ⇒ Object
- .update(connection, path, options = {}) ⇒ Object
- .v3_get(connection, uid, options = {}) ⇒ Object
Instance Method Summary collapse
- #expire_cache_with_v3 ⇒ Object
- #full_path ⇒ Object
-
#initialize(data = {}) ⇒ ItemDefinition
constructor
A new instance of ItemDefinition.
- #initialize_with_v3(data = {}) ⇒ Object
- #item_value_definition_list ⇒ Object
- #save! ⇒ Object
Methods included from ParseHelper
#load_xml_doc, #node_value, #xmlpathpreamble
Methods inherited from Object
Constructor Details
#initialize(data = {}) ⇒ ItemDefinition
Returns a new instance of ItemDefinition.
37 38 39 40 41 |
# File 'lib/amee/item_definition.rb', line 37 def initialize(data = {}) @name = data[:name] @drill_downs = data[:drillDown] || [] super end |
Instance Attribute Details
#algorithms ⇒ Object
Returns the value of attribute algorithms.
17 18 19 |
# File 'lib/amee/v3/item_definition.rb', line 17 def algorithms @algorithms end |
#drill_downs ⇒ Object (readonly)
Returns the value of attribute drill_downs.
43 44 45 |
# File 'lib/amee/item_definition.rb', line 43 def drill_downs @drill_downs end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
43 44 45 |
# File 'lib/amee/item_definition.rb', line 43 def name @name end |
#usages ⇒ Object
Returns the value of attribute usages.
17 18 19 |
# File 'lib/amee/v3/item_definition.rb', line 17 def usages @usages end |
Class Method Details
.create(connection, options = {}) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/amee/item_definition.rb', line 129 def self.create(connection, = {}) unless .is_a?(Hash) raise AMEE::ArgumentError.new("Second argument must be a hash of options!") end # Send data response = connection.post("/definitions/itemDefinitions", ).body # Parse response item_definition = ItemDefinition.parse(connection, response) # Get the ItemDefinition again return ItemDefinition.get(connection, "/definitions/itemDefinitions/" + item_definition.uid) rescue raise AMEE::BadData.new("Couldn't create ItemDefinition. Check that your information is correct.\n#{response}") end |
.delete(connection, item_definition) ⇒ Object
143 144 145 146 147 148 149 150 151 |
# File 'lib/amee/item_definition.rb', line 143 def self.delete(connection, item_definition) # Deleting takes a while... up the timeout to 120 seconds temporarily t = connection.timeout connection.timeout = 120 connection.delete("/definitions/itemDefinitions/" + item_definition.uid) connection.timeout = t rescue raise AMEE::BadData.new("Couldn't delete ProfileItem. Check that your information is correct.") end |
.from_json(json) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/amee/item_definition.rb', line 62 def self.from_json(json) # Read JSON doc = JSON.parse(json) data = {} data[:uid] = doc['itemDefinition']['uid'] data[:created] = DateTime.parse(doc['itemDefinition']['created']) data[:modified] = DateTime.parse(doc['itemDefinition']['modified']) data[:name] = doc['itemDefinition']['name'] data[:drillDown] = doc['itemDefinition']['drillDown'].split(",") rescue nil # Create object ItemDefinition.new(data) rescue raise AMEE::BadData.new("Couldn't load ItemDefinition from JSON. Check that your URL is correct.\n#{json}") end |
.from_xml(xml, is_list = true) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/amee/item_definition.rb', line 77 def self.from_xml(xml, is_list = true) prefix = is_list ? "/Resources/ItemDefinitionsResource/" : "/Resources/ItemDefinitionResource/" # Parse data from response into hash doc = REXML::Document.new(xml) data = {} data[:uid] = REXML::XPath.first(doc, prefix + "ItemDefinition/@uid").to_s data[:created] = DateTime.parse(REXML::XPath.first(doc, prefix + "ItemDefinition/@created").to_s) data[:modified] = DateTime.parse(REXML::XPath.first(doc, prefix + "ItemDefinition/@modified").to_s) data[:name] = REXML::XPath.first(doc, prefix + "ItemDefinition/Name").text data[:drillDown] = REXML::XPath.first(doc, prefix + "ItemDefinition/DrillDown").text.split(",") rescue nil # Create object ItemDefinition.new(data) rescue raise AMEE::BadData.new("Couldn't load ItemDefinition from XML. Check that your URL is correct.\n#{xml}") end |
.from_xml_with_v3(xml, is_list = true) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/amee/v3/item_definition.rb', line 38 def self.from_xml_with_v3(xml, is_list = true) return self.from_xml_without_v3(xml, is_list) if xml.include?('<Resources>') # Parse data from response into hash doc = load_xml_doc(xml) data = {} data[:uid] = x '@uid', :doc => doc, :meta => true data[:created] = DateTime.parse(x '@created', :doc => doc, :meta => true) data[:modified] = DateTime.parse(x '@modified', :doc => doc, :meta => true) data[:name] = x 'Name', :doc => doc, :meta => true data[:drillDown] = x('DrillDown', :doc => doc, :meta => true).split(",") rescue nil data[:usages] = [(x 'Usages/Usage/Name', :doc => doc, :meta => true)].flatten.delete_if{|x| x.nil?} data[:algorithms] = begin names = [(x 'Algorithms/Algorithm/Name', :doc => doc, :meta => true)].flatten.delete_if{|x| x.nil?} uids = [(x 'Algorithms/Algorithm/@uid', :doc => doc, :meta => true)].flatten.delete_if{|x| x.nil?} Hash[names.zip(uids)] end # Create object ItemDefinition.new(data) rescue raise AMEE::BadData.new("Couldn't load ItemDefinition from XML. Check that your URL is correct.\n#{$!}\n#{xml}") end |
.get(connection, path, options = {}) ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/amee/item_definition.rb', line 93 def self.get(connection, path, = {}) # Load data from path response = connection.get(path, ).body # Parse response item_definition = ItemDefinition.parse(connection, response, false) # Done return item_definition rescue raise AMEE::BadData.new("Couldn't load ItemDefinition. Check that your URL is correct.\n#{response}") end |
.list(connection) ⇒ Object
45 46 47 |
# File 'lib/amee/item_definition.rb', line 45 def self.list(connection) ItemDefinitionList.new(connection) end |
.load(connection, uid, options = {}) ⇒ Object
121 122 123 |
# File 'lib/amee/item_definition.rb', line 121 def self.load(connection,uid,={}) ItemDefinition.get(connection,"/definitions/itemDefinitions/#{uid}",) end |
.metaxmlpathpreamble ⇒ Object
19 20 21 |
# File 'lib/amee/v3/item_definition.rb', line 19 def self. '/Representation/ItemDefinition/' end |
.parse(connection, response, is_list = true) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/amee/item_definition.rb', line 49 def self.parse(connection, response, is_list = true) # Parse data from response if response.is_json? item_definition = ItemDefinition.from_json(response) else item_definition = ItemDefinition.from_xml(response, is_list) end # Store connection in object for future use item_definition.connection = connection # Done return item_definition end |
.update(connection, path, options = {}) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/amee/item_definition.rb', line 104 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 ItemDefinition.get(connection, path) else return ItemDefinition.parse(connection, response.body) end end rescue raise AMEE::BadData.new("Couldn't update ItemDefinition. Check that your information is correct.\n#{response}") end |
.v3_get(connection, uid, options = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/amee/v3/item_definition.rb', line 27 def self.v3_get(connection, uid, ={}) # Load data from path response = connection.v3_get("/#{AMEE::Connection.api_version}/definitions/#{uid};full", ) # Parse response item_definition = ItemDefinition.parse(connection, response, false) # Done return item_definition rescue raise AMEE::BadData.new("Couldn't load ItemDefinition. Check that your URL is correct.\n#{$!}\n#{response}") end |
Instance Method Details
#expire_cache_with_v3 ⇒ Object
71 72 73 74 |
# File 'lib/amee/v3/item_definition.rb', line 71 def expire_cache_with_v3 @connection.expire_matching("/#{AMEE::Connection.api_version}/definitions/#{@uid}.*") expire_cache_without_v3 end |
#full_path ⇒ Object
153 154 155 |
# File 'lib/amee/item_definition.rb', line 153 def full_path "/definitions/itemDefinitions/#{@uid}" end |
#initialize_with_v3(data = {}) ⇒ Object
10 11 12 13 14 |
# File 'lib/amee/v3/item_definition.rb', line 10 def initialize_with_v3(data = {}) @usages = data[:usages] || [] @algorithms = data[:algorithms] || {} initialize_without_v3(data) end |
#item_value_definition_list ⇒ Object
125 126 127 |
# File 'lib/amee/item_definition.rb', line 125 def item_value_definition_list @item_value_definitions ||= AMEE::Admin::ItemValueDefinitionList.new(connection,uid) end |
#save! ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/amee/v3/item_definition.rb', line 63 def save! = { :usages => @usages.join(','), :name => @name } @connection.v3_put("/#{AMEE::Connection.api_version}/definitions/#{@uid}",) end |