ruby-satisfaction 0.1.0 → 0.2.0

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/lib/satisfaction.rb CHANGED
@@ -58,9 +58,15 @@ class Satisfaction
58
58
  end
59
59
 
60
60
  def me
61
- @me ||= Me.new('me', self)
62
- @me.load
63
- @me
61
+ me = satisfaction.identity_map.get_record(Me, 'me') do
62
+ Me.new('me', satisfaction)
63
+ end
64
+
65
+ if me.loaded?
66
+ me
67
+ else
68
+ me.load
69
+ end
64
70
  end
65
71
 
66
72
  def autoload?
@@ -68,15 +74,18 @@ class Satisfaction
68
74
  end
69
75
 
70
76
  def set_basic_auth(user, password)
77
+ identity_map.expire_record(Me, 'me')
71
78
  @user = user
72
79
  @password = password
73
80
  end
74
81
 
75
82
  def set_consumer(key, secret)
83
+ identity_map.expire_record(Me, 'me')
76
84
  @consumer = OAuth::Consumer.new(key, secret)
77
85
  end
78
86
 
79
87
  def set_token(token, secret)
88
+ identity_map.expire_record(Me, 'me')
80
89
  @token = OAuth::Token.new(token, secret)
81
90
  end
82
91
 
@@ -14,4 +14,8 @@ class Satisfaction::IdentityMap
14
14
  end
15
15
  result
16
16
  end
17
+
18
+ def expire_record(klass, id)
19
+ @records[[klass, id]] = nil
20
+ end
17
21
  end
@@ -12,6 +12,10 @@ class Satisfaction::Loader
12
12
 
13
13
  def initialize(options={})
14
14
  @options = options.reverse_merge({:cache => :hash})
15
+ reset_cache
16
+ end
17
+
18
+ def reset_cache
15
19
  @cache = case @options[:cache]
16
20
  when :hash then HashCache.new
17
21
  when :memcache then MemcacheCache.new(@options[:memcache] || {})
@@ -35,14 +39,20 @@ class Satisfaction::Loader
35
39
 
36
40
  case response
37
41
  when Net::HTTPNotModified
38
- return cache_record.body
42
+ return [:ok, cache_record.body]
39
43
  when Net::HTTPSuccess
40
44
  cache.put(uri, response)
41
- response.body
45
+ [:ok, response.body]
42
46
  when Net::HTTPMovedPermanently
43
47
  limit = options[:redirect_limit] || 3
44
48
  raise ArgumentError, "Too many redirects" unless limit > 0 #TODO: what is a better error here?
45
49
  get(response['location'], options.merge(:redirect_limit => limit - 1))
50
+ when Net::HTTPBadRequest
51
+ [:bad_request, response.body]
52
+ when Net::HTTPForbidden
53
+ [:forbidden, response.body]
54
+ when Net::HTTPUnauthorized
55
+ [:unauthorized, response.body]
46
56
  else
47
57
  raise "Explode: #{response.to_yaml}"
48
58
  end
@@ -14,12 +14,11 @@ end
14
14
 
15
15
  class Me < Person
16
16
  def path
17
- "/me"
17
+ loaded? ? super : "/me"
18
18
  end
19
19
 
20
- def load
21
- result = satisfaction.get("#{path}.json")
22
- self.attributes = JSON.parse(result)
23
- self
20
+ def was_loaded(result)
21
+ @id = self.attributes["id"]
22
+ setup_associations
24
23
  end
25
24
  end
@@ -19,16 +19,25 @@ class Resource < Satisfaction::HasSatisfaction
19
19
  end
20
20
 
21
21
  def load
22
- result = satisfaction.get("#{path}.json")
23
- self.attributes = JSON.parse(result)
24
- self
22
+ result = satisfaction.get("#{path}.json")
23
+
24
+ if result.first == :ok
25
+ self.attributes = JSON.parse(result.last)
26
+ was_loaded(result.last)
27
+ self
28
+ else
29
+ result
30
+ end
31
+ end
32
+
33
+ def was_loaded(result)
34
+ #override this to augment post-loading behavior
25
35
  end
26
36
 
27
37
  def delete
28
38
  satisfaction.delete("#{path}.json")
29
39
  end
30
40
 
31
-
32
41
  def put(attrs)
33
42
  params = requestify(attrs, self.class.name.underscore)
34
43
  result = satisfaction.put("#{path}.json", params)
@@ -175,16 +184,21 @@ class Page < Satisfaction::HasSatisfaction
175
184
 
176
185
  def load(force=false)
177
186
  return @data if loaded? && !force
187
+
188
+ result = satisfaction.get("#{@path}.json", @options.merge(:page => @page))
178
189
 
179
- results = satisfaction.get("#{@path}.json", @options.merge(:page => @page))
180
- json = JSON.parse(results)
181
- @total = json["total"]
190
+ if result.first == :ok
191
+ json = JSON.parse(result.last)
192
+ @total = json["total"]
182
193
 
183
- @data = json["data"].map do |result|
184
- obj = @klass.decode_sfn(result, satisfaction)
185
- satisfaction.identity_map.get_record(@klass, obj.id) do
186
- obj
194
+ @data = json["data"].map do |result|
195
+ obj = @klass.decode_sfn(result, satisfaction)
196
+ satisfaction.identity_map.get_record(@klass, obj.id) do
197
+ obj
198
+ end
187
199
  end
200
+ else
201
+ result
188
202
  end
189
203
  end
190
204
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-satisfaction
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Fleckenstein
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-04-09 00:00:00 -07:00
12
+ date: 2008-04-15 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency