Floppy-amee 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -21,7 +21,7 @@ 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.
24
+ examples/create_profile.rb for details. You can also load ProfileCategories.
25
25
 
26
26
  The gem will use the AMEE JSON API if the JSON gem is installed on the local
27
27
  system. Otherwise the XML API will be used.
@@ -23,5 +23,5 @@ end.parse!
23
23
  connection = AMEE::Connection.new(options[:server], options[:username], options[:password])
24
24
 
25
25
  # Create a new profile
26
- profile = AMEE::Profile.create(connection)
26
+ profile = AMEE::Profile::Profile.create(connection)
27
27
  puts "#{profile.uid} created"
@@ -20,7 +20,7 @@ end.parse!
20
20
  connection = AMEE::Connection.new(options[:server], options[:username], options[:password])
21
21
 
22
22
  # List all available profiles
23
- profiles = AMEE::Profile.list(connection)
23
+ profiles = AMEE::Profile::Profile.list(connection)
24
24
  puts "#{profiles.size} #{profiles.size == 1 ? "profile" : "profiles"} available in AMEE:"
25
25
  profiles.each do |p|
26
26
  puts p.uid
data/lib/amee.rb CHANGED
@@ -22,4 +22,5 @@ require 'amee/profile_object'
22
22
  require 'amee/data_category'
23
23
  require 'amee/data_item'
24
24
  require 'amee/data_item_value'
25
- require 'amee/profile'
25
+ require 'amee/profile'
26
+ require 'amee/profile_category'
@@ -2,7 +2,7 @@ require 'date'
2
2
 
3
3
  module AMEE
4
4
  module Data
5
- class Category < AMEE::DataObject
5
+ class Category < AMEE::Data::Object
6
6
 
7
7
  def initialize(data = {})
8
8
  @children = data ? data[:children] : []
@@ -1,6 +1,6 @@
1
1
  module AMEE
2
2
  module Data
3
- class Item < AMEE::DataObject
3
+ class Item < AMEE::Data::Object
4
4
 
5
5
  def initialize(data = {})
6
6
  @values = data ? data[:values] : []
@@ -1,6 +1,6 @@
1
1
  module AMEE
2
2
  module Data
3
- class ItemValue < AMEE::DataObject
3
+ class ItemValue < AMEE::Data::Object
4
4
 
5
5
  def initialize(data = {})
6
6
  @value = data ? data[:value] : nil
@@ -1,9 +1,11 @@
1
1
  module AMEE
2
- class DataObject < AMEE::Object
2
+ module Data
3
+ class Object < AMEE::Object
3
4
 
4
- def full_path
5
- "/data#{@path}"
5
+ def full_path
6
+ "/data#{@path}"
7
+ end
8
+
6
9
  end
7
-
8
10
  end
9
11
  end
data/lib/amee/profile.rb CHANGED
@@ -1,93 +1,95 @@
1
1
  module AMEE
