amee 2.7.0 → 3.0.1
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.
- data/{CHANGELOG → CHANGELOG.txt} +11 -0
- data/Gemfile.lock +37 -0
- data/LICENSE.txt +27 -0
- data/{README → README.txt} +6 -4
- data/Rakefile +84 -17
- data/VERSION +1 -1
- data/amee.gemspec +172 -0
- data/bin/ameesh +3 -0
- data/examples/create_profile.rb +3 -0
- data/examples/create_profile_item.rb +3 -0
- data/examples/list_profiles.rb +3 -0
- data/examples/view_data_category.rb +3 -0
- data/examples/view_data_item.rb +3 -0
- data/examples/view_profile_item.rb +3 -0
- data/init.rb +3 -0
- data/lib/amee/collection.rb +3 -0
- data/lib/amee/connection.rb +3 -0
- data/lib/amee/data_category.rb +3 -0
- data/lib/amee/data_item.rb +3 -0
- data/lib/amee/data_item_value.rb +3 -0
- data/lib/amee/data_item_value_history.rb +3 -0
- data/lib/amee/data_object.rb +3 -0
- data/lib/amee/drill_down.rb +3 -0
- data/lib/amee/exceptions.rb +3 -0
- data/lib/amee/item_definition.rb +3 -0
- data/lib/amee/item_value_definition.rb +3 -0
- data/lib/amee/logger.rb +3 -0
- data/lib/amee/object.rb +3 -0
- data/lib/amee/pager.rb +3 -0
- data/lib/amee/parse_helper.rb +3 -0
- data/lib/amee/profile.rb +3 -0
- data/lib/amee/profile_category.rb +3 -0
- data/lib/amee/profile_item.rb +3 -0
- data/lib/amee/profile_item_value.rb +3 -0
- data/lib/amee/profile_object.rb +3 -0
- data/lib/amee/rails.rb +3 -0
- data/lib/amee/shell.rb +3 -0
- data/lib/amee/user.rb +3 -0
- data/lib/amee/v3/collection.rb +61 -0
- data/lib/amee/v3/connection.rb +99 -0
- data/lib/amee/v3/item_definition.rb +73 -0
- data/lib/amee/v3/item_value_definition.rb +99 -0
- data/lib/amee/v3/item_value_definition_list.rb +53 -0
- data/lib/amee/v3/meta_helper.rb +28 -0
- data/lib/amee/v3/return_value_definition.rb +202 -0
- data/lib/amee/v3.rb +12 -0
- data/lib/amee.rb +4 -0
- data/rails/init.rb +3 -0
- data/spec/amee_spec.rb +3 -0
- data/spec/cache_spec.rb +3 -0
- data/spec/connection_spec.rb +3 -0
- data/spec/data_category_spec.rb +4 -1
- data/spec/data_item_spec.rb +3 -0
- data/spec/data_item_value_history_spec.rb +3 -0
- data/spec/data_item_value_spec.rb +3 -0
- data/spec/data_object_spec.rb +3 -0
- data/spec/drill_down_spec.rb +3 -0
- data/spec/fixtures/BD88D30D1214.xml +1 -0
- data/spec/fixtures/empty_return_value_definition_list.xml +6 -0
- data/spec/fixtures/itemdef.xml +20 -0
- data/spec/fixtures/itemdef_441BF4BEA15B.xml +14 -0
- data/spec/fixtures/itemdef_no_usages.xml +10 -0
- data/spec/fixtures/itemdef_one_usage.xml +14 -0
- data/spec/fixtures/ivdlist.xml +605 -0
- data/spec/fixtures/ivdlist_BD88D30D1214.xml +1 -0
- data/spec/fixtures/return_value_definition.xml +18 -0
- data/spec/fixtures/return_value_definition_list.xml +46 -0
- data/spec/item_definition_spec.rb +12 -9
- data/spec/item_value_definition_spec.rb +29 -19
- data/spec/logger_spec.rb +2 -2
- data/spec/object_spec.rb +3 -0
- data/spec/parse_helper_spec.rb +3 -0
- data/spec/profile_category_spec.rb +3 -0
- data/spec/profile_item_spec.rb +3 -0
- data/spec/profile_item_value_spec.rb +3 -0
- data/spec/profile_object_spec.rb +3 -0
- data/spec/profile_spec.rb +3 -0
- data/spec/rails_spec.rb +3 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/user_spec.rb +3 -0
- data/spec/v3/connection_spec.rb +169 -0
- data/spec/v3/item_definition_spec.rb +328 -0
- data/spec/v3/item_value_definition_spec.rb +116 -0
- data/spec/v3/return_value_definition_spec.rb +90 -0
- metadata +38 -10
- data/COPYING +0 -19
@@ -0,0 +1,61 @@
|
|
1
|
+
# Copyright (C) 2008-2011 AMEE UK Ltd. - http://www.amee.com
|
2
|
+
# Released as Open Source Software under the BSD 3-Clause license. See LICENSE.txt for details.
|
3
|
+
|
4
|
+
module AMEE
|
5
|
+
class Collection < Array
|
6
|
+
|
7
|
+
def v3
|
8
|
+
collectionpath=~/^\/3/
|
9
|
+
end
|
10
|
+
|
11
|
+
def fetch_with_v3
|
12
|
+
@options.merge! @pager.options if @pager
|
13
|
+
retries = [1] * connection.retries
|
14
|
+
begin
|
15
|
+
if @use_v3_connection == true
|
16
|
+
@response = @connection.v3_get(collectionpath, @options)
|
17
|
+
else
|
18
|
+
@response= @connection.get(collectionpath, @options).body
|
19
|
+
end
|
20
|
+
if @response.is_json?
|
21
|
+
@json = true
|
22
|
+
@doc = JSON.parse(@response)
|
23
|
+
else
|
24
|
+
@doc = load_xml_doc(@response)
|
25
|
+
end
|
26
|
+
rescue JSON::ParserError, Nokogiri::XML::SyntaxError
|
27
|
+
@connection.expire(collectionpath)
|
28
|
+
if delay = retries.shift
|
29
|
+
sleep delay
|
30
|
+
retry
|
31
|
+
else
|
32
|
+
raise
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
alias_method_chain :fetch, :v3
|
37
|
+
|
38
|
+
def each_page_with_v3
|
39
|
+
@pager=AMEE::Limiter.new(@options) if v3
|
40
|
+
# in v3 need to specify limit to start with, not in v2
|
41
|
+
begin
|
42
|
+
fetch
|
43
|
+
yield
|
44
|
+
if json
|
45
|
+
@pager = v3 ? AMEE::Limiter.from_json(doc,@pager.options.merge(@options)) : # json not built
|
46
|
+
AMEE::Pager.from_json(doc['pager'])
|
47
|
+
else
|
48
|
+
@pager = v3 ?
|
49
|
+
AMEE::Limiter.from_xml(doc.xpath('/Representation/*').first,
|
50
|
+
@pager.options.merge(@options)) :
|
51
|
+
AMEE::Pager.from_xml(doc.xpath('/Resources//Pager').first)
|
52
|
+
end
|
53
|
+
break if @max && length>=@max
|
54
|
+
end while @pager && @pager.next! #pager is nil if no pager in response,
|
55
|
+
# pager.next! is false if @pager said current=last.
|
56
|
+
end
|
57
|
+
alias_method_chain :each_page, :v3
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
@@ -0,0 +1,99 @@
|
|
1
|
+
# Copyright (C) 2008-2011 AMEE UK Ltd. - http://www.amee.com
|
2
|
+
# Released as Open Source Software under the BSD 3-Clause license. See LICENSE.txt for details.
|
3
|
+
|
4
|
+
module AMEE
|
5
|
+
class Connection
|
6
|
+
def v3_connection
|
7
|
+
@v3_http ||= begin
|
8
|
+
@v3_http = Net::HTTP.new(v3_hostname, @port)
|
9
|
+
if @ssl == true
|
10
|
+
@v3_http.use_ssl = true
|
11
|
+
if File.exists? RootCA
|
12
|
+
@v3_http.ca_file = RootCA
|
13
|
+
@v3_http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
14
|
+
@v3_http.verify_depth = 5
|
15
|
+
end
|
16
|
+
end
|
17
|
+
@v3_http.set_debug_output($stdout) if @debug
|
18
|
+
@v3_http
|
19
|
+
end
|
20
|
+
end
|
21
|
+
def v3_get(path, options = {})
|
22
|
+
# Create URL parameters
|
23
|
+
unless options.empty?
|
24
|
+
path += "?" + options.map { |x| "#{CGI::escape(x[0].to_s)}=#{CGI::escape(x[1].to_s)}" }.join('&')
|
25
|
+
end
|
26
|
+
cache(path) { v3_request(Net::HTTP::Get.new(path)) }
|
27
|
+
end
|
28
|
+
def v3_put(path, options = {})
|
29
|
+
expire_matching "#{parent_path(path)}.*"
|
30
|
+
put = Net::HTTP::Put.new(path)
|
31
|
+
if options[:body]
|
32
|
+
put.body = options[:body]
|
33
|
+
put['Content-Type'] = content_type :xml if options[:body].is_xml?
|
34
|
+
put['Content-Type'] = content_type :json if options[:body].is_json?
|
35
|
+
else
|
36
|
+
put.set_form_data(options)
|
37
|
+
end
|
38
|
+
v3_request put
|
39
|
+
end
|
40
|
+
def v3_post(path, options = {})
|
41
|
+
expire_matching "#{raw_path(path)}.*"
|
42
|
+
post = Net::HTTP::Post.new(path)
|
43
|
+
returnobj=options.delete(:returnobj) || false
|
44
|
+
post.set_form_data(options)
|
45
|
+
v3_request post,returnobj
|
46
|
+
end
|
47
|
+
def v3_delete(path, options = {})
|
48
|
+
expire_matching "#{parent_path(path)}.*"
|
49
|
+
delete = Net::HTTP::Delete.new(path)
|
50
|
+
v3_request delete
|
51
|
+
end
|
52
|
+
def v3_auth
|
53
|
+
# now the same as v2, i.e.
|
54
|
+
[@username,@password]
|
55
|
+
end
|
56
|
+
private
|
57
|
+
def v3_hostname
|
58
|
+
unless @server.starts_with?("platform-api-")
|
59
|
+
if @server.starts_with?("platform-")
|
60
|
+
@server.gsub("platform-", "platform-api-")
|
61
|
+
else
|
62
|
+
"platform-api-#{@server}"
|
63
|
+
end
|
64
|
+
else
|
65
|
+
@server
|
66
|
+
end
|
67
|
+
end
|
68
|
+
def v3_request(req,returnobj=false)
|
69
|
+
# Open HTTP connection
|
70
|
+
v3_connection.start
|
71
|
+
# Set auth
|
72
|
+
req.basic_auth *v3_auth
|
73
|
+
# Do request
|
74
|
+
timethen=Time.now
|
75
|
+
response = send_request(v3_connection, req, :xml)
|
76
|
+
Logger.log.debug("Requested #{req.class} at #{req.path} with #{req.body}, taking #{(Time.now-timethen)*1000} miliseconds")
|
77
|
+
v3_response_ok? response, req
|
78
|
+
returnobj ? response : response.body
|
79
|
+
ensure
|
80
|
+
v3_connection.finish if v3_connection.started?
|
81
|
+
end
|
82
|
+
|
83
|
+
def v3_response_ok?(response, request)
|
84
|
+
case response.code
|
85
|
+
when '200', '201', '204'
|
86
|
+
return true
|
87
|
+
when '404'
|
88
|
+
raise AMEE::NotFound.new("The URL was not found on the server.\nRequest: #{request.method} #{request.path}")
|
89
|
+
when '403'
|
90
|
+
raise AMEE::PermissionDenied.new("You do not have permission to perform the requested operation.\nRequest: #{request.method} #{request.path}\n#{request.body}\Response: #{response.body}")
|
91
|
+
when '401'
|
92
|
+
raise AMEE::AuthFailed.new("Authentication failed. Please check your username and password.")
|
93
|
+
when '400'
|
94
|
+
raise AMEE::BadRequest.new("Bad request. This is probably due to malformed input data.\nRequest: #{request.method} #{request.path}\n#{request.body}\Response: #{response.body}")
|
95
|
+
end
|
96
|
+
raise AMEE::UnknownError.new("An error occurred while talking to AMEE: HTTP response code #{response.code}.\nRequest: #{request.method} #{request.path}\n#{request.body}\Response: #{response.body}")
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# Copyright (C) 2008-2011 AMEE UK Ltd. - http://www.amee.com
|
2
|
+
# Released as Open Source Software under the BSD 3-Clause license. See LICENSE.txt for details.
|
3
|
+
|
4
|
+
module AMEE
|
5
|
+
module Admin
|
6
|
+
class ItemDefinition < AMEE::Object
|
7
|
+
|
8
|
+
include ParseHelper
|
9
|
+
|
10
|
+
def initialize_with_v3(data = {})
|
11
|
+
@usages = data[:usages] || []
|
12
|
+
initialize_without_v3(data)
|
13
|
+
end
|
14
|
+
alias_method_chain :initialize, :v3
|
15
|
+
|
16
|
+
attr_accessor :usages
|
17
|
+
|
18
|
+
def self.metaxmlpathpreamble
|
19
|
+
'/Representation/ItemDefinition/'
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.load(connection,uid,options={})
|
23
|
+
ItemDefinition.v3_get(connection,uid,options)
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.v3_get(connection, uid, options={})
|
27
|
+
# Load data from path
|
28
|
+
response = connection.v3_get("/#{AMEE_API_VERSION}/definitions/#{uid};full", options)
|
29
|
+
# Parse response
|
30
|
+
item_definition = ItemDefinition.parse(connection, response, false)
|
31
|
+
# Done
|
32
|
+
return item_definition
|
33
|
+
rescue
|
34
|
+
raise AMEE::BadData.new("Couldn't load ItemDefinition. Check that your URL is correct.\n#{$!}\n#{response}")
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.from_xml_with_v3(xml, is_list = true)
|
38
|
+
return self.from_xml_without_v3(xml, is_list) if xml.include?('<Resources>')
|
39
|
+
# Parse data from response into hash
|
40
|
+
doc = load_xml_doc(xml)
|
41
|
+
data = {}
|
42
|
+
data[:uid] = x '@uid', :doc => doc, :meta => true
|
43
|
+
data[:created] = DateTime.parse(x '@created', :doc => doc, :meta => true)
|
44
|
+
data[:modified] = DateTime.parse(x '@modified', :doc => doc, :meta => true)
|
45
|
+
data[:name] = x 'Name', :doc => doc, :meta => true
|
46
|
+
data[:drillDown] = x('DrillDown', :doc => doc, :meta => true).split(",") rescue nil
|
47
|
+
data[:usages] = [(x 'Usages/Usage/Name', :doc => doc, :meta => true)].flatten.delete_if{|x| x.nil?}
|
48
|
+
# Create object
|
49
|
+
ItemDefinition.new(data)
|
50
|
+
rescue
|
51
|
+
raise AMEE::BadData.new("Couldn't load ItemDefinition from XML. Check that your URL is correct.\n#{$!}\n#{xml}")
|
52
|
+
end
|
53
|
+
class << self
|
54
|
+
alias_method_chain :from_xml, :v3
|
55
|
+
end
|
56
|
+
|
57
|
+
def save!
|
58
|
+
save_options = {
|
59
|
+
:usages => @usages.join(','),
|
60
|
+
:name => @name
|
61
|
+
}
|
62
|
+
@connection.v3_put("/#{AMEE_API_VERSION}/definitions/#{@uid}",save_options)
|
63
|
+
end
|
64
|
+
|
65
|
+
def expire_cache_with_v3
|
66
|
+
@connection.expire_matching("/#{AMEE_API_VERSION}/definitions/#{@uid}.*")
|
67
|
+
expire_cache_without_v3
|
68
|
+
end
|
69
|
+
alias_method_chain :expire_cache, :v3
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
# Copyright (C) 2008-2011 AMEE UK Ltd. - http://www.amee.com
|
2
|
+
# Released as Open Source Software under the BSD 3-Clause license. See LICENSE.txt for details.
|
3
|
+
|
4
|
+
module AMEE
|
5
|
+
module Admin
|
6
|
+
class ItemValueDefinition
|
7
|
+
include MetaHelper
|
8
|
+
|
9
|
+
def initialize_with_v3(data = {})
|
10
|
+
storemeta(data[:meta]) if data[:meta]
|
11
|
+
initialize_without_v3(data)
|
12
|
+
end
|
13
|
+
alias_method_chain :initialize, :v3
|
14
|
+
|
15
|
+
def metapath
|
16
|
+
"/#{AMEE_API_VERSION}/definitions/#{itemdefuid}/values/#{uid};wikiDoc;usages"
|
17
|
+
end
|
18
|
+
def metaxmlpathpreamble
|
19
|
+
'/Representation/ItemValueDefinition/'
|
20
|
+
end
|
21
|
+
def storemeta(options)
|
22
|
+
@meta=OpenStruct.new
|
23
|
+
@meta.wikidoc= options[:wikidoc]
|
24
|
+
@usages = options[:usages] || []
|
25
|
+
end
|
26
|
+
def parsemeta
|
27
|
+
data = {}
|
28
|
+
data[:wikidoc]=x 'WikiDoc', :meta => true
|
29
|
+
names = x('Usages/Usage/Name', :meta => true)
|
30
|
+
types = x('Usages/Usage/Type', :meta => true)
|
31
|
+
data[:usages] = names && types ? Hash[*(names.zip(types.map{|x| x.downcase.to_sym})).flatten] : {}
|
32
|
+
storemeta(data)
|
33
|
+
end
|
34
|
+
def putmeta_args
|
35
|
+
str = <<EOF
|
36
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
37
|
+
<ItemValueDefinition>
|
38
|
+
<WikiDoc>#{meta.wikidoc}</WikiDoc>
|
39
|
+
<Usages>
|
40
|
+
EOF
|
41
|
+
@usages.each_pair do |name, type|
|
42
|
+
str += <<EOF
|
43
|
+
<Usage>
|
44
|
+
<Name>#{name}</Name>
|
45
|
+
<Type>#{type.to_s.upcase}</Type>
|
46
|
+
</Usage>
|
47
|
+
EOF
|
48
|
+
end
|
49
|
+
str += <<EOF
|
50
|
+
</Usages>
|
51
|
+
</ItemValueDefinition>
|
52
|
+
EOF
|
53
|
+
{:body => str}
|
54
|
+
end
|
55
|
+
def usages
|
56
|
+
@usages.keys
|
57
|
+
end
|
58
|
+
def clear_usages!
|
59
|
+
@usages = {}
|
60
|
+
end
|
61
|
+
def set_usage_type(usage, type)
|
62
|
+
loadmeta if @usages.nil?
|
63
|
+
@usages[usage] = type
|
64
|
+
end
|
65
|
+
def usage_type(usage)
|
66
|
+
loadmeta if @usages.nil?
|
67
|
+
@usages[usage] || :optional
|
68
|
+
end
|
69
|
+
def optional?(usage)
|
70
|
+
usage_type(usage) == :optional
|
71
|
+
end
|
72
|
+
def compulsory?(usage)
|
73
|
+
usage_type(usage) == :compulsory
|
74
|
+
end
|
75
|
+
def forbidden?(usage)
|
76
|
+
usage_type(usage) == :forbidden
|
77
|
+
end
|
78
|
+
def ignored?(usage)
|
79
|
+
usage_type(usage) == :ignored
|
80
|
+
end
|
81
|
+
def default
|
82
|
+
return nil if @default.nil?
|
83
|
+
case valuetype
|
84
|
+
when "DECIMAL", "DOUBLE"
|
85
|
+
@default.to_f rescue nil
|
86
|
+
else
|
87
|
+
@default
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def expire_cache_with_v3
|
92
|
+
@connection.expire_matching("/#{AMEE_API_VERSION}/definitions/#{itemdefuid}/values/#{uid}.*")
|
93
|
+
expire_cache_without_v3
|
94
|
+
end
|
95
|
+
alias_method_chain :expire_cache, :v3
|
96
|
+
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# Copyright (C) 2008-2011 AMEE UK Ltd. - http://www.amee.com
|
2
|
+
# Released as Open Source Software under the BSD 3-Clause license. See LICENSE.txt for details.
|
3
|
+
|
4
|
+
module AMEE
|
5
|
+
module Admin
|
6
|
+
|
7
|
+
class ItemValueDefinitionList < AMEE::Collection
|
8
|
+
|
9
|
+
def initialize_with_v3(connection,uid,options={}, &block)
|
10
|
+
@use_v3_connection = true
|
11
|
+
initialize_without_v3(connection, uid, options, &block)
|
12
|
+
end
|
13
|
+
alias_method_chain :initialize, :v3
|
14
|
+
|
15
|
+
def collectionpath
|
16
|
+
"/#{AMEE_API_VERSION}/definitions/#{@uid}/values;full"
|
17
|
+
end
|
18
|
+
|
19
|
+
def xmlcollectorpath
|
20
|
+
'/Representation/ItemValueDefinitions/ItemValueDefinition'
|
21
|
+
end
|
22
|
+
|
23
|
+
def parse_json(p)
|
24
|
+
raise AMEE::NotSupported
|
25
|
+
end
|
26
|
+
|
27
|
+
def parse_xml(p)
|
28
|
+
# This should really be handed off to ItemValueDefinition fully
|
29
|
+
data = {}
|
30
|
+
data[:itemdefuid] = @uid
|
31
|
+
data[:uid] = x '@uid',:doc => p
|
32
|
+
data[:name] = x 'Name',:doc => p || data[:uid]
|
33
|
+
data[:path] = x 'Path',:doc => p
|
34
|
+
data[:default] = x 'Value',:doc => p
|
35
|
+
data[:unit] = x 'Unit',:doc => p
|
36
|
+
data[:perunit] = x 'PerUnit',:doc => p
|
37
|
+
data[:valuetype] =x 'ValueDefinition/ValueType',:doc => p
|
38
|
+
data[:choices] =(x('Choices',:doc => p)||'').split(',')
|
39
|
+
data[:versions] =[(x('Versions/Version/Version',:doc => p))].flatten
|
40
|
+
data[:meta] = {}
|
41
|
+
data[:meta][:wikidoc]=x 'WikiDoc', :doc => p
|
42
|
+
names = x('Usages/Usage/Name', :doc => p)
|
43
|
+
types = x('Usages/Usage/Type', :doc => p)
|
44
|
+
data[:meta][:usages] = names && types ? Hash[*(names.zip(types.map{|x| x.downcase.to_sym})).flatten] : {}
|
45
|
+
data[:drill]=(x('DrillDown',:doc => p)=='true')
|
46
|
+
data[:from_profile]=(x('FromProfile',:doc => p)=='true')
|
47
|
+
data[:from_data]=(x('FromData',:doc => p)=='true')
|
48
|
+
data
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# Copyright (C) 2008-2011 AMEE UK Ltd. - http://www.amee.com
|
2
|
+
# Released as Open Source Software under the BSD 3-Clause license. See LICENSE.txt for details.
|
3
|
+
|
4
|
+
require 'ostruct'
|
5
|
+
|
6
|
+
module MetaHelper
|
7
|
+
def putmeta(args = {})
|
8
|
+
tags=args.delete(:tags)
|
9
|
+
args.merge! putmeta_args # Added in order to abstract PUTs away from providing arguments in the call. Eventually args param will be deprecated.
|
10
|
+
@connection.v3_put(metapath,args)
|
11
|
+
puttags(tags) if tags
|
12
|
+
loadmeta
|
13
|
+
end
|
14
|
+
def puttags(tags)
|
15
|
+
existing_tags=AMEE::Tags.new(@connection,:category=>uid)
|
16
|
+
existing_tags.reset_to(tags)
|
17
|
+
end
|
18
|
+
def loadmeta
|
19
|
+
resp=@connection.v3_get(metapath)
|
20
|
+
@doc=load_xml_doc(resp)
|
21
|
+
parsemeta
|
22
|
+
@meta
|
23
|
+
end
|
24
|
+
private :loadmeta
|
25
|
+
def meta
|
26
|
+
@meta||=loadmeta
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,202 @@
|
|
1
|
+
# Copyright (C) 2008-2011 AMEE UK Ltd. - http://www.amee.com
|
2
|
+
# Released as Open Source Software under the BSD 3-Clause license. See LICENSE.txt for details.
|
3
|
+
|
4
|
+
module AMEE
|
5
|
+
module Admin
|
6
|
+
|
7
|
+
class ReturnValueDefinitionList < AMEE::Collection
|
8
|
+
# TODO this class does not yet page through multiple pages
|
9
|
+
def initialize(connection,uid,options={})
|
10
|
+
@uid=uid
|
11
|
+
@use_v3_connection=true
|
12
|
+
super(connection,options)
|
13
|
+
end
|
14
|
+
def klass
|
15
|
+
ReturnValueDefinition
|
16
|
+
end
|
17
|
+
def collectionpath
|
18
|
+
"/#{AMEE_API_VERSION}/definitions/#{@uid}/returnvalues;full"
|
19
|
+
end
|
20
|
+
|
21
|
+
def jsoncollector
|
22
|
+
@doc['returnValueDefinitions']
|
23
|
+
end
|
24
|
+
def xmlcollectorpath
|
25
|
+
'/Representation/ReturnValueDefinitions/ReturnValueDefinition'
|
26
|
+
end
|
27
|
+
|
28
|
+
def parse_json(p)
|
29
|
+
data = {}
|
30
|
+
data[:itemdefuid] = @uid
|
31
|
+
data[:uid] = p['uid']
|
32
|
+
data[:name] = p['type']
|
33
|
+
data[:unit] = p['unit']
|
34
|
+
data[:perunit] = p['perunit']
|
35
|
+
data[:valuetype] = p['valueDefinition']['valueType']
|
36
|
+
data
|
37
|
+
end
|
38
|
+
def parse_xml(p)
|
39
|
+
data = {}
|
40
|
+
data[:itemdefuid] = @uid
|
41
|
+
data[:uid] = x '@uid',:doc => p
|
42
|
+
data[:name] = x 'Type',:doc => p || data[:uid]
|
43
|
+
data[:unit] = x 'Unit',:doc => p
|
44
|
+
data[:perunit] = x 'PerUnit',:doc => p
|
45
|
+
data
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class ReturnValueDefinition < AMEE::Object
|
50
|
+
|
51
|
+
def initialize(data = {})
|
52
|
+
@itemdefuid=data[:itemdefuid]
|
53
|
+
@name = data[:name]
|
54
|
+
@uid = data[:uid]
|
55
|
+
@type = data[:type]
|
56
|
+
@unit = data[:unit]
|
57
|
+
@perunit = data[:perunit]
|
58
|
+
@valuetype = data[:valuetype]
|
59
|
+
|
60
|
+
super
|
61
|
+
end
|
62
|
+
|
63
|
+
attr_reader :name,:uid,:itemdefuid, :type, :valuetype, :unit, :perunit
|
64
|
+
|
65
|
+
def self.list(connection)
|
66
|
+
ReturnValueDefinitionList.new(connection)
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.parse(connection, response)
|
70
|
+
# Parse data from response
|
71
|
+
if response.is_json?
|
72
|
+
item_definition = ReturnValueDefinition.from_json(response)
|
73
|
+
else
|
74
|
+
item_definition = ReturnValueDefinition.from_xml(response)
|
75
|
+
end
|
76
|
+
# Store connection in object for future use
|
77
|
+
item_definition.connection = connection
|
78
|
+
# Done
|
79
|
+
return item_definition
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.from_json(json)
|
83
|
+
# Read JSON
|
84
|
+
doc = JSON.parse(json)
|
85
|
+
data = {}
|
86
|
+
p=doc['returnValueDefinition']
|
87
|
+
data[:uid] = p['uid']
|
88
|
+
data[:itemdefuid] = p['itemDefinition']['uid']
|
89
|
+
data[:type] = p['type']
|
90
|
+
data[:path] = p['path']
|
91
|
+
data[:unit] = p['unit']
|
92
|
+
data[:perunit] = p['perUnit']
|
93
|
+
data[:valuetype] = p['valueDefinition']['valueType']
|
94
|
+
data[:default] = p['value']
|
95
|
+
|
96
|
+
# Create object
|
97
|
+
ReturnValueDefinition.new(data)
|
98
|
+
rescue
|
99
|
+
raise AMEE::BadData.new("Couldn't load ReturnValueDefinition from JSON. Check that your URL is correct.\n#{json}")
|
100
|
+
end
|
101
|
+
|
102
|
+
def self.xmlpathpreamble
|
103
|
+
"/Representation/ReturnValueDefinition/"
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.from_xml(xml)
|
107
|
+
|
108
|
+
|
109
|
+
# Parse data from response into hash
|
110
|
+
doc = load_xml_doc(xml)
|
111
|
+
data = {}
|
112
|
+
data[:uid] = x "@uid",:doc=>doc
|
113
|
+
data[:itemdefuid] = x "ItemDefinition/@uid",:doc=>doc
|
114
|
+
data[:type] = x "Type",:doc=>doc
|
115
|
+
data[:unit] = x "Unit",:doc=>doc
|
116
|
+
data[:perunit] = x "PerUnit",:doc=>doc
|
117
|
+
data[:valuetype] = x "ValueDefinition/ValueType",:doc=>doc
|
118
|
+
data[:default] = x "ReturnValueDefinition/Default",:doc=>doc
|
119
|
+
|
120
|
+
|
121
|
+
# Create object
|
122
|
+
ReturnValueDefinition.new(data)
|
123
|
+
rescue => err
|
124
|
+
raise AMEE::BadData.new("Couldn't load ReturnValueDefinition from XML. Check that your URL is correct.\n#{xml}\n#{err}")
|
125
|
+
end
|
126
|
+
|
127
|
+
def self.get(connection, path, options = {})
|
128
|
+
# Load data from path
|
129
|
+
response = connection.v3_get(path, options)
|
130
|
+
# Parse response
|
131
|
+
item_definition = ReturnValueDefinition.parse(connection, response)
|
132
|
+
# Done
|
133
|
+
return item_definition
|
134
|
+
rescue => err
|
135
|
+
raise AMEE::BadData.new("Couldn't load ReturnValueDefinition. Check that your URL is correct.\n#{response}\n#{err}")
|
136
|
+
end
|
137
|
+
|
138
|
+
|
139
|
+
|
140
|
+
def self.load(connection,itemdefuid,ivduid,options={})
|
141
|
+
ReturnValueDefinition.get(connection,"/#{AMEE_API_VERSION}/definitions/#{itemdefuid}/returnvalues/#{ivduid};full",options)
|
142
|
+
end
|
143
|
+
|
144
|
+
def reload(connection)
|
145
|
+
ReturnValueDefinition.load(connection,itemdefuid,uid)
|
146
|
+
end
|
147
|
+
|
148
|
+
def self.update(connection, path, options = {})
|
149
|
+
|
150
|
+
# Do we want to automatically fetch the item afterwards?
|
151
|
+
get_item = options.delete(:get_item)
|
152
|
+
get_item = true if get_item.nil?
|
153
|
+
# Go
|
154
|
+
response = connection.v3_put(path, options)
|
155
|
+
if get_item
|
156
|
+
if response.empty?
|
157
|
+
return ReturnValueDefinition.get(connection, path)
|
158
|
+
else
|
159
|
+
return ReturnValueDefinition.parse(connection, response)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
rescue
|
163
|
+
raise AMEE::BadData.new("Couldn't update ReturnValueDefinition. Check that your information is correct.\n#{response}")
|
164
|
+
end
|
165
|
+
|
166
|
+
def self.create(connection,itemdefuid, options = {})
|
167
|
+
|
168
|
+
unless options.is_a?(Hash)
|
169
|
+
raise AMEE::ArgumentError.new("Second argument must be a hash of options!")
|
170
|
+
end
|
171
|
+
# Send data
|
172
|
+
vt=options.delete(:valuetype)
|
173
|
+
case vt
|
174
|
+
when 'text'
|
175
|
+
options.merge!( :valueDefinition=>"CCEB59CACE1B")
|
176
|
+
else
|
177
|
+
options.merge!( :valueDefinition=>"45433E48B39F")
|
178
|
+
end
|
179
|
+
|
180
|
+
options.merge!(:returnobj=>true)
|
181
|
+
response = connection.v3_post("/#{AMEE_API_VERSION}/definitions/#{itemdefuid}/returnvalues", options)
|
182
|
+
return ReturnValueDefinition.load(connection,itemdefuid , response['Location'].split('/')[7])
|
183
|
+
rescue
|
184
|
+
raise AMEE::BadData.new("Couldn't create ReturnValueDefinition. Check that your information is correct.\n#{response}")
|
185
|
+
end
|
186
|
+
|
187
|
+
def self.delete(connection, itemdefuid,return_value_definition)
|
188
|
+
|
189
|
+
# Deleting takes a while... up the timeout to 120 seconds temporarily
|
190
|
+
t = connection.timeout
|
191
|
+
connection.timeout = 120
|
192
|
+
connection.v3_delete("/#{AMEE_API_VERSION}/definitions/#{itemdefuid}/returnvalues/" + return_value_definition.uid)
|
193
|
+
connection.timeout = t
|
194
|
+
rescue
|
195
|
+
raise AMEE::BadData.new("Couldn't delete ReturnValueDefinition. Check that your information is correct.")
|
196
|
+
end
|
197
|
+
|
198
|
+
end
|
199
|
+
|
200
|
+
end
|
201
|
+
|
202
|
+
end
|
data/lib/amee/v3.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# Copyright (C) 2008-2011 AMEE UK Ltd. - http://www.amee.com
|
2
|
+
# Released as Open Source Software under the BSD 3-Clause license. See LICENSE.txt for details.
|
3
|
+
|
4
|
+
AMEE_API_VERSION = '3.3'
|
5
|
+
|
6
|
+
require 'amee/v3/meta_helper'
|
7
|
+
require 'amee/v3/collection'
|
8
|
+
require 'amee/v3/connection'
|
9
|
+
require 'amee/v3/item_definition'
|
10
|
+
require 'amee/v3/item_value_definition'
|
11
|
+
require 'amee/v3/item_value_definition_list'
|
12
|
+
require 'amee/v3/return_value_definition'
|
data/lib/amee.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
# Copyright (C) 2008-2011 AMEE UK Ltd. - http://www.amee.com
|
2
|
+
# Released as Open Source Software under the BSD 3-Clause license. See LICENSE.txt for details.
|
3
|
+
|
1
4
|
require 'rexml/document'
|
2
5
|
require 'nokogiri'
|
3
6
|
require 'active_support'
|
@@ -49,6 +52,7 @@ require 'amee/pager'
|
|
49
52
|
require 'amee/item_definition'
|
50
53
|
require 'amee/item_value_definition'
|
51
54
|
require 'amee/user'
|
55
|
+
require 'amee/v3'
|
52
56
|
|
53
57
|
class Date
|
54
58
|
def amee1_date
|
data/rails/init.rb
CHANGED
data/spec/amee_spec.rb
CHANGED
data/spec/cache_spec.rb
CHANGED
data/spec/connection_spec.rb
CHANGED