74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/amee/profile_item.rb', line 74
def self.from_v2_json(json)
doc = JSON.parse(json)
data = {}
data[:profile_uid] = doc['profile']['uid']
data[:data_item_uid] = doc['profileItem']['dataItem']['uid']
data[:uid] = doc['profileItem']['uid']
data[:name] = doc['profileItem']['name']
data[:path] = doc['path']
data[:total_amount] = doc['profileItem']['amount']['value'].to_f
data[:total_amount_unit] = doc['profileItem']['amount']['unit']
data[:start_date] = DateTime.parse(doc['profileItem']['startDate'])
data[:end_date] = DateTime.parse(doc['profileItem']['endDate']) rescue nil
data[:values] = []
doc['profileItem']['itemValues'].each do |item|
value_data = {}
item.each_pair do |key,value|
case key
when 'name', 'path', 'uid', 'value', 'unit'
value_data[key.downcase.to_sym] = value
when 'perUnit'
value_data[:per_unit] = value
end
end
data[:values] << value_data
end
if doc['profileItem']['amounts']
if doc['profileItem']['amounts']['amount']
data[:amounts] = doc['profileItem']['amounts']['amount'].map do |item|
{
:type => item['type'],
:value => item['value'].to_f,
:unit => item['unit'],
:per_unit => item['perUnit'],
:default => (item['default'] == 'true'),
}
end
end
if doc['profileItem']['amounts']['note']
data[:notes] = doc['profileItem']['amounts']['note'].map do |item|
{
:type => item['type'],
:value => item['value'],
}
end
end
end
Item.new(data)
rescue
raise AMEE::BadData.new("Couldn't load ProfileItem from V2 JSON data. Check that your URL is correct.\n#{json}")
end
|