2
- class Profile < AMEE::ProfileObject
3
-
4
- def self.list(connection)
5
- # Load data from path
6
- response = connection.get('/profiles')
7
- # Parse data from response
8
- profiles = []
9
- if response.is_json?
10
- # Read JSON
11
- doc = JSON.parse(response)
12
- doc['profiles'].each do |p|
2
+ module Profile
3
+ class Profile < AMEE::Profile::Object
4
+
5
+ def self.list(connection)
6
+ # Load data from path
7
+ response = connection.get('/profiles')
8
+ # Parse data from response
9
+ profiles = []
10
+ if response.is_json?
11
+ # Read JSON
12
+ doc = JSON.parse(response)
13
+ doc['profiles'].each do |p|
14
+ data = {}
15
+ data[:uid] = p['uid']
16
+ data[:created] = DateTime.parse(p['created'])
17
+ data[:modified] = DateTime.parse(p['modified'])
18
+ data[:name] = p['name']
19
+ data[:path] = "/#{p['path']}"
20
+ # Create profile
21
+ profile = Profile.new(data)
22
+ # Store in array
23
+ profiles << profile
24
+ end
25
+ else
26
+ # Read XML
27
+ doc = REXML::Document.new(response)
28
+ REXML::XPath.each(doc, '/Resources/ProfilesResource/Profiles/Profile') do |p|
29
+ data = {}
30
+ data[:uid] = p.attributes['uid'].to_s
31
+ data[:created] = DateTime.parse(p.attributes['created'].to_s)
32
+ data[:modified] = DateTime.parse(p.attributes['modified'].to_s)
33
+ data[:name] = p.elements['Name'].text || data[:uid]
34
+ data[:path] = "/#{p.elements['Path'].text || data[:uid]}"
35
+ # Create profile
36
+ profile = Profile.new(data)
37
+ # Store connection in profile object
38
+ profile.connection = connection
39
+ # Store in array
40
+ profiles << profile
41
+ end
42
+ end
43
+ # Done
44
+ return profiles
45
+ rescue
46
+ raise AMEE::BadData.new("Couldn't load Profile list.")
47
+ end
48
+
49
+ def self.create(connection)
50
+ # Create new profile
51
+ response = connection.post('/profiles', :profile => true)
52
+ # Parse data from response
53
+ if response.is_json?
54
+ # Read JSON
55
+ doc = JSON.parse(response)
56
+ p = doc['profile']
13
57
  data = {}
14
58
  data[:uid] = p['uid']
15
59
  data[:created] = DateTime.parse(p['created'])
16
60
  data[:modified] = DateTime.parse(p['modified'])
17
61
  data[:name] = p['name']
18
- data[:path] = "/#{p['path']}"
62
+ data[:path] = p['path']
19
63
  # Create profile
20
- profile = AMEE::Profile.new(data)
21
- # Store in array
22
- profiles << profile
23
- end
24
- else
25
- # Read XML
26
- doc = REXML::Document.new(response)
27
- REXML::XPath.each(doc, '/Resources/ProfilesResource/Profiles/Profile') do |p|
64
+ profile = Profile.new(data)
65
+ # Done
66
+ return profile
67
+ else
68
+ # Read XML
69
+ doc = REXML::Document.new(response)
70
+ p = REXML::XPath.first(doc, '/Resources/ProfilesResource/Profile')
28
71
  data = {}
29
72
  data[:uid] = p.attributes['uid'].to_s
30
73
  data[:created] = DateTime.parse(p.attributes['created'].to_s)
31
74
  data[:modified] = DateTime.parse(p.attributes['modified'].to_s)
32
75
  data[:name] = p.elements['Name'].text || data[:uid]
33
- data[:path] = "/#{p.elements['Path'].text || data[:uid]}"
76
+ data[:path] = p.elements['Path'].text || data[:uid]
34
77
  # Create profile
35
- profile = AMEE::Profile.new(data)
78
+ profile = Profile.new(data)
36
79
  # Store connection in profile object
37
80
  profile.connection = connection
38
- # Store in array
39
- profiles << profile
81
+ # Done
82
+ return profile
40
83
  end
84
+ rescue
85
+ raise AMEE::BadData.new("Couldn't create Profile.")
41
86
  end
42
- # Done
43
- return profiles
44
- rescue
45
- raise AMEE::BadData.new("Couldn't load Profile list.")
46
- end
47
87
 
