Floppy-amee 0.1.2 → 0.1.5

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/README CHANGED
@@ -22,9 +22,16 @@ 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. See examples/list_profiles.rb for details.
24
24
 
25
+ The gem will use the AMEE JSON API if the JSON gem is installed on the local
26
+ system. Otherwise the XML API will be used. Currently, only DataCategories and
27
+ Profile lists are supported with JSON, so you can disable JSON support by setting
28
+ the fourth argument to AMEE::Connection.new() to false:
29
+
30
+ > AMEE::Connection.new($srv, $user, $pass, false) # last arg disables JSON support
31
+
25
32
  == INTERACTIVE SHELL
26
33
 
27
34
  You can use the 'ameesh' app to interactively explore the AMEE data area. Run
28
35
  'ameesh -u USERNAME -p PASSWORD -s SERVER' to try it out. Source code for this
29
36
  tool is in bin/ameesh and lib/amee/shell.rb. Profiles are not accessible through
30
- this interface yet.
37
+ this interface yet.
@@ -17,7 +17,7 @@ OptionParser.new do |opts|
17
17
  end.parse!
18
18
 
19
19
  # Connect
20
- connection = AMEE::Connection.new(options[:server], options[:username], options[:password])
20
+ connection = AMEE::Connection.new(options[:server], options[:username], options[:password], false) # Disable JSON support for now
21
21
 
22
22
  # For each path in arg list, show details
23
23
  ARGV.each do |path|
@@ -2,12 +2,13 @@ require 'net/http'
2
2
 
3
3
  module AMEE
4
4
  class Connection
5
-
6
- def initialize(server, username = nil, password = nil)
5
+
6
+ def initialize(server, username = nil, password = nil, use_json_if_available = true)
7
7
  @server = server
8
8
  @username = username
9
9
  @password = password
10
10
  @auth_token = nil
11
+ @use_json_if_available = use_json_if_available
11
12
  raise "Must specify both username and password for authenticated access" if (@username || @password) && !valid?
12
13
  # Make connection to server
13
14
  @http = Net::HTTP.new(@server)
@@ -37,7 +38,7 @@ module AMEE
37
38
  response = nil
38
39
  get = Net::HTTP::Get.new(path)
39
40
  get['authToken'] = @auth_token
40
- get['Accept'] = 'application/xml'
41
+ get['Accept'] = content_type
41
42
  response = @http.request(get)
42
43
  # Handle 404s
43
44
  raise AMEE::NotFound.new("URL doesn't exist on server.") if response.code == '404'
@@ -53,6 +54,10 @@ module AMEE
53
54
 
54
55
  protected
55
56
 
57
+ def content_type
58
+ (@use_json_if_available && defined?(JSON)) ? 'application/json' : 'application/xml'
59
+ end
60
+
56
61
  def authentication_failed?(response)
57
62
  response.code == '401'
58
63
  end
@@ -64,7 +69,7 @@ module AMEE
64
69
  response = nil
65
70
  post = Net::HTTP::Post.new("/auth")
66
71
  post.body = "username=#{@username}&password=#{@password}"
67
- post['Accept'] = 'application/xml'
72
+ post['Accept'] = content_type
68
73
  response = @http.request(post)
69
74
  @auth_token = response['authToken']
70
75
  raise AMEE::AuthFailed.new("Authentication failed. Please check your username and password.") unless authenticated?
@@ -16,29 +16,60 @@ module AMEE
16
16
  def self.get(connection, path)
17
17
  # Load data from path
18
18
  response = connection.get(path)
