Floppy-amee 0.4.2 → 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README +2 -1
- data/lib/amee/profile_item.rb +109 -0
- metadata +2 -2
data/README
CHANGED
@@ -21,7 +21,8 @@ Homepage: http://github.com/Floppy/amee-ruby
|
|
21
21
|
Currently, you can read DataCategories, DataItems and DataItemValues. See
|
22
22
|
examples/view_data_*.rb for simple usage examples. You can also get the list
|
23
23
|
of available Profiles and create new ones. See examples/list_profiles.rb and
|
24
|
-
examples/create_profile.rb for details. You can also load ProfileCategories
|
24
|
+
examples/create_profile.rb for details. You can also load ProfileCategories,
|
25
|
+
and load and create ProfileItems.
|
25
26
|
|
26
27
|
The gem will use the AMEE JSON API if the JSON gem is installed on the local
|
27
28
|
system. Otherwise the XML API will be used.
|
data/lib/amee/profile_item.rb
CHANGED
@@ -2,6 +2,115 @@ module AMEE
|
|
2
2
|
module Profile
|
3
3
|
class Item < AMEE::Profile::Object
|
4
4
|
|
5
|
+
def initialize(data = {})
|
6
|
+
@values = data ? data[:values] : []
|
7
|
+
@total_amount_per_month = data[:total_amount_per_month]
|
8
|
+
@valid_from = data[:valid_from]
|
9
|
+
@end = data[:end]
|
10
|
+
super
|
11
|
+
end
|
12
|
+
|
13
|
+
attr_reader :values
|
14
|
+
attr_reader :total_amount_per_month
|
15
|
+
attr_reader :valid_from
|
16
|
+
attr_reader :end
|
17
|
+
|
18
|
+
def self.from_json(json)
|
19
|
+
# Parse json
|
20
|
+
doc = JSON.parse(json)
|
21
|
+
data = {}
|
22
|
+
data[:profile_uid] = doc['profile']['uid']
|
23
|
+
data[:uid] = doc['profileItem']['uid']
|
24
|
+
data[:name] = doc['profileItem']['name']
|
25
|
+
data[:path] = doc['path']
|
26
|
+
data[:total_amount_per_month] = doc['profileItem']['amountPerMonth']
|
27
|
+
data[:valid_from] = DateTime.strptime(doc['profileItem']['validFrom'], "%Y%m%d")
|
28
|
+
data[:end] = doc['profileItem']['end'] == "false" ? false : true
|
29
|
+
data[:values] = []
|
30
|
+
doc['profileItem']['itemValues'].each do |item|
|
31
|
+
value_data = {}
|
32
|
+
value_data[:values] = {}
|
33
|
+
item.each_pair do |key,value|
|
34
|
+
case key
|
35
|
+
when 'name', 'path', 'uid', 'value'
|
36
|
+
value_data[key.downcase.to_sym] = value
|
37
|
+
end
|
38
|
+
end
|
39
|
+
data[:values] << value_data
|
40
|
+
end
|
41
|
+
# Create object
|
42
|
+
Item.new(data)
|
43
|
+
rescue
|
44
|
+
raise AMEE::BadData.new("Couldn't load ProfileItem from JSON data. Check that your URL is correct.")
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.from_xml(xml)
|
48
|
+
# Parse XML
|
49
|
+
doc = REXML::Document.new(xml)
|
50
|
+
data = {}
|
51
|
+
data[:profile_uid] = REXML::XPath.first(doc, "/Resources/ProfileItemResource/Profile/@uid").to_s
|
52
|
+
data[:uid] = REXML::XPath.first(doc, "/Resources/ProfileItemResource/ProfileItem/@uid").to_s
|
53
|
+
data[:name] = REXML::XPath.first(doc, '/Resources/ProfileItemResource/ProfileItem/Name').text
|
54
|
+
data[:path] = REXML::XPath.first(doc, '/Resources/ProfileItemResource/Path').text || ""
|
55
|
+
data[:total_amount_per_month] = REXML::XPath.first(doc, '/Resources/ProfileItemResource/ProfileItem/AmountPerMonth').text.to_f rescue nil
|
56
|
+
data[:valid_from] = DateTime.strptime(REXML::XPath.first(doc, "/Resources/ProfileItemResource/ProfileItem/ValidFrom").text, "%Y%m%d")
|
57
|
+
data[:end] = REXML::XPath.first(doc, '/Resources/ProfileItemResource/ProfileItem/End').text == "false" ? false : true
|
58
|
+
data[:values] = []
|
59
|
+
REXML::XPath.each(doc, '/Resources/ProfileItemResource/ProfileItem/ItemValues/ItemValue') do |item|
|
60
|
+
value_data = {}
|
61
|
+
value_data[:values] = {}
|
62
|
+
item.elements.each do |element|
|
63
|
+
key = element.name
|
64
|
+
value = element.text
|
65
|
+
case key
|
66
|
+
when 'Name', 'Path'
|
67
|
+
value_data[key.downcase.to_sym] = value
|
68
|
+
when 'Value'
|
69
|
+
value_data[:value] = value
|
70
|
+
end
|
71
|
+
end
|
72
|
+
value_data[:uid] = item.attributes['uid'].to_s
|
73
|
+
data[:values] << value_data
|
74
|
+
end
|
75
|
+
# Create object
|
76
|
+
Item.new(data)
|
77
|
+
rescue
|
78
|
+
raise AMEE::BadData.new("Couldn't load ProfileItem from XML data. Check that your URL is correct.")
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.get_history(connection, path, num_months, end_date = Date.today)
|
82
|
+
month = end_date.month
|
83
|
+
year = end_date.year
|
84
|
+
history = []
|
85
|
+
num_months.times do
|
86
|
+
date = Date.new(year, month)
|
87
|
+
history << self.get(connection, path, date)
|
88
|
+
month -= 1
|
89
|
+
if (month == 0)
|
90
|
+
year -= 1
|
91
|
+
month = 12
|
92
|
+
end
|
93
|
+
end
|
94
|
+
return history.reverse
|
95
|
+
end
|
96
|
+
|
97
|
+
def self.get(connection, path, for_date = Date.today)
|
98
|
+
# Load data from path
|
99
|
+
response = connection.get(path, :profileDate => for_date.strftime("%Y%m"))
|
100
|
+
# Parse data from response
|
101
|
+
if response.is_json?
|
102
|
+
cat = Item.from_json(response)
|
103
|
+
else
|
104
|
+
cat = Item.from_xml(response)
|
105
|
+
end
|
106
|
+
# Store connection in object for future use
|
107
|
+
cat.connection = connection
|
108
|
+
# Done
|
109
|
+
return cat
|
110
|
+
#rescue
|
111
|
+
# raise AMEE::BadData.new("Couldn't load ProfileItem. Check that your URL is correct.")
|
112
|
+
end
|
113
|
+
|
5
114
|
def self.create(profile, data_item_uid, options = {})
|
6
115
|
# Send data to path
|
7
116
|
options.merge! :dataItemUid => data_item_uid
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: Floppy-amee
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Smith
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-09-
|
12
|
+
date: 2008-09-12 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|