amee 2.0.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ module AMEE
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
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,82 @@
1
+ module AMEE
2
+ module Rails
3
+
4
+ def self.connection(options = {})
5
+ Connection.global(options)
6
+ end
7
+
8
+ class Connection
9
+ def self.global(options = {})
10
+ unless @connection
11
+ @connection = self.connect($AMEE_CONFIG['server'], $AMEE_CONFIG['username'], $AMEE_CONFIG['password'], options)
12
+ # Also store as $amee for backwards compatibility, though this is now deprecated
13
+ $amee = @connection
14
+ end
15
+ @connection
16
+ end
17
+ protected
18
+ def self.connect(server, username, password, options)
19
+ connection = AMEE::Connection.new(server, username, password, options)
20
+ connection.authenticate
21
+ return connection
22
+ end
23
+ end
24
+
25
+ def self.included(base)
26
+ base.extend ClassMethods
27
+ end
28
+
29
+ module ClassMethods
30
+ def has_amee_profile(options = {})
31
+ # Include the instance methods for creation and desctruction
32
+ include InstanceMethods
33
+ # Install callbacks
34
+ before_validation_on_create :amee_create
35
+ alias_method_chain :save, :amee
36
+ before_destroy :amee_destroy
37
+ # Check that this object has an AMEE profile UID when saving
38
+ validates_presence_of :amee_profile
39
+ end
40
+ end
41
+
42
+ module InstanceMethods
43
+
44
+ def save_with_amee
45
+ save_without_amee && amee_save
46
+ end
47
+
48
+ def amee_create
49
+ # Create profile
50
+ profile = AMEE::Profile::Profile.create(amee_connection)
51
+ self.amee_profile = profile.uid
52
+ end
53
+
54
+ def amee_save
55
+ # This is only here to be overridden
56
+ return true
57
+ end
58
+
59
+ def amee_destroy
60
+ # Delete profile
61
+ AMEE::Profile::Profile.delete(amee_connection, amee_profile)
62
+ rescue
63
+ puts "Couldn't remove profile #{amee_profile}"
64
+ end
65
+
66
+ def amee_connection
67
+ # Should be overridden by code which doesn't use the global AMEE connection
68
+ AMEE::Rails.connection
69
+ end
70
+
71
+ end
72
+
73
+ end
74
+ end
75
+
76
+ if Object.const_defined?("ActionController")
77
+ class ActionController::Base
78
+ def global_amee_connection(options={})
79
+ AMEE::Rails.connection(options)
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,90 @@
1
+ module AMEE
2
+ module Shell
3
+
4
+ def amee_help
5
+ puts "AMEE shell - version #{AMEE::VERSION::STRING}"
6
+ puts "--------------------------"
7
+ puts "Commands:"
8
+ puts " ls"
9
+ puts " - display contents of current category."
10
+ puts " cd 'path'"
11
+ puts " - change category. Path must be a quoted string. You can use things like '/data', '..', or 'subcategory'."
12
+ puts " pwd"
13
+ puts " - display current category path."
14
+ puts " cat 'name'"
15
+ puts " - display contents of data item called 'name' within the current category."
16
+ puts " set_value 'item_name', 'value_name', value"
17
+ puts " - set the value 'value_name' inside 'item_name' to value."
18
+ puts " amee_help"
19
+ puts " - display this help text."
20
+ end
21
+
22
+ def ls
23
+ puts "Categories:"
24
+ @@category.children.each do |c|
25
+ puts " - #{c[:path]}"
26
+ end
27
+ puts "Items:"
28
+ @@category.items.each do |i|
29
+ puts " - #{i[:path]} (#{i[:label]})"
30
+ end
31
+ nil
32
+ end
33
+
34
+ def pwd
35
+ @@category.full_path
36
+ end
37
+
38
+ def cd(path)
39
+ if path == '..'
40
+ path_components = @@category.full_path.split('/')
41
+ path = path_components.first(path_components.size - 1).join('/')
42
+ elsif !path.match(/^\/.*/)
43
+ path = @@category.full_path + '/' + path
44
+ end
45
+ @@category = AMEE::Data::Category.get($connection, path)
46
+ @@category.full_path
47
+ end
48
+
49
+ def cat(name)
50
+ item = @@category.items.detect { |i| i[:path].match("^#{name}") }
51
+ fullpath = "#{@@category.full_path}/#{item[:path]}"
52
+ item = AMEE::Data::Item.get($connection, fullpath)
53
+ puts fullpath
54
+ puts "Label: #{item.label}"
55
+ puts "Values:"
56
+ item.values.each do |v|
57
+ puts " - #{v[:name]}: #{v[:value]}"
58
+ end
59
+ nil
60
+ end
61
+
62
+ def set_value(item, name, value)
63
+ item = @@category.items.detect { |i| i[:path].match("^#{item}") }
64
+ fullpath = "#{@@category.full_path}/#{item[:path]}/#{name}"
65
+ itemval = AMEE::Data::ItemValue.get($connection, fullpath)
66
+ itemval.value = value
67
+ itemval.save!
68
+ end
69
+
70
+ end
71
+ end
72
+
73
+ dir = File.dirname(__FILE__) + "/.."
74
+ $LOAD_PATH << dir unless $LOAD_PATH.include?(dir)
75
+
76
+ require 'rubygems'
77
+ require 'amee'
78
+ include AMEE::Shell
79
+
80
+ # Set up connection
81
+ $connection = AMEE::Connection.new(ENV['AMEE_SERVER'], ENV['AMEE_USERNAME'], ENV['AMEE_PASSWORD'])
82
+
83
+ if $connection.valid?
84
+ # Change to root of data api to get going
85
+ cd '/data'
86
+ # Display AMEE details
87
+ amee_help
88
+ else
89
+ puts "Can't connect to AMEE - please check details"
90
+ end
@@ -0,0 +1,111 @@
1
+ module AMEE
2
+ module Admin
3
+
4
+ class User < AMEE::Object
5
+
6
+ attr_reader :username
7
+ attr_reader :name
8
+ attr_reader :email
9
+ attr_reader :api_version
10
+ attr_reader :status
11
+
12
+ def initialize(data = {})
13
+ @username = data[:username]
14
+ @name = data[:name]
15
+ @email = data[:email]
16
+ @status = data[:status]
17
+ @api_version = data[:api_version].to_f rescue nil
18
+ @environment_uid = data[:environment_uid]
19
+ super
20
+ end
21
+
22
+ def self.parse(connection, response)
23
+ # Parse data from response
24
+ if response.is_json?
25
+ user = User.from_json(response)
26
+ else
27
+ user = User.from_xml(response)
28
+ end
29
+ # Store connection in object for future use
30
+ user.connection = connection
31
+ # Done
32
+ return user
33
+ end
34
+
35
+ def self.from_json(json)
36
+ # Read JSON
37
+ doc = JSON.parse(json)
38
+ data = {}
39
+ data[:environment_uid] = doc['user']['environment']['uid']
40
+ data[:uid] = doc['user']['uid']
41
+ data[:created] = DateTime.parse(doc['user']['created'])
42
+ data[:modified] = DateTime.parse(doc['user']['modified'])
43
+ data[:username] = doc['user']['username']
44
+ data[:name] = doc['user']['name']
45
+ data[:email] = doc['user']['email']
46
+ data[:api_version] = doc['user']['apiVersion']
47
+ data[:status] = doc['user']['status']
48
+ # Create object
49
+ User.new(data)
50
+ rescue
51
+ raise AMEE::BadData.new("Couldn't load User from JSON. Check that your URL is correct.\n#{json}")
52
+ end
53
+
54
+ def self.from_xml(xml)
55
+ # Parse data from response into hash
56
+ doc = REXML::Document.new(xml)
57
+ data = {}
58
+ data[:environment_uid] = REXML::XPath.first(doc, "//Environment/@uid").to_s
59
+ data[:uid] = REXML::XPath.first(doc, "//User/@uid").to_s
60
+ data[:created] = DateTime.parse(REXML::XPath.first(doc, "//User/@created").to_s)
61
+ data[:modified] = DateTime.parse(REXML::XPath.first(doc, "//User/@modified").to_s)
62
+ data[:username] = REXML::XPath.first(doc, "//User/Username").text
63
+ data[:name] = REXML::XPath.first(doc, "//User/Name").text
64
+ data[:email] = REXML::XPath.first(doc, "//User/Email").text
65
+ data[:api_version] = REXML::XPath.first(doc, "//User/ApiVersion").text
66
+ data[:status] = REXML::XPath.first(doc, "//User/Status").text
67
+ # Create object
68
+ User.new(data)
69
+ rescue
70
+ raise AMEE::BadData.new("Couldn't load User from XML. Check that your URL is correct.\n#{xml}")
71
+ end
72
+
73
+ def self.get(connection, path, options = {})
74
+ # Load data from path
75
+ response = connection.get(path, options).body
76
+ # Parse response
77
+ User.parse(connection, response)
78
+ rescue
79
+ raise AMEE::BadData.new("Couldn't load User. Check that your URL is correct.\n#{response}")
80
+ end
81
+
82
+ def update(options = {})
83
+ connection.put(full_path, options).body
84
+ AMEE::Admin::User.get(connection, full_path)
85
+ end
86
+
87
+ def self.create(connection, environment_uid, options = {})
88
+ unless options.is_a?(Hash)
89
+ raise AMEE::ArgumentError.new("Second argument must be a hash of options!")
90
+ end
91
+ # Send data
92
+ response = connection.post("/environments/#{environment_uid}/users", options).body
93
+ # Parse response
94
+ User.parse(connection, response)
95
+ rescue
96
+ raise AMEE::BadData.new("Couldn't create User. Check that your information is correct.\n#{response}")
97
+ end
98
+
99
+ def delete
100
+ connection.delete(full_path)
101
+ rescue
102
+ raise AMEE::BadData.new("Couldn't delete User. Check that your information is correct.")
103
+ end
104
+
105
+ def full_path
106
+ "/environments/#{@environment_uid}/users/#{uid}"
107
+ end
108
+
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,10 @@
1
+ module AMEE
2
+
3
+ module VERSION #:nodoc:
4
+ MAJOR = 2
5
+ MINOR = 0
6
+ TINY = 22
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+
10
+ end
@@ -0,0 +1,12 @@
1
+ require 'amee'
2
+ require 'amee/rails'
3
+
4
+ # Load config/amee.yml
5
+ amee_config = "#{RAILS_ROOT}/config/amee.yml"
6
+ if File.exist?(amee_config)
7
+ # Load config
8
+ $AMEE_CONFIG = YAML.load_file(amee_config)[RAILS_ENV]
9
+ end
10
+
11
+ # Add AMEE extensions into ActiveRecord::Base
12
+ ActiveRecord::Base.class_eval { include AMEE::Rails } if Object.const_defined?("ActiveRecord")
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: amee
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.25
5
+ platform: ruby
6
+ authors:
7
+ - James Smith
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-01 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description:
36
+ email: james@floppy.org.uk
37
+ executables:
38
+ - ameesh
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - README
45
+ - COPYING
46
+ - lib/amee.rb
47
+ - lib/amee/connection.rb
48
+ - lib/amee/data_item.rb
49
+ - lib/amee/exceptions.rb
50
+ - lib/amee/profile.rb
51
+ - lib/amee/profile_object.rb
52
+ - lib/amee/profile_category.rb
53
+ - lib/amee/profile_item.rb
54
+ - lib/amee/profile_item_value.rb
55
+ - lib/amee/version.rb
56
+ - lib/amee/data_category.rb
57
+ - lib/amee/data_item_value.rb
58
+ - lib/amee/data_object.rb
59
+ - lib/amee/object.rb
60
+ - lib/amee/shell.rb
61
+ - lib/amee/drill_down.rb
62
+ - lib/amee/rails.rb
63
+ - lib/amee/pager.rb
64
+ - lib/amee/item_definition.rb
65
+ - lib/amee/user.rb
66
+ - bin/ameesh
67
+ - examples/list_profiles.rb
68
+ - examples/create_profile.rb
69
+ - examples/create_profile_item.rb
70
+ - examples/view_data_category.rb
71
+ - examples/view_data_item.rb
72
+ - init.rb
73
+ - rails/init.rb
74
+ - amee.example.yml
75
+ has_rdoc: true
76
+ homepage: http://github.com/Floppy/amee-ruby
77
+ licenses: []
78
+
79
+ post_install_message:
80
+ rdoc_options: []
81
+
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ version:
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ version:
96
+ requirements: []
97
+
98
+ rubyforge_project:
99
+ rubygems_version: 1.3.5
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Ruby interface to the AMEE carbon calculator
103
+ test_files: []
104
+