48
- def self.create(connection)
49
- # Create new profile
50
- response = connection.post('/profiles', :profile => true)
51
- # Parse data from response
52
- if response.is_json?
53
- # Read JSON
54
- doc = JSON.parse(response)
55
- p = doc['profile']
56
- data = {}
57
- data[:uid] = p['uid']
58
- data[:created] = DateTime.parse(p['created'])
59
- data[:modified] = DateTime.parse(p['modified'])
60
- data[:name] = p['name']
61
- data[:path] = p['path']
62
- # Create profile
63
- profile = AMEE::Profile.new(data)
64
- # Done
65
- return profile
66
- else
67
- # Read XML
68
- doc = REXML::Document.new(response)
69
- p = REXML::XPath.first(doc, '/Resources/ProfilesResource/Profile')
70
- data = {}
71
- data[:uid] = p.attributes['uid'].to_s
72
- data[:created] = DateTime.parse(p.attributes['created'].to_s)
73
- data[:modified] = DateTime.parse(p.attributes['modified'].to_s)
74
- data[:name] = p.elements['Name'].text || data[:uid]
75
- data[:path] = p.elements['Path'].text || data[:uid]
76
- # Create profile
77
- profile = AMEE::Profile.new(data)
78
- # Store connection in profile object
79
- profile.connection = connection
80
- # Done
81
- return profile
82
- end
83
- rescue
84
- raise AMEE::BadData.new("Couldn't create Profile.")
88
+ # def delete
89
+ # # Create new profile
90
+ # response = connection.delete(full_path)
91
+ # end
92
+
85
93
  end
86
-
87
- # def delete
88
- # # Create new profile
89
- # response = connection.delete(full_path)
90
- # end
91
-
92
94
  end
93
95
  end
