Class: AMEE::Admin::ItemDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/amee/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

#xmlpathpreamble

Constructor Details

#initialize(data = {}) ⇒ ItemDefinition

Returns a new instance of ItemDefinition.



34
35
36
37
38
# File 'lib/amee/item_definition.rb', line 34

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

Instance Attribute Details

#drill_downsObject (readonly)

Returns the value of attribute drill_downs.



40
41
42
# File 'lib/amee/item_definition.rb', line 40

def drill_downs
  @drill_downs
end

#nameObject (readonly)

Returns the value of attribute name.



40
41
42
# File 'lib/amee/item_definition.rb', line 40

def name
  @name
end

Class Method Details

.create(connection, options = {}) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/amee/item_definition.rb', line 126

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



140
141
142
143
144
145
146
147
148
# File 'lib/amee/item_definition.rb', line 140

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/amee/item_definition.rb', line 59

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/amee/item_definition.rb', line 74

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



90
91
92
93
94
95
96
97
98
99
# File 'lib/amee/item_definition.rb', line 90

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



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

def self.list(connection)
  ItemDefinitionList.new(connection)
end

.load(connection, uid, options = {}) ⇒ Object



118
119
120
# File 'lib/amee/item_definition.rb', line 118

def self.load(connection,uid,options={})
  ItemDefinition.get(connection,"/definitions/itemDefinitions/#{uid}",options)
end

.parse(connection, response, is_list = true) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/amee/item_definition.rb', line 46

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/amee/item_definition.rb', line 101

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

Instance Method Details

#item_value_definition_listObject



122
123
124
# File 'lib/amee/item_definition.rb', line 122

def item_value_definition_list
  @item_value_definitions ||= AMEE::Admin::ItemValueDefinitionList.new(connection,uid)
end