Floppy-amee 0.3.0 → 0.3.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/README CHANGED
@@ -35,11 +35,11 @@ this interface yet.
35
35
 
36
36
  == RAILS
37
37
 
38
- This gem can be used as a Rails plugin. You can either extract it into
38
+ This gem can also be used as a Rails plugin. You can either extract it into
39
39
  vendor/plugins, or use the new-style config.gem command in environment.rb. For
40
40
  example:
41
41
 
42
- config.gem "Floppy-amee", :lib => "amee", :source => "http://gems.github.com"
42
+ config.gem "Floppy-amee", :lib => "amee", :source => "http://gems.github.com", :version => '>= 0.3.0'
43
43
 
44
44
  The first time you run your app after installing the gem, a config/amee.yml file
45
45
  will be created. You should edit this file and add your AMEE username and
data/bin/ameesh CHANGED
File without changes
@@ -9,19 +9,14 @@ module AMEE
9
9
  @password = password
10
10
  @auth_token = nil
11
11
  @use_json_if_available = use_json_if_available
12
- raise "Must specify both username and password for authenticated access" if (@username || @password) && !valid?
12
+ if (@username || @password) && !valid?
13
+ raise "Must specify both username and password for authenticated access"
14
+ end
13
15
  # Make connection to server
14
16
  @http = Net::HTTP.new(@server)
15
17
  #@http.set_debug_output($stdout)
16
- @http.start
17
- rescue SocketError
18
- raise AMEE::ConnectionFailed.new("Connection failed. Check server name or network connection.")
19
18
  end
20
19
 
21
- def finalize
22
- @http.finish
23
- end
24
-
25
20
  def valid?
26
21
  !((@username || @password) ? (@username.nil? || @password.nil? || @server.nil?) : @server.nil?)
27
22
  end
@@ -35,46 +30,32 @@ module AMEE
35
30
  end
36
31
 
37
32
  def get(path)
38
- response = nil
39
- get = Net::HTTP::Get.new(path)
40
- get['authToken'] = @auth_token
41
- get['Accept'] = content_type
42
- response = @http.request(get)
43
- # Handle 404s
44
- raise AMEE::NotFound.new("URL doesn't exist on server.") if response.code == '404'
45
- # If request fails, authenticate and try again
46
- if authentication_failed?(response)
47
- authenticate
48
- get['authToken'] = @auth_token
49
- response = @http.request(get)
50
- end
51
- yield response.body if block_given?
52
- response.body
33
+ # Send request
34
+ do_request(Net::HTTP::Get.new(path))
53
35
  end
54
36
 
55
37
  def post(path, data = {})
56
- response = nil
38
+ # Create POST request
57
39
  post = Net::HTTP::Post.new(path)
58
- post['authToken'] = @auth_token
59
- post['Accept'] = content_type
60
- # Add data params to body
61
40
  body = []
62
41
  data.each_pair do |key, value|
63
42
  body << "#{key}=#{value}"
64
43
  end
65
44
  post.body = body.join '&'
66
45
  # Send request
67
- response = @http.request(post)
68
- # Handle 404s
69
- raise AMEE::NotFound.new("URL doesn't exist on server.") if response.code == '404'
70
- # If request fails, authenticate and try again
71
- if authentication_failed?(response)
72
- authenticate
73
- post['authToken'] = @auth_token
74
- response = @http.request(post)
46
+ do_request(post)
47
+ end
48
+
49
+ def put(path, data = {})
50
+ # Create PUT request
51
+ put = Net::HTTP::Put.new(path)
52
+ body = []
53
+ data.each_pair do |key, value|
54
+ body << "#{key}=#{value}"
75
55
  end
76
- yield response.body if block_given?
77
- response.body
56
+ put.body = body.join '&'
57
+ # Send request
58
+ do_request(put)
78
59
  end
79
60
 
80
61
  def authenticate
@@ -87,7 +68,9 @@ module AMEE
87
68
  post['Accept'] = content_type
88
69
  response = @http.request(post)
89
70
  @auth_token = response['authToken']
90
- raise AMEE::AuthFailed.new("Authentication failed. Please check your username and password.") unless authenticated?
71
+ unless authenticated?
72
+ raise AMEE::AuthFailed.new("Authentication failed. Please check your username and password.")
73
+ end
91
74
  end
92
75
 
93
76
  protected
@@ -96,9 +79,47 @@ module AMEE
96
79
  (@use_json_if_available && defined?(JSON)) ? 'application/json' : 'application/xml'
97
80
  end
98
81
 
