amfetamine 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,6 +24,7 @@ module Amfetamine
24
24
 
25
25
 
26
26
  attr_reader :attributes
27
+ attr_accessor :cache_key
27
28
 
28
29
  def id=(val)
29
30
  @attributes['id'] = val
@@ -98,6 +99,7 @@ module Amfetamine
98
99
  def initialize(args={})
99
100
  super
100
101
  @attributes = {}
102
+ self.cache_key = self.class.recent_cache_key # Shows how this object was retrieved from cache
101
103
  args.each { |k,v| self.send("#{k}=", v) }
102
104
  @notsaved = true
103
105
  self
@@ -129,11 +131,9 @@ module Amfetamine
129
131
  persisted? ? id.to_s : nil
130
132
  end
131
133
 
132
- # Checks if object is cached
133
- # TODO this is not very efficient, but dalli doesn't provide a polling function :(
134
+ # Checks if object is cached by checking if a SINGULAR request was made to this object.
134
135
  def cached?
135
- keys = belongs_to_relationships.collect { |r| r.singular_path } << self.singular_path
136
- keys.any? { |k| cache.get(k) }
136
+ self.cache_key ? cache.get(self.cache_key).present? : false
137
137
  end
138
138
 
139
139
  # Checks if object is cachable
@@ -4,6 +4,7 @@ module Amfetamine
4
4
  class UnknownRESTMethod < Exception; end; # Only used if rest method is not handled by amfetamine
5
5
  class RecordNotFound < Exception; end;
6
6
  class InvalidCacheData < Exception; end;
7
+ class RemoteConnectionError < Exception; end; # Raised when response handling fails
7
8
  #class MatcherNotImplemented < Exception; end;
8
9
  class ExternalConnectionsNotAllowed < Exception; end; # Used for test helpers if connection is not allowed and attempt is made to connect
9
10
  end
@@ -10,12 +10,16 @@ module Amfetamine
10
10
  end
11
11
 
12
12
  module ClassMethods
13
+ def recent_cache_key
14
+ @recent_cache_key
15
+ end
16
+
13
17
  def find(id, opts={})
14
18
  begin
15
19
  key = opts[:nested_path] || self.find_path(id)
16
20
  data = get_data(key, opts[:conditions])
17
21
  if data[:status] == :success
18
- build_object(data[:body])
22
+ val = build_object(data[:body])
19
23
  else
20
24
  nil
21
25
  end
@@ -31,7 +35,7 @@ module Amfetamine
31
35
  data = get_data(key, opts[:conditions])
32
36
 
33
37
  if data[:status] == :success
34
- data[:body].compact.map { |d| build_object(d) }
38
+ vals = data[:body].compact.map { |d| build_object(d) }
35
39
  else
36
40
  []
37
41
  end
@@ -61,15 +65,15 @@ module Amfetamine
61
65
  def get_data(key, conditions=nil, method=:get)
62
66
  if cacheable?
63
67
  if conditions
64
- cache_key = key + conditions.to_query
68
+ @recent_cache_key = key + conditions.to_query
65
69
  cache_conditions(key, conditions)
66
70
  else
67
- cache_key = key
71
+ @recent_cache_key = key
68
72
  end
69
73
 
70
- Amfetamine.logger.info "Fetching object from cache: #{cache_key}"
71
- cache.fetch(cache_key) do
72
- Amfetamine.logger.info "Miss! #{cache_key}"
74
+ Amfetamine.logger.info "Fetching object from cache: #{@recent_cache_key}"
75
+ cache.fetch(@recent_cache_key) do
76
+ Amfetamine.logger.info "Miss! #{@recent_cache_key}"
73
77
  handle_request(method, key, { :query => conditions } )
74
78
  end
75
79
  else
@@ -105,12 +109,19 @@ module Amfetamine
105
109
  end
106
110
 
107
111
  if handle_response(response)
112
+
108
113
  begin
109
114
  update_attributes_from_response(response[:body])
110
115
  ensure
111
116
  clean_cache!
112
117
  end
113
- cache.set(singular_path, self.to_cacheable) if cacheable?
118
+
119
+ path = self.belongs_to_relationship? ? belongs_to_relationships.first.singular_path : singular_path
120
+ self.cache_key = path
121
+
122
+ cache.set(path, self.to_cacheable) if self.cacheable?
123
+ else
124
+ false
114
125
  end
115
126
  end
116
127
  end
@@ -22,15 +22,19 @@ module Amfetamine
22
22
  # TODO: Needs refactoring, now just want to make the test pass =)
23
23
  # Making assumption here that when response is nil, it should have possitive result. Needs refactor when slept more
24
24
  def handle_response(response)
25
- if response[:status] == :success || response[:status] == :created
25
+ case response[:status]
26
+ when :success, :created
26
27
  self.instance_variable_set('@notsaved', false)
27
28
  true
28
- elsif response[:status] == :errors
29
+ when :errors
29
30
  Amfetamine.logger.warn "Errors from response\n #{response[:body]}"
30
31
  response[:body].each do |attr, mesg|
31
32
  errors.add(attr.to_sym, mesg )
32
33
  end
33
34
  false
35
+ when :server_error
36
+ Amfetamine.logger.warn "Something went wrong at the remote end."
37
+ false
34
38
  end
35
39
  end
36
40
 
@@ -1,3 +1,3 @@
1
1
  module Amfetamine
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.6"
3
3
  end
@@ -36,10 +36,12 @@ describe Amfetamine::Base do
36
36
  context "#find" do
37
37
  it "should find dummy" do
