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

Constructor Details

#initialize(data = {}) ⇒ ItemDefinition

Returns a new instance of ItemDefinition.



49
50
51
52
# File 'lib/amee/item_definition.rb', line 49

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



54
55
56
# File 'lib/amee/item_definition.rb', line 54

def name
  @name
end

Class Method Details

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



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/amee/item_definition.rb', line 130

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



144
145
146
147
148
149
150
151
152
# File 'lib/amee/item_definition.rb', line 144

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



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

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']
  # 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



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

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



102
103
104
105
106
107
108
109
110
111
# File 'lib/amee/item_definition.rb', line 102

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



56
57
58
# File 'lib/amee/item_definition.rb', line 56

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

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



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

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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/amee/item_definition.rb', line 113

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