19
- # Parse data from response into hash
20
- doc = REXML::Document.new(response)
21
- data = {}
22
- data[:uid] = REXML::XPath.first(doc, "/Resources/DataCategoryResource/DataCategory/@uid").to_s
23
- data[:created] = DateTime.parse(REXML::XPath.first(doc, "/Resources/DataCategoryResource/DataCategory/@created").to_s)
24
- data[:modified] = DateTime.parse(REXML::XPath.first(doc, "/Resources/DataCategoryResource/DataCategory/@modified").to_s)
25
- data[:name] = REXML::XPath.first(doc, '/Resources/DataCategoryResource/DataCategory/Name').text
26
- data[:path] = REXML::XPath.first(doc, '/Resources/DataCategoryResource/Path').text || ""
27
- data[:children] = []
28
- REXML::XPath.each(doc, '/Resources/DataCategoryResource/Children/DataCategories/DataCategory') do |child|
29
- category_data = {}
30
- category_data[:name] = child.elements['Name'].text
31
- category_data[:path] = child.elements['Path'].text
32
- category_data[:uid] = child.attributes['uid'].to_s
33
- data[:children] << category_data
34
- end
35
- data[:items] = []
36
- REXML::XPath.each(doc, '/Resources/DataCategoryResource/Children/DataItems/DataItem') do |item|
37
- item_data = {}
38
- item_data[:label] = item.elements['label'].text
39
- item_data[:path] = item.elements['path'].text
40
- item_data[:uid] = item.attributes['uid'].to_s
41
- data[:items] << item_data
19
+ # Parse data from response
20
+ profiles = []
21
+ if response.slice(0,1) == '{'
22
+ # Read JSON
23
+ doc = JSON.parse(response)
24
+ data = {}
25
+ data[:uid] = doc['dataCategory']['uid']
26
+ data[:created] = DateTime.parse(doc['dataCategory']['created'])
27
+ data[:modified] = DateTime.parse(doc['dataCategory']['modified'])
28
+ data[:name] = doc['dataCategory']['name']
29
+ data[:path] = doc['path']
30
+ data[:children] = []
31
+ doc['children']['dataCategories'].each do |child|
32
+ category_data = {}
33
+ category_data[:name] = child['name']
34
+ category_data[:path] = child['path']
35
+ category_data[:uid] = child['uid']
36
+ data[:children] << category_data
37
+ end
38
+ data[:items] = []
39
+ if doc['children']['dataItems']['rows']
40
+ doc['children']['dataItems']['rows'].each do |item|
41
+ item_data = {}
42
+ item_data[:label] = item['label']
43
+ item_data[:path] = item['path']
44
+ item_data[:uid] = item['uid']
45
+ data[:items] << item_data
46
+ end
47
+ end
48
+ else
49
+ # Read XML
50
+ doc = REXML::Document.new(response)
51
+ data = {}
52
+ data[:uid] = REXML::XPath.first(doc, "/Resources/DataCategoryResource/DataCategory/@uid").to_s
53
+ data[:created] = DateTime.parse(REXML::XPath.first(doc, "/Resources/DataCategoryResource/DataCategory/@created").to_s)
54
+ data[:modified] = DateTime.parse(REXML::XPath.first(doc, "/Resources/DataCategoryResource/DataCategory/@modified").to_s)
55
+ data[:name] = REXML::XPath.first(doc, '/Resources/DataCategoryResource/DataCategory/Name').text
56
+ data[:path] = REXML::XPath.first(doc, '/Resources/DataCategoryResource/Path').text || ""
57
+ data[:children] = []
58
+ REXML::XPath.each(doc, '/Resources/DataCategoryResource/Children/DataCategories/DataCategory') do |child|
59
+ category_data = {}
60
+ category_data[:name] = child.elements['Name'].text
61
+ category_data[:path] = child.elements['Path'].text
62
+ category_data[:uid] = child.attributes['uid'].to_s
63
+ data[:children] << category_data
64
+ end
65
+ data[:items] = []
66
+ REXML::XPath.each(doc, '/Resources/DataCategoryResource/Children/DataItems/DataItem') do |item|
67
+ item_data = {}
68
+ item_data[:label] = item.elements['label'].text
69
+ item_data[:path] = item.elements['path'].text
70
+ item_data[:uid] = item.attributes['uid'].to_s
71
+ data[:items] << item_data
72
+ end
42
73
  end
43
74
  # Create category object
44
75
  cat = Category.new(data)
@@ -4,11 +4,13 @@ module AMEE
4
4
 
5
5
  def initialize(data = {})
6
6
  @values = data ? data[:values] : []
7
+ @choices = data ? data[:choices] : []
7
8
  @label = data ? data[:label] : []
8
9
  super
9
10
  end
10
11
 
11
12
  attr_reader :values
13
+ attr_reader :choices
12
14
  attr_reader :label
13
15
 
14
16
  def self.get(connection, path)
@@ -33,6 +35,14 @@ module AMEE
33
35
  value_data[:uid] = value.attributes['uid'].to_s
34
36
  data[:values] << value_data
35
37
  end
38
+ # Get choices
39
+ data[:choices] = []
40
+ REXML::XPath.each(doc, '/Resources/DataItemResource/Choices/Choices/Choice') do |choice|
41
+ choice_data = {}
42
+ choice_data[:name] = choice.elements['Name'].text
43
+ choice_data[:value] = choice.elements['Value'].text
44
+ data[:choices] << choice_data
45
+ end
36
46
  # Create item object
37
47
  item = Item.new(data)
38
48
  # Store connection in object for future use
data/lib/amee/profile.rb CHANGED
@@ -3,27 +3,47 @@ module AMEE
3
3
 
4
4
  def self.list(connection)
5
5
  # Load data from path
6
- response = connection.get('/profiles')
7
- # Parse data from response into hash
8
- doc = REXML::Document.new(response)
6
+ response = connection.get('/profiles')
7
+ # Parse data from response
9
8
  profiles = []
