simple_stack 0.0.6 → 0.0.7

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.
@@ -6,7 +6,11 @@ module SimpleStack
6
6
  end
7
7
 
8
8
  def cacheable?
9
- false
9
+ if respond_to? :connection
10
+ connection.cache_enabled
11
+ else
12
+ false
13
+ end
10
14
  end
11
15
 
12
16
  def cached_attributes
@@ -1,9 +1,11 @@
1
1
  module SimpleStack
2
2
  class Connection
3
- attr_accessor :url
3
+ attr_accessor :url, :graceful_degradation, :cache_enabled
4
4
 
5
5
  def initialize(options = {})
6
6
  self.url = options[:url].to_s.sub(/\/$/, "")
7
+ self.graceful_degradation = options[:graceful_degradation]
8
+ self.cache_enabled = options[:cache_enabled]
7
9
  end
8
10
 
9
11
  def connect_to(type, options)
@@ -0,0 +1,87 @@
1
+ module SimpleStack
2
+ class Exception < ::Exception
3
+ def self.factory(error)
4
+ return Exception.new(error) if !error.is_a?(Hash)
5
+ case error["error"]
6
+ when "EntityNotFound"
7
+ EntityNotFound.new(error["message"])
8
+ when "FeatureNotImplemented"
9
+ NotImplemented.new(error["message"])
10
+ when "FeatureNotAvailable"
11
+ NotImplemented.new(error["message"])
12
+ when "HypervisorError"
13
+ HypervisorError.new(error["message"])
14
+ else
15
+ Exception.new(error["message"])
16
+ end
17
+ end
18
+ end
19
+
20
+ class EntityNotFound < Exception
21
+ end
22
+
23
+ class HypervisorError < Exception
24
+ end
25
+
26
+ class NotImplemented < Exception
27
+ end
28
+
29
+ class GracefulObject
30
+ def [](key)
31
+ GracefulObject.new
32
+ end
33
+
34
+ def []=(key, value)
35
+ GracefulObject.new
36
+ end
37
+
38
+ def +(other)
39
+ other + self
40
+ end
41
+
42
+ def -(other)
43
+ -(other - self)
44
+ end
45
+
46
+ def *(other)
47
+ other * self
48
+ end
49
+
50
+ def to_i
51
+ 0
52
+ end
53
+
54
+ def to_f
55
+ 0.0
56
+ end
57
+
58
+ def to_str
59
+ "GracefulObject"
60
+ end
61
+ alias :to_s :to_str
62
+
63
+ def to_ary
64
+ []
65
+ end
66
+ alias :to_a :to_ary
67
+
68
+ def to_hash
69
+ {}
70
+ end
71
+
72
+ def coerce(other)
73
+ coerced = if other.is_a? Fixnum
74
+ self.to_i
75
+ elsif other.is_a? String
76
+ self.to_s
77
+ elsif other.is_a? Array
78
+ self.to_a
79
+ elsif other.is_a? Hash
80
+ self.to_hash
81
+ else
82
+ nil
83
+ end
84
+ [coerced, other]
85
+ end
86
+ end
87
+ end
@@ -9,11 +9,15 @@ module SimpleStack
9
9
  end
10
10
 
11
11
  def add_tag(tag)
12
+ cached_attributes[:tags] ||= []
12
13
  hypervisor.post "#{url}/tags", :name => tag
14
+ cached_attributes[:tags] << tag
13
15
  end
14
16
 
15
17
  def remove_tag(tag)
18
+ cached_attributes[:tags] ||= []
16
19
  hypervisor.delete "#{url}/tags/#{tag}"
20
+ cached_attributes[:tags].delete tag
17
21
  end
18
22
 
19
23
  def reboot(opts={:force => false})
@@ -38,19 +38,26 @@ module SimpleStack
38
38
  end
39
39
 
40
40
  def get(url)
41
- HTTParty.get(url, :headers => headers, :no_follow => true)
41
+ http_call { HTTParty.get(url, :headers => headers, :no_follow => true) }
42
42
  end
43
43
 
44
44
  def post(url, body)
45
- HTTParty.post(url, :body => JSON.dump(body), :headers => headers, :no_follow => true)
45
+ http_call { HTTParty.post(url, :body => JSON.dump(body), :headers => headers, :no_follow => true) }
46
46
  end
47
47
 
48
48
  def put(url, body)
49
- HTTParty.put(url, :body => JSON.dump(body), :headers => headers, :no_follow => true)
49
+ http_call { HTTParty.put(url, :body => JSON.dump(body), :headers => headers, :no_follow => true) }
50
50
  end
51
51
 
52
52
  def delete(url)
53
- HTTParty.delete(url, :headers => headers, :no_follow => true)
53
+ http_call { HTTParty.delete(url, :headers => headers, :no_follow => true) }
54
+ end
55
+
56
+ def http_call
57
+ response = yield
58
+ return SimpleStack::GracefulObject.new if response.code == 501 && connection.graceful_degradation
59
+ raise SimpleStack::Exception.factory(response.parsed_response) if response.code >= 400
60
+ response
54
61
  end
55
62
 
56
63
  def inspect
@@ -1,3 +1,3 @@
1
1
  module SimpleStack
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
data/lib/simple_stack.rb CHANGED
@@ -2,6 +2,7 @@ require "json"
2
2
  require "base64"
3
3
  require "httparty"
4
4
  require "simple_stack/version"
5
+ require "simple_stack/exception"
5
6
  require "simple_stack/connection"
6
7
  require "simple_stack/cacheable"
7
8
  require "simple_stack/collection"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_stack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-06-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &2157330680 !ruby/object:Gem::Requirement
16
+ requirement: &2156945680 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2157330680
24
+ version_requirements: *2156945680
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: httparty
27
- requirement: &2157330080 !ruby/object:Gem::Requirement
27
+ requirement: &2156945240 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2157330080
35
+ version_requirements: *2156945240
36
36
  description: A Simple Stack gem
37
37
  email:
38
38
  - thiago.morello@locaweb.com.br
@@ -49,6 +49,7 @@ files:
49
49
  - lib/simple_stack/collection.rb
50
50
  - lib/simple_stack/connection.rb
51
51
  - lib/simple_stack/entity.rb
52
+ - lib/simple_stack/exception.rb
52
53
  - lib/simple_stack/guest.rb
53
54
  - lib/simple_stack/hypervisor.rb
54
55
  - lib/simple_stack/snapshot.rb