Class: AMEE::Data::ItemValue

Inherits:
Object show all
Defined in:
lib/amee/data_item_value.rb

Instance Attribute Summary collapse

Attributes inherited from Object

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Object

#full_path

Constructor Details

#initialize(data = {}) ⇒ ItemValue

Returns a new instance of ItemValue.



5
6
7
8
9
10
11
12
# File 'lib/amee/data_item_value.rb', line 5

def initialize(data = {})
  @value = data ? data[:value] : nil
  @type = data ? data[:type] : nil
  @from_profile = data ? data[:from_profile] : nil
  @from_data = data ? data[:from_data] : nil
  @start_date = data ? data[:start_date] : nil
  super
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



14
15
16
# File 'lib/amee/data_item_value.rb', line 14

def type
  @type
end

Class Method Details

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/amee/data_item_value.rb', line 109

def self.create(data_item, options = {})
  # Do we want to automatically fetch the item afterwards?
  get_item = options.delete(:get_item)
  get_item = true if get_item.nil?
  # Store format if set
  format = options[:format]
  unless options.is_a?(Hash)
    raise AMEE::ArgumentError.new("Third argument must be a hash of options!")
  end
  # Set startDate
  if (options[:start_date])
    options[:startDate] = options[:start_date].xmlschema
    options.delete(:start_date)
  end

  response = data_item.connection.post(data_item.full_path, options)
  location = response['Location'].match("http://.*?(/.*)")[1]

  if get_item == true
    get_options = {}
    get_options[:format] = format if format
    return AMEE::Data::ItemValue.get(data_item.connection, location)
  else
    return location
  end
rescue
  raise AMEE::BadData.new("Couldn't create DataItemValue. Check that your information is correct.")
end

.delete(connection, path) ⇒ Object



159
160
161
162
163
# File 'lib/amee/data_item_value.rb', line 159

def self.delete(connection, path)
  connection.delete(path)
rescue
  raise AMEE::BadData.new("Couldn't delete DataItemValue. Check that your information is correct.")
end

.from_json(json, path) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/amee/data_item_value.rb', line 41

def self.from_json(json, path)
  # Read JSON
  doc = JSON.parse(json)['itemValue']
  data = {}
  data[:uid] = doc['uid']
  data[:created] = DateTime.parse(doc['created'])
  data[:modified] = DateTime.parse(doc['modified'])
  data[:name] = doc['name']
  data[:path] = path.gsub(/^\/data/, '')
  data[:value] = doc['value']
  data[:type] = doc['itemValueDefinition']['valueDefinition']['valueType']
  data[:start_date] = DateTime.parse(doc['startDate']) rescue nil
  # Create object
  ItemValue.new(data)
rescue 
  raise AMEE::BadData.new("Couldn't load DataItemValue from JSON. Check that your URL is correct.\n#{json}")
end

.from_xml(xml, path) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/amee/data_item_value.rb', line 59

def self.from_xml(xml, path)
  # Read XML
  doc = REXML::Document.new(xml)
  data = {}
  data[:uid] = REXML::XPath.first(doc, "/Resources/DataItemValueResource/ItemValue/@uid").to_s
  data[:created] = DateTime.parse(REXML::XPath.first(doc, "/Resources/DataItemValueResource/ItemValue/@Created").to_s)
  data[:modified] = DateTime.parse(REXML::XPath.first(doc, "/Resources/DataItemValueResource/ItemValue/@Modified").to_s)
  data[:name] = REXML::XPath.first(doc, '/Resources/DataItemValueResource/ItemValue/Name').text
  data[:path] = path.gsub(/^\/data/, '')
  data[:value] = REXML::XPath.first(doc, '/Resources/DataItemValueResource/ItemValue/Value').text
  data[:type] = REXML::XPath.first(doc, '/Resources/DataItemValueResource/ItemValue/ItemValueDefinition/ValueDefinition/ValueType').text
  data[:from_profile] = REXML::XPath.first(doc, '/Resources/DataItemValueResource/ItemValue/ItemValueDefinition/FromProfile').text == "true" ? true : false
  data[:from_data] = REXML::XPath.first(doc, '/Resources/DataItemValueResource/ItemValue/ItemValueDefinition/FromData').text == "true" ? true : false
  data[:start_date] = DateTime.parse(REXML::XPath.first(doc, "/Resources/DataItemValueResource/ItemValue/StartDate").text) rescue nil
  # Create object
  ItemValue.new(data)
rescue
  raise AMEE::BadData.new("Couldn't load DataItemValue from XML. Check that your URL is correct.\n#{xml}")
end

.get(connection, path) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/amee/data_item_value.rb', line 79

def self.get(connection, path)
  # Load data from path
  response = connection.get(path).body
  # Parse data from response
  data = {}
  value = ItemValue.parse(connection, response, path)
  # Done
  return value
rescue
  raise AMEE::BadData.new("Couldn't load DataItemValue. Check that your URL is correct.")
end

.parse(connection, response, path) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/amee/data_item_value.rb', line 95

def self.parse(connection, response, path) 
  if response.is_json?
    value = ItemValue.from_json(response, path)
  else
    value = ItemValue.from_xml(response, path)
  end
  # Store connection in object for future use
  value.connection = connection
  # Done
  return value
rescue
  raise AMEE::BadData.new("Couldn't load DataItemValue. Check that your URL is correct.\n#{response}")
end

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



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/amee/data_item_value.rb', line 138

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 AMEE::Data::ItemValue.get(connection, path)
    else
      return AMEE::Data::ItemValue.parse(connection, response.body)
    end
  end
rescue
  raise AMEE::BadData.new("Couldn't update DataItemValue. Check that your information is correct.\n#{response}")
end

Instance Method Details

#from_data?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/amee/data_item_value.rb', line 33

def from_data?
  @from_data
end

#from_profile?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/amee/data_item_value.rb', line 29

def from_profile?
  @from_profile
end

#save!Object



91
92
93
# File 'lib/amee/data_item_value.rb', line 91

def save!
  response = @connection.put(full_path, :value => value).body
end

#start_dateObject



37
38
39
# File 'lib/amee/data_item_value.rb', line 37

def start_date
  @start_date
end

#update(options = {}) ⇒ Object



155
156
157
# File 'lib/amee/data_item_value.rb', line 155

def update(options = {})
  AMEE::Data::ItemValue.update(connection, full_path, options)
end

#valueObject



16
17
18
19
20
21
22
23
# File 'lib/amee/data_item_value.rb', line 16

def value
  case type
  when "DECIMAL"
    @value.to_f
  else
    @value
  end
end

#value=(val) ⇒ Object



25
26
27
# File 'lib/amee/data_item_value.rb', line 25

def value=(val)
  @value = val
end