Class: AMEE::Admin::ItemDefinition

Inherits:
Object
  • Object
show all
Includes:
ParseHelper
Defined in:
lib/amee/item_definition.rb,
lib/amee/v3/item_definition.rb

Instance Attribute Summary collapse

Attributes inherited from Object

#connection, #created, #modified, #path, #uid

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ParseHelper

#load_xml_doc, #node_value, #xmlpathpreamble

Methods inherited from Object

get_and_parse

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

#algorithmsObject

Returns the value of attribute algorithms.



17
18
19
# File 'lib/amee/v3/item_definition.rb', line 17

def algorithms
  @algorithms
end

#drill_downsObject (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

#nameObject (readonly)

Returns the value of attribute name.



43
44
45
# File 'lib/amee/item_definition.rb', line 43

def name
  @name
end

#usagesObject

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, options = {})
  unless options.is_a?(Hash)
    raise AMEE::ArgumentError.new("Second argument must be a hash of options!")
  end
  # Send data
  response = connection.post("/definitions/itemDefinitions", options).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_without_v3Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/amee/v3/item_definition.rb', line 39

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

.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, options = {})
  # Load data from path
  response = connection.get(path, options).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,options={})
  ItemDefinition.get(connection,"/definitions/itemDefinitions/#{uid}",options)
end

.metaxmlpathpreambleObject



19
20
21
# File 'lib/amee/v3/item_definition.rb', line 19

def self.metaxmlpathpreamble
  '/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, options = {})
  # Do we want to automatically fetch the item afterwards?
  get_item = options.delete(:get_item)
  get_item = true if get_item.nil?
  # Go
  response = connection.put(path, options)
  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, options={})
  # Load data from path
  response = connection.v3_get("/#{AMEE::Connection.api_version}/definitions/#{uid};full", options)
  # 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_cacheObject



72
73
74
75
# File 'lib/amee/v3/item_definition.rb', line 72

def expire_cache
  @connection.expire_matching("/#{AMEE::Connection.api_version}/definitions/#{@uid}.*")
  expire_cache_without_v3
end

#expire_cache_without_v3Object



71
# File 'lib/amee/v3/item_definition.rb', line 71

alias_method :expire_cache_without_v3, :expire_cache

#full_pathObject



153
154
155
# File 'lib/amee/item_definition.rb', line 153

def full_path
  "/definitions/itemDefinitions/#{@uid}"
end

#initialize_without_v3ItemDefinition

Returns a new instance of ItemDefinition.

Returns:



10
11
12
13
14
# File 'lib/amee/v3/item_definition.rb', line 10

def initialize(data = {})
  @name = data[:name]
  @drill_downs = data[:drillDown] || []
  super
end

#item_value_definition_listObject



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!
  save_options = {
    :usages => @usages.join(','),
    :name => @name
  }
  @connection.v3_put("/#{AMEE::Connection.api_version}/definitions/#{@uid}",save_options)
end