38
38
  dummy.instance_variable_set('@notsaved', false)
39
- stub_single_response(dummy) do
40
- Dummy.find(dummy.id).should == dummy
39
+ Dummy.prevent_external_connections! do |r|
40
+ r.get { dummy }
41
+ response = Dummy.find(dummy.id)
42
+ response.should == dummy
43
+ response.should be_cached
41
44
  end
42
- dummy.should be_cached
43
45
  end
44
46
 
45
47
  it "should return nil if object not found" do
@@ -84,6 +86,7 @@ describe Amfetamine::Base do
84
86
  end
85
87
  new_dummy.should be_a(Dummy)
86
88
  new_dummy.should_not be_new
89
+ puts new_dummy.cache_key
87
90
  new_dummy.should be_cached
88
91
  end
89
92
 
@@ -104,12 +107,14 @@ describe Amfetamine::Base do
104
107
  end
105
108
 
106
109
  it "should update if response is succesful" do
107
- stub_update_response do
110
+ Dummy.prevent_external_connections! do |allowed|
111
+ allowed.put {}
108
112
  dummy.update_attributes({:title => 'zomg'})
109
113
  end
114
+
110
115
  dummy.should_not be_new
111
116
  dummy.title.should eq('zomg')
112
- dummy.should be_cached
117
+ dummy.should be_cached
113
118
  end
114
119
 
115
120
  it "should show errors if response is not succesful" do
@@ -90,6 +90,7 @@ describe Amfetamine::Relationships do
90
90
  stub_post_response(child) do
91
91
  new_child = dummy.create_child
92
92
  end
93
+ puts new_child.inspect
93
94
 
94
95
  new_child.should_not be_new
95
96
  new_child.should be_cached
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amfetamine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-24 00:00:00.000000000 Z
12
+ date: 2012-02-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70103214207640 !ruby/object:Gem::Requirement
16
+ requirement: &70274617798920 !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: *70103214207640
24
+ version_requirements: *70274617798920
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: guard
27
- requirement: &70103214207220 !ruby/object:Gem::Requirement
27
+ requirement: &70274617798500 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70103214207220
35
+ version_requirements: *70274617798500
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: guard-rspec
38
- requirement: &70103214206780 !ruby/object:Gem::Requirement
38
+ requirement: &70274617798080 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70103214206780
46
+ version_requirements: *70274617798080
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: ruby_gntp
49
- requirement: &70103214206320 !ruby/object:Gem::Requirement
49
+ requirement: &70274617797660 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70103214206320
57
+ version_requirements: *70274617797660
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: httparty
60
- requirement: &70103214205900 !ruby/object:Gem::Requirement
60
+ requirement: &70274617797220 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70103214205900
68
+ version_requirements: *70274617797220
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: fakeweb
71
- requirement: &70103214205480 !ruby/object:Gem::Requirement
71
+ requirement: &70274617796780 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70103214205480
79
+ version_requirements: *70274617796780
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: simplecov
82
- requirement: &70103214205060 !ruby/object:Gem::Requirement
82
+ requirement: &70274617796360 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70103214205060
90
+ version_requirements: *70274617796360
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: simplecov-rcov
93
- requirement: &70103214204620 !ruby/object:Gem::Requirement
93
+ requirement: &70274617795940 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *70103214204620
101
+ version_requirements: *70274617795940
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: dalli
104
- requirement: &70103214204180 !ruby/object:Gem::Requirement
104
+ requirement: &70274617795480 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :runtime
111
111
  prerelease: false
112
- version_requirements: *70103214204180
112
+ version_requirements: *70274617795480
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: activesupport
115
- requirement: &70103214203760 !ruby/object:Gem::Requirement
115
+ requirement: &70274617795040 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,10 +120,10 @@ dependencies:
120
120
  version: '0'
121
121
  type: :runtime
122
122
  prerelease: false
123
- version_requirements: *70103214203760
123
+ version_requirements: *70274617795040
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: activemodel
126
- requirement: &70103214203340 !ruby/object:Gem::Requirement
126
+ requirement: &70274617794500 !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
129
129
  - - ! '>='
@@ -131,10 +131,10 @@ dependencies:
131
131
  version: '0'
132
132
  type: :runtime
133
133
  prerelease: false
134
- version_requirements: *70103214203340
134
+ version_requirements: *70274617794500
135
135
  - !ruby/object:Gem::Dependency
136
136
  name: json
137
- requirement: &70103214233620 !ruby/object:Gem::Requirement
137
+ requirement: &70274617793960 !ruby/object:Gem::Requirement
138
138
  none: false
139
139
  requirements:
140
140
  - - ! '>='
@@ -142,10 +142,10 @@ dependencies:
142
142
  version: '0'
143
143
  type: :runtime
144
144
  prerelease: false
145
- version_requirements: *70103214233620
145
+ version_requirements: *70274617793960
146
146
  - !ruby/object:Gem::Dependency
147
147
  name: rake
148
- requirement: &70103214233200 !ruby/object:Gem::Requirement
148
+ requirement: &70274617825920 !ruby/object:Gem::Requirement
149
149
  none: false
150
150
  requirements:
151
151
  - - ! '>='
@@ -153,7 +153,7 @@ dependencies:
153
153
  version: '0'
154
154
  type: :runtime
155
155
  prerelease: false
156
- version_requirements: *70103214233200
156
+ version_requirements: *70274617825920
157
157
  description: Wraps REST to objects, provides caching and makes your app go Bzzz!
158
158
  email:
159
159
  - timon@exvo.com