@@ -0,0 +1,136 @@
1
+ require 'date'
2
+
3
+ module AMEE
4
+ module Profile
5
+ class Category < AMEE::Profile::Object
6
+
7
+ def initialize(data = {})
8
+ @children = data ? data[:children] : []
9
+ @items = data ? data[:items] : []
10
+ @total_amount_per_month = data[:total_amount_per_month]
11
+ @values = data[:values]
12
+ super
13
+ end
14
+
15
+ attr_reader :children
16
+ attr_reader :items
17
+ attr_reader :total_amount_per_month
18
+ attr_reader :values
19
+
20
+ def self.from_json(json)
21
+ # Parse json
22
+ doc = JSON.parse(json)
23
+ data = {}
24
+ data[:profile_uid] = doc['profile']['uid']
25
+ data[:profile_date] = DateTime.strptime(doc['profileDate'], "%Y%m")
26
+ data[:name] = doc['dataCategory']['name']
27
+ data[:path] = doc['path']
28
+ data[:total_amount_per_month] = doc['totalAmountPerMonth']
29
+ data[:children] = []
30
+ doc['children']['dataCategories'].each do |child|
31
+ category_data = {}
32
+ category_data[:name] = child['name']
33
+ category_data[:path] = child['path']
34
+ category_data[:uid] = child['uid']
35
+ data[:children] << category_data
36
+ end
37
+ data[:items] = []
38
+ if doc['children']['profileItems']['rows']
39
+ doc['children']['profileItems']['rows'].each do |item|
40
+ item_data = {}
41
+ item_data[:values] = {}
42
+ item.each_pair do |key, value|
43
+ case key
44
+ when 'dataItemLabel', 'dataItemUid', 'name', 'path', 'uid'
45
+ item_data[key.to_sym] = value
46
+ when 'created', 'modified', 'label' # ignore these
47
+ nil
48
+ when 'validFrom'
49
+ item_data[:validFrom] = DateTime.strptime(value, "%Y%m%d")
50
+ when 'end'
51
+ item_data[:end] = (value == "true")
52
+ when 'amountPerMonth'
53
+ item_data[:amountPerMonth] = value.to_f
54
+ else
55
+ item_data[:values][key.to_sym] = value
56
+ end
57
+ end
58
+ data[:items] << item_data
59
+ end
60
+ end
61
+ # Create object
62
+ Category.new(data)
63
+ rescue
64
+ raise AMEE::BadData.new("Couldn't load ProfileCategory from JSON data. Check that your URL is correct.")
65
+ end
66
+
67
+ def self.from_xml(xml)
68
+ # Parse XML
69
+ doc = REXML::Document.new(xml)
70
+ data = {}
71
+ data[:profile_uid] = REXML::XPath.first(doc, "/Resources/ProfileCategoryResource/Profile/@uid").to_s
72
+ data[:profile_date] = DateTime.strptime(REXML::XPath.first(doc, "/Resources/ProfileCategoryResource/ProfileDate").text, "%Y%m")
73
+ data[:name] = REXML::XPath.first(doc, '/Resources/ProfileCategoryResource/DataCategory/Name').text
74
+ data[:path] = REXML::XPath.first(doc, '/Resources/ProfileCategoryResource/Path').text || ""
75
+ data[:total_amount_per_month] = REXML::XPath.first(doc, '/Resources/ProfileCategoryResource/TotalAmountPerMonth').text.to_f rescue nil
76
+ data[:children] = []
77
+ REXML::XPath.each(doc, '/Resources/ProfileCategoryResource/Children/ProfileCategories/DataCategory') do |child|
78
+ category_data = {}
79
+ category_data[:name] = child.elements['Name'].text
80
+ category_data[:path] = child.elements['Path'].text
81
+ category_data[:uid] = child.attributes['uid'].to_s
82
+ data[:children] << category_data
83
+ end
84
+ data[:items] = []
85
+ REXML::XPath.each(doc, '/Resources/ProfileCategoryResource/Children/ProfileItems/ProfileItem') do |item|
86
+ item_data = {}
87
+ item_data[:values] = {}
88
+ item.elements.each do |element|
89
+ key = element.name
90
+ value = element.text
91
+ case key
92
+ when 'dataItemLabel', 'dataItemUid', 'name', 'path'
93
+ item_data[key.to_sym] = value
94
+ when 'validFrom'
95
+ item_data[:validFrom] = DateTime.strptime(value, "%Y%m%d")
96
+ when 'end'
97
+ item_data[:end] = (value == "true")
98
+ when 'amountPerMonth'
99
+ item_data[:amountPerMonth] = value.to_f
100
+ else
101
+ item_data[:values][key.to_sym] = value
102
+ end
103
+ end
104
+ item_data[:uid] = item.attributes['uid'].to_s
105
+ data[:items] << item_data
106
+ end
107
+ # Create object
108
+ Category.new(data)
109
+ rescue
110
+ raise AMEE::BadData.new("Couldn't load ProfileCategory from XML data. Check that your URL is correct.")
111
+ end
112
+
113
+ def self.get(connection, path)
114
+ # Load data from path
115
+ response = connection.get(path)
116
+ # Parse data from response
117
+ if response.is_json?
118
+ cat = Category.from_json(response)
119
+ else
120
+ cat = Category.from_xml(response)
121
+ end
122
+ # Store connection in object for future use
123
+ cat.connection = connection
124
+ # Done
125
+ return cat
126
+ rescue
127
+ raise AMEE::BadData.new("Couldn't load ProfileCategory. Check that your URL is correct.")
128
+ end
129
+
130
+ def child(child_path)
131
+ AMEE::Profile::Category.get(connection, "#{full_path}/#{child_path}")
132
+ end
133
+
134
+ end
135
+ end
136
+ end
@@ -1,9 +1,20 @@
1
1
  module AMEE
2
- class ProfileObject < AMEE::Object
3
-
4
- def full_path
5
- "/profiles#{@path}"
6
- end
2
+ module Profile
3
+ class Object < AMEE::Object
4
+
5
+ def initialize(options = {})
6
+ @profile_uid = options[:profile_uid]
7
+ @profile_date = options[:profile_date]
8
+ super
9
+ end
10
+
11
+ attr_reader :profile_uid
12
+ attr_reader :profile_date
13
+
14
+ def full_path
15
+ "/profiles#{'/' if @profile_uid}#{@profile_uid}#{@path}"
16
+ end
7
17
 
18
+ end
8
19
  end
9
20
  end
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.3.1
4
+ version: 0.4.0
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-08-29 00:00:00 -07:00
12
+ date: 2008-09-03 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -30,6 +30,7 @@ files:
30
30
  - lib/amee/exceptions.rb
31
31
  - lib/amee/profile.rb
32
32
  - lib/amee/profile_object.rb
33
+ - lib/amee/profile_category.rb
33
34
  - lib/amee/version.rb
34
35
  - lib/amee/data_category.rb
35
36
  - lib/amee/data_item_value.rb