99
- def authentication_failed?(response)
100
- response.code == '401'
82
+ def response_ok?(response)
83
+ case response.code
84
+ when '200'
85
+ return true
86
+ when '403'
87
+ raise AMEE::PermissionDenied.new("You do not have permission to perform the requested operation")
88
+ when '401'
89
+ authenticate
90
+ return false
91
+ else
92
+ raise AMEE::UnknownError.new("An error occurred while talking to AMEE: HTTP response code #{response.code}")
93
+ end
94
+ end
95
+
96
+ def do_request(request)
97
+ # Open HTTP connection
98
+ @http.start
99
+ # Do request
100
+ begin
101
+ response = send_request(request)
102
+ end while !response_ok?(response)
103
+ # Return body of response
104
+ return response.body
105
+ rescue SocketError
106
+ raise AMEE::ConnectionFailed.new("Connection failed. Check server name or network connection.")
107
+ ensure
108
+ # Close HTTP connection
109
+ @http.finish if @http.started?
101
110
  end
102
111
 
112
+ def send_request(request)
113
+ request['authToken'] = @auth_token
114
+ request['Accept'] = content_type
115
+ response = @http.request(request)
116
+ # Handle 404s
117
+ if response.code == '404'
118
+ raise AMEE::NotFound.new("URL doesn't exist on server.")
119
+ end
120
+ # Done
121
+ response
122
+ end
123
+
103
124
  end
104
125
  end
@@ -95,10 +95,10 @@ module AMEE
95
95
  item.connection = connection
96
96
  # Done
97
97
  return item
98
- rescue
98
+ rescue
99
99
  raise AMEE::BadData.new("Couldn't load DataItem. Check that your URL is correct.")
100
100
  end
101
101
 
102
102
  end
103
103
  end
104
- end
104
+ end
@@ -21,6 +21,10 @@ module AMEE
21
21
  end
22
22
  end
23
23
 
24
+ def value=(val)
25
+ @value = val
26
+ end
27
+
24
28
  def from_profile?
25
29
  @from_profile
26
30
  end
@@ -79,10 +83,14 @@ module AMEE
79
83
  value.connection = connection
80
84
  # Done
81
85
  return value
82
- rescue
86
+ rescue
83
87
  raise AMEE::BadData.new("Couldn't load DataItemValue. Check that your URL is correct.")
84
88
  end
85
89
 
90
+ def save!
91
+ response = @connection.put(full_path, :value => value)
92
+ end
93
+
86
94
  end
87
95
  end
88
96
  end
@@ -9,10 +9,16 @@ module AMEE
9
9
  class AuthRequired < Exception
10
10
  end
11
11
 
12
+ class PermissionDenied < Exception
13
+ end
14
+
12
15
  class ConnectionFailed < Exception
13
16
  end
14
17
 
15
18
  class NotFound < Exception
16
19
  end
17
20
 
21
+ class UnknownError < Exception
22
+ end
23
+
18
24
  end
data/lib/amee/profile.rb CHANGED
@@ -41,6 +41,8 @@ module AMEE
41
41
  end
42
42
  # Done
43
43
  return profiles
44
+ rescue
45
+ raise AMEE::BadData.new("Couldn't load Profile list.")
44
46
  end
45
47
 
46
48
  def self.create(connection)
@@ -78,12 +80,14 @@ module AMEE
78
80
  # Done
79
81
  return profile
80
82
  end
83
+ rescue
84
+ raise AMEE::BadData.new("Couldn't create Profile.")
81
85
  end
82
86
 
83
- def delete
84
- # Create new profile
85
- response = connection.delete(full_path)
86
- end
87
+ # def delete
88
+ # # Create new profile
89
+ # response = connection.delete(full_path)
90
+ # end
87
91
 
88
92
  end
89
- end
93
+ end
data/lib/amee/shell.rb CHANGED
@@ -5,11 +5,18 @@ module AMEE
5
5
  puts "AMEE shell - version #{AMEE::VERSION::STRING}"
6
6
  puts "--------------------------"
7
7
  puts "Commands:"
8
- puts " ls - display contents of current category."
9
- puts " cd 'path' - change category. Path must be a quoted string. You can use things like '/data', '..', or 'subcategory'."
10
- puts " pwd - display current category path."
11
- puts " cat 'name' - display contents of data item called 'name' within the current category."
12
- puts " amee_help - display this help text."
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."
13
20
  end
14
21
 
15
22
  def ls
@@ -51,10 +58,21 @@ module AMEE
51
58
  end
52
59
  nil
53
60
  end
54
-
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
+
55
70
  end
56
71
  end
57
72
 
73
+ dir = File.dirname(__FILE__) + "/.."
74
+ $LOAD_PATH << dir unless $LOAD_PATH.include?(dir)
75
+
58
76
  require 'rubygems'
59
77
  require 'amee'
60
78
  include AMEE::Shell
data/lib/amee/version.rb CHANGED
@@ -2,7 +2,7 @@ module AMEE
2
2
 
3
3
  module VERSION #:nodoc:
4
4
  MAJOR = 0
5
- MINOR = 2
5
+ MINOR = 3
6
6
  TINY = 1
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  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.0
4
+ version: 0.3.1
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-22 00:00:00 -07:00
12
+ date: 2008-08-29 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15