10
- REXML::XPath.each(doc, '/Resources/ProfilesResource/Profiles/Profile') do |p|
11
- data = {}
12
- data[:uid] = p.attributes['uid'].to_s
13
- data[:created] = DateTime.parse(p.attributes['created'].to_s)
14
- data[:modified] = DateTime.parse(p.attributes['modified'].to_s)
15
- data[:name] = p.elements['Name'].text || data[:uid]
16
- data[:path] = p.elements['Path'].text || data[:uid]
17
- # Create profile
18
- profile = AMEE::Profile.new(data)
19
- # Store connection in object for future use
20
- profile.connection = connection
21
- # Store in array
22
- profiles << profile
9
+ if response.slice(0,1) == '{'
10
+ # Read JSON
11
+ doc = JSON.parse(response)
12
+ doc['profiles'].each do |p|
13
+ data = {}
14
+ data[:uid] = p['uid']
15
+ data[:created] = DateTime.parse(p['created'])
16
+ data[:modified] = DateTime.parse(p['modified'])
17
+ data[:name] = p['name']
18
+ data[:path] = p['path']
19
+ # Create profile
20
+ profile = AMEE::Profile.new(data)
21
+ # Store connection in object for future use
22
+ profile.connection = connection
23
+ # Store in array
24
+ profiles << profile
25
+ end
26
+ else
27
+ # Read XML
28
+ doc = REXML::Document.new(response)
29
+ REXML::XPath.each(doc, '/Resources/ProfilesResource/Profiles/Profile') do |p|
30
+ data = {}
31
+ data[:uid] = p.attributes['uid'].to_s
32
+ data[:created] = DateTime.parse(p.attributes['created'].to_s)
33
+ data[:modified] = DateTime.parse(p.attributes['modified'].to_s)
34
+ data[:name] = p.elements['Name'].text || data[:uid]
35
+ data[:path] = p.elements['Path'].text || data[:uid]
36
+ # Create profile
37
+ profile = AMEE::Profile.new(data)
38
+ # Store connection in object for future use
39
+ profile.connection = connection
40
+ # Store in array
41
+ profiles << profile
42
+ end
23
43
  end
24
44
  # Done
25
45
  return profiles
26
46
  end
27
-
47
+
28
48
  end
29
49
  end
data/lib/amee/shell.rb CHANGED
@@ -60,7 +60,7 @@ require 'amee'
60
60
  include AMEE::Shell
61
61
 
62
62
  # Set up connection
63
- $connection = AMEE::Connection.new(ENV['AMEE_SERVER'], ENV['AMEE_USERNAME'], ENV['AMEE_PASSWORD'])
63
+ $connection = AMEE::Connection.new(ENV['AMEE_SERVER'], ENV['AMEE_USERNAME'], ENV['AMEE_PASSWORD'], false) # Disable JSON support for now
64
64
 
65
65
  if $connection.valid?
66
66
  # Change to root of data api to get going
@@ -0,0 +1,10 @@
1
+ module AMEE
2
+
3
+ module VERSION #:nodoc:
4
+ MAJOR = 0
5
+ MINOR = 1
6
+ TINY = 5
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+
10
+ end
data/lib/amee.rb CHANGED
@@ -1,4 +1,13 @@
1
1
  require 'rexml/document'
2
+
3
+ # We don't NEED the JSON gem, but if it's available, use it.
4
+ begin
5
+ require 'json'
6
+ rescue LoadError
7
+ nil
8
+ end
9
+
10
+ require 'amee/version'
2
11
  require 'amee/exceptions'
3
12
  require 'amee/connection'
4
13
  require 'amee/object'
@@ -6,14 +15,3 @@ require 'amee/data_category'
6
15
  require 'amee/data_item'
7
16
  require 'amee/data_item_value'
8
17
  require 'amee/profile'
9
-
10
- module AMEE
11
-
12
- module VERSION #:nodoc:
13
- MAJOR = 0
14
- MINOR = 1
15
- TINY = 2
16
- STRING = [MAJOR, MINOR, TINY].join('.')
17
- end
18
-
19
- 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.1.2
4
+ version: 0.1.5
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-07-24 00:00:00 -07:00
12
+ date: 2008-07-29 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -24,16 +24,17 @@ extra_rdoc_files: []
24
24
  files:
25
25
  - README
26
26
  - COPYING
27
- - bin/ameesh
28
27
  - lib/amee.rb
29
28
  - lib/amee/connection.rb
30
- - lib/amee/data_category.rb
31
29
  - lib/amee/data_item.rb
32
- - lib/amee/data_item_value.rb
33
30
  - lib/amee/exceptions.rb
34
- - lib/amee/object.rb
35
31
  - lib/amee/profile.rb
32
+ - lib/amee/version.rb
33
+ - lib/amee/data_category.rb
34
+ - lib/amee/data_item_value.rb
35
+ - lib/amee/object.rb
36
36
  - lib/amee/shell.rb
37
+ - bin/ameesh
37
38
  - examples/list_profiles.rb
38
39
  - examples/view_data_category.rb
39
40
  - examples/view_data_item.rb