Class: AMEE::Admin::ReturnValueDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/amee/v3/return_value_definition.rb

Instance Attribute Summary collapse

Attributes inherited from Object

#connection, #created, #modified, #path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Object

#expire_cache, get_and_parse

Methods included from ParseHelper

#load_xml_doc, #node_value, #xmlpathpreamble

Constructor Details

#initialize(data = {}) ⇒ ReturnValueDefinition

Returns a new instance of ReturnValueDefinition.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/amee/v3/return_value_definition.rb', line 51

def initialize(data = {})
  @itemdefuid=data[:itemdefuid]
  @name = data[:name]
  @uid = data[:uid]       
  @type = data[:type]
  @unit = data[:unit]
  @perunit = data[:perunit]
  @valuetype = data[:valuetype]
  
  super
end

Instance Attribute Details

#itemdefuidObject (readonly)

Returns the value of attribute itemdefuid.



63
64
65
# File 'lib/amee/v3/return_value_definition.rb', line 63

def itemdefuid
  @itemdefuid
end

#nameObject (readonly)

Returns the value of attribute name.



63
64
65
# File 'lib/amee/v3/return_value_definition.rb', line 63

def name
  @name
end

#perunitObject (readonly)

Returns the value of attribute perunit.



63
64
65
# File 'lib/amee/v3/return_value_definition.rb', line 63

def perunit
  @perunit
end

#typeObject (readonly)

Returns the value of attribute type.



63
64
65
# File 'lib/amee/v3/return_value_definition.rb', line 63

def type
  @type
end

#uidObject (readonly)

Returns the value of attribute uid.



63
64
65
# File 'lib/amee/v3/return_value_definition.rb', line 63

def uid
  @uid
end

#unitObject (readonly)

Returns the value of attribute unit.



63
64
65
# File 'lib/amee/v3/return_value_definition.rb', line 63

def unit
  @unit
end

#valuetypeObject (readonly)

Returns the value of attribute valuetype.



63
64
65
# File 'lib/amee/v3/return_value_definition.rb', line 63

def valuetype
  @valuetype
end

Class Method Details

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



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/amee/v3/return_value_definition.rb', line 166

def self.create(connection,itemdefuid, options = {})
  
  unless options.is_a?(Hash)
    raise AMEE::ArgumentError.new("Second argument must be a hash of options!")
  end
  # Send data
  vt=options.delete(:valuetype)
  case vt
  when 'text'
    options.merge!( :valueDefinition=>"CCEB59CACE1B")
  else
    options.merge!( :valueDefinition=>"45433E48B39F")
  end
  
  options.merge!(:returnobj=>true)
  response = connection.v3_post("/#{AMEE::Connection.api_version}/definitions/#{itemdefuid}/returnvalues", options)
  return ReturnValueDefinition.load(connection,itemdefuid , response.headers_hash['Location'].split('/')[7])
rescue
  raise AMEE::BadData.new("Couldn't create ReturnValueDefinition. Check that your information is correct.\n#{response}")
end

.delete(connection, itemdefuid, return_value_definition) ⇒ Object



187
188
189
190
191
192
193
194
195
196
# File 'lib/amee/v3/return_value_definition.rb', line 187

def self.delete(connection, itemdefuid,return_value_definition)
  
  # Deleting takes a while... up the timeout to 120 seconds temporarily
  t = connection.timeout
  connection.timeout = 120
  connection.v3_delete("/#{AMEE::Connection.api_version}/definitions/#{itemdefuid}/returnvalues/" + return_value_definition.uid)
  connection.timeout = t
rescue
  raise AMEE::BadData.new("Couldn't delete ReturnValueDefinition. Check that your information is correct.")
end

.from_json(json) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/amee/v3/return_value_definition.rb', line 82

def self.from_json(json)
  # Read JSON
  doc = JSON.parse(json)
  data = {}
  p=doc['returnValueDefinition']
  data[:uid] = p['uid']
  data[:itemdefuid] = p['itemDefinition']['uid']
  data[:type] = p['type']
  data[:path] = p['path']
  data[:unit] = p['unit']
  data[:perunit] = p['perUnit']
  data[:valuetype] = p['valueDefinition']['valueType']
  data[:default] = p['value']
  
  # Create object
  ReturnValueDefinition.new(data)
rescue
  raise AMEE::BadData.new("Couldn't load ReturnValueDefinition from JSON. Check that your URL is correct.\n#{json}")
end

.from_xml(xml) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/amee/v3/return_value_definition.rb', line 106

def self.from_xml(xml)

 
  # Parse data from response into hash
  doc = load_xml_doc(xml)
  data = {}
  data[:uid] = x "@uid",:doc=>doc
  data[:itemdefuid] = x "ItemDefinition/@uid",:doc=>doc
  data[:type] = x "Type",:doc=>doc
  data[:unit] = x "Unit",:doc=>doc
  data[:perunit] =  x "PerUnit",:doc=>doc
  data[:valuetype] = x  "ValueDefinition/ValueType",:doc=>doc
  data[:default] = x "ReturnValueDefinition/Default",:doc=>doc
  
 
  # Create object
  ReturnValueDefinition.new(data)
rescue => err
  raise AMEE::BadData.new("Couldn't load ReturnValueDefinition from XML. Check that your URL is correct.\n#{xml}\n#{err}")
end

.get(connection, path, options = {}) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/amee/v3/return_value_definition.rb', line 127

def self.get(connection, path, options = {})
  # Load data from path
  response = connection.v3_get(path, options)
  # Parse response
  item_definition = ReturnValueDefinition.parse(connection, response)
  # Done
  return item_definition
rescue => err
  raise AMEE::BadData.new("Couldn't load ReturnValueDefinition. Check that your URL is correct.\n#{response}\n#{err}")
end

.list(connection) ⇒ Object



65
66
67
# File 'lib/amee/v3/return_value_definition.rb', line 65

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

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



140
141
142
# File 'lib/amee/v3/return_value_definition.rb', line 140

def self.load(connection,itemdefuid,ivduid,options={})
  ReturnValueDefinition.get(connection,"/#{AMEE::Connection.api_version}/definitions/#{itemdefuid}/returnvalues/#{ivduid};full",options)
end

.parse(connection, response) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/amee/v3/return_value_definition.rb', line 69

def self.parse(connection, response)
  # Parse data from response
  if response.is_json?
    item_definition = ReturnValueDefinition.from_json(response)
  else
    item_definition = ReturnValueDefinition.from_xml(response)
  end
  # Store connection in object for future use
  item_definition.connection = connection
  # Done
  return item_definition
end

.update(connection, path, options = {}) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/amee/v3/return_value_definition.rb', line 148

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.v3_put(path, options)
  if get_item
    if response.empty?
      return ReturnValueDefinition.get(connection, path)
    else
      return ReturnValueDefinition.parse(connection, response)
    end
  end
rescue
  raise AMEE::BadData.new("Couldn't update ReturnValueDefinition. Check that your information is correct.\n#{response}")
end

.xmlpathpreambleObject



102
103
104
# File 'lib/amee/v3/return_value_definition.rb', line 102

def self.xmlpathpreamble
  "/Representation/ReturnValueDefinition/"
end

Instance Method Details

#reload(connection) ⇒ Object



144
145
146
# File 'lib/amee/v3/return_value_definition.rb', line 144

def reload(connection)
  ReturnValueDefinition.load(connection,itemdefuid,uid)
end