motion-resource 0.0.1 → 0.0.2

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.
@@ -24,17 +24,21 @@ module MotionResource
24
24
  if block.nil?
25
25
  instance_variable_get("@#{name}") || []
26
26
  else
27
- cached = instance_variable_get("@#{name}")
28
- block.call(cached) and return if cached
27
+ if cached = instance_variable_get("@#{name}")
28
+ cached_response = instance_variable_get("@#{name}_response")
29
+ MotionResource::Base.request_block_call(block, cached, cached_response)
30
+ return
31
+ end
29
32
 
30
- Object.const_get(name.to_s.classify).find_all(params.call(self)) do |results|
33
+ Object.const_get(name.to_s.classify).find_all(params.call(self)) do |results, response|
31
34
  if results && results.first && results.first.respond_to?("#{backwards_association}=")
32
35
  results.each do |result|
33
36
  result.send("#{backwards_association}=", self)
34
37
  end
35
38
  end
36
39
  instance_variable_set("@#{name}", results)
37
- block.call(results)
40
+ instance_variable_set("@#{name}_response", response)
41
+ MotionResource::Base.request_block_call(block, results, response)
38
42
  end
39
43
  end
40
44
  end
@@ -59,12 +63,16 @@ module MotionResource
59
63
  if block.nil?
60
64
  instance_variable_get("@#{name}")
61
65
  else
62
- cached = instance_variable_get("@#{name}")
63
- block.call(cached) and return if cached
64
-
65
- Object.const_get(name.to_s.classify).find(self.send("#{name}_id"), params.call(self)) do |result|
66
+ if cached = instance_variable_get("@#{name}")
67
+ cached_response = instance_variable_get("@#{name}_response")
68
+ MotionResource::Base.request_block_call(block, cached, cached_response)
69
+ return
70
+ end
71
+
72
+ Object.const_get(name.to_s.classify).find(self.send("#{name}_id"), params.call(self)) do |result, response|
66
73
  instance_variable_set("@#{name}", result)
67
- block.call(result)
74
+ instance_variable_set("@#{name}_response", response)
75
+ MotionResource::Base.request_block_call(block, result, response)
68
76
  end
69
77
  end
70
78
  end
@@ -2,6 +2,14 @@ module MotionResource
2
2
  class Base
3
3
  attr_accessor :id
4
4
 
5
+ def self.subclasses
6
+ @subclasses ||= []
7
+ end
8
+
9
+ def self.inherited(subclass)
10
+ self.subclasses << subclass
11
+ end
12
+
5
13
  def initialize(params = {})
6
14
  @new_record = true
7
15
  update_attributes(params)
@@ -13,6 +21,7 @@ module MotionResource
13
21
 
14
22
  class << self
15
23
  def instantiate(json)
24
+ json = json.symbolize_keys
16
25
  raise ArgumentError, "No :id parameter given for #{self.name}.instantiate" unless json[:id]
17
26
 
18
27
  klass = if json[:type]
@@ -6,7 +6,7 @@ module MotionResource
6
6
 
7
7
  def update(&block)
8
8
  self.class.put(member_url, :payload => { self.class.name.underscore => attributes }) do |response, json|
9
- block.call json.blank? ? nil : self.class.instantiate(json) if block
9
+ self.class.request_block_call(block, json.blank? ? self : self.class.instantiate(json), response) if block
10
10
  end
11
11
  end
12
12
 
@@ -14,19 +14,19 @@ module MotionResource
14
14
  # weird heisenbug: Specs crash without that line :(
15
15
  dummy = self
16
16
  self.class.post(collection_url, :payload => { self.class.name.underscore => attributes }) do |response, json|
17
- block.call json.blank? ? nil : self.class.instantiate(json) if block
17
+ self.class.request_block_call(block, json.blank? ? self : self.class.instantiate(json), response) if block
18
18
  end
19
19
  end
20
20
 
21
21
  def destroy(&block)
22
22
  self.class.delete(member_url) do |response, json|
23
- block.call json.blank? ? nil : self.class.instantiate(json) if block
23
+ self.class.request_block_call(block, json.blank? ? nil : self.class.instantiate(json), response) if block
24
24
  end
25
25
  end
26
26
 
27
27
  def reload(&block)
28
28
  self.class.get(member_url) do |response, json|
29
- block.call json.blank? ? nil : self.class.instantiate(json) if block
29
+ self.class.request_block_call(block, json.blank? ? nil : self.class.instantiate(json), response) if block
30
30
  end
31
31
  end
32
32
  end
@@ -47,8 +47,7 @@ module MotionResource
47
47
  end
48
48
  end
49
49
  end
50
-
51
- private
50
+
52
51
  def request_block_call(block, default_arg, extra_arg)
53
52
  if block
54
53
  if block.arity == 1
@@ -0,0 +1,6 @@
1
+ module MotionResource
2
+ class Base
3
+ class_inheritable_accessor :logger
4
+ self.logger = MotionSupport::NullLogger.new
5
+ end
6
+ end
@@ -46,9 +46,11 @@ module MotionResource
46
46
  if self.default_url_options
47
47
  options.merge!(self.default_url_options)
48
48
  end
49
+ logger.log "#{method.upcase} #{complete_url(url)}"
49
50
  BubbleWrap::HTTP.send(method, complete_url(url), options) do |response|
50
51
  if response.ok?
51
52
  body = response.body.to_str.strip rescue nil
53
+ logger.log "response: #{body}"
52
54
  if body.blank?
53
55
  block.call(response, {})
54
56
  else
@@ -0,0 +1,16 @@
1
+ module MotionResource
2
+ module SpecHelpers
3
+ def self.extended(base)
4
+ base.after do
5
+ forget_instances_of(MotionResource::Base)
6
+ end
7
+ end
8
+
9
+ def forget_instances_of(klass)
10
+ klass.identity_map.clear
11
+ klass.subclasses.each do |subklass|
12
+ forget_instances_of(subklass)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module MotionResource
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -59,6 +59,15 @@ describe "belongs_to" do
59
59
  end
60
60
  end
61
61
 
62
+ it "should give cached HTTP response immediately if exist when called with a block" do
63
+ @comment = Comment.new
64
+ @comment.post = Post.new
65
+ @comment.instance_variable_set(:@post_response, "Test response")
66
+ @comment.post do |result, response|
67
+ response.should == "Test response"
68
+ end
69
+ end
70
+
62
71
  it "should cache resource after fetching" do
63
72
  @comment = Comment.new(:post_id => 1)
64
73
  @comment.post do |result|
@@ -70,6 +79,18 @@ describe "belongs_to" do
70
79
  @comment.post.should == @result
71
80
  end
72
81
  end
82
+
83
+ it "should give HTTP response to block" do
84
+ @comment = Comment.new(:post_id => 1)
85
+ @comment.post do |results, response|
86
+ @response = response
87
+ resume
88
+ end
89
+
90
+ wait_max 1.0 do
91
+ @response.should.be.ok
92
+ end
93
+ end
73
94
  end
74
95
 
75
96
  describe "writer" do
@@ -60,6 +60,15 @@ describe "has_many" do
60
60
  end
61
61
  end
62
62
 
63
+ it "should give cached HTTP response immediately if exist when called with a block" do
64
+ @post = Post.new
65
+ @post.comments = [Comment.new]
66
+ @post.instance_variable_set(:@comments_response, "Test response")
67
+ @post.comments do |results, response|
68
+ response.should == "Test response"
69
+ end
70
+ end
71
+
63
72
  it "should assign backward associations when fetching resources" do
64
73
  @post = Post.new
65
74
  @post.comments do |results|
@@ -83,6 +92,18 @@ describe "has_many" do
83
92
  @post.comments.should == @results
84
93
  end
85
94
  end
95
+
96
+ it "should give HTTP response to block" do
97
+ @post = Post.new
98
+ @post.comments do |results, response|
99
+ @response = response
100
+ resume
101
+ end
102
+
103
+ wait_max 1.0 do
104
+ @response.should.be.ok
105
+ end
106
+ end
86
107
  end
87
108
 
88
109
  describe "writer" do
@@ -1,13 +1,15 @@
1
1
  describe "scope" do
2
2
  extend WebStub::SpecHelpers
3
3
 
4
+ before do
5
+ stub_request(:get, "http://example.com/comments/recent.json").to_return(json: [{ id: 1, text: 'Whats up?' }])
6
+ end
7
+
4
8
  it "should define a custom url" do
5
9
  Comment.should.respond_to :recent_url
6
10
  end
7
11
 
8
12
  it "should fetch collection" do
9
- stub_request(:get, "http://example.com/comments/recent.json").to_return(json: [{ id: 1, text: 'Whats up?' }])
10
-
11
13
  Comment.recent do |results|
12
14
  @results = results
13
15
  resume
@@ -18,4 +20,15 @@ describe "scope" do
18
20
  @results.first.text.should == 'Whats up?'
19
21
  end
20
22
  end
23
+
24
+ it "should give HTTP response to block" do
25
+ Comment.recent do |results, response|
26
+ @response = response
27
+ resume
28
+ end
29
+
30
+ wait_max 1.0 do
31
+ @response.should.be.ok
32
+ end
33
+ end
21
34
  end
@@ -51,4 +51,12 @@ describe "base" do
51
51
  shape1.contents.should == 'something'
52
52
  end
53
53
  end
54
+
55
+ describe "subclasses" do
56
+ it "should keep track of subclasses" do
57
+ MotionResource::Base.subclasses.should.include(Shape)
58
+ Shape.subclasses.should.include(Rectangle)
59
+ MotionResource::Base.subclasses.should.not.include(Rectangle)
60
+ end
61
+ end
54
62
  end
@@ -30,14 +30,39 @@ describe "crud" do
30
30
 
31
31
  it "should create without json in response" do
32
32
  stub_request(:post, "http://example.com/comments.json").to_return(body: "")
33
- comment = Comment.new
34
- comment.create do |result|
33
+ @comment = Comment.new
34
+ @comment.create do |result|
35
35
  @result = result
36
36
  resume
37
37
  end
38
38
 
39
39
  wait_max 1.0 do
40
- @result.should.be.nil
40
+ @result.should == @comment
41
+ end
42
+ end
43
+
44
+ it "should create with empty json response" do
45
+ stub_request(:post, "http://example.com/comments.json").to_return(json: {})
46
+ @comment = Comment.new
47
+ @comment.create do |result|
48
+ @result = result
49
+ resume
50
+ end
51
+
52
+ wait_max 1.0 do
53
+ @result.should == @comment
54
+ end
55
+ end
56
+
57
+ it "should give HTTP response to block" do
58
+ stub_request(:post, "http://example.com/comments.json").to_return(json: {}, status_code: 404)
59
+ Comment.new.create do |result, response|
60
+ @response = response
61
+ resume
62
+ end
63
+
64
+ wait_max 1.0 do
65
+ @response.should.not.be.ok
41
66
  end
42
67
  end
43
68
  end
@@ -71,14 +96,39 @@ describe "crud" do
71
96
 
72
97
  it "should update without json in response" do
73
98
  stub_request(:put, "http://example.com/comments/10.json").to_return(body: "")
74
- comment = Comment.instantiate(:id => 10)
75
- comment.update do |result|
99
+ @comment = Comment.instantiate(:id => 10)
100
+ @comment.update do |result|
76
101
  @result = result
77
102
  resume
78
103
  end
79
104
 
80
105
  wait_max 1.0 do
81
- @result.should.be.nil
106
+ @result.should.be == @comment
107
+ end
108
+ end
109
+
110
+ it "should update with empty json response" do
111
+ stub_request(:put, "http://example.com/comments/10.json").to_return(json: {})
112
+ @comment = Comment.instantiate(:id => 10)
113
+ @comment.update do |result|
114
+ @result = result
115
+ resume
116
+ end
117
+
118
+ wait_max 1.0 do
119
+ @result.should.be == @comment
120
+ end
121
+ end
122
+
123
+ it "should give HTTP response to block" do
124
+ stub_request(:put, "http://example.com/comments/10.json").to_return(json: {}, status_code: 404)
125
+ Comment.instantiate(:id => 10).update do |result, response|
126
+ @response = response
127
+ resume
128
+ end
129
+
130
+ wait_max 1.0 do
131
+ @response.should.not.be.ok
82
132
  end
83
133
  end
84
134
  end
@@ -109,6 +159,18 @@ describe "crud" do
109
159
  @result.should.be.nil
110
160
  end
111
161
  end
162
+
163
+ it "should give HTTP response to block" do
164
+ stub_request(:delete, "http://example.com/comments/10.json").to_return(json: {}, status_code: 404)
165
+ Comment.instantiate(:id => 10).destroy do |result, response|
166
+ @response = response
167
+ resume
168
+ end
169
+
170
+ wait_max 1.0 do
171
+ @response.should.not.be.ok
172
+ end
173
+ end
112
174
  end
113
175
 
114
176
  describe "reload" do
@@ -137,5 +199,17 @@ describe "crud" do
137
199
  @result.should.be.nil
138
200
  end
139
201
  end
202
+
203
+ it "should give HTTP response to block" do
204
+ stub_request(:get, "http://example.com/comments/10.json").to_return(json: {}, status_code: 404)
205
+ Comment.instantiate(:id => 10).reload do |result, response|
206
+ @response = response
207
+ resume
208
+ end
209
+
210
+ wait_max 1.0 do
211
+ @response.should.not.be.ok
212
+ end
213
+ end
140
214
  end
141
215
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: motion-resource
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Thomas Kadauke
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-11-29 00:00:00 Z
13
+ date: 2012-12-31 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bubble-wrap
@@ -75,7 +75,9 @@ files:
75
75
  - lib/motion-resource/base.rb
76
76
  - lib/motion-resource/crud.rb
77
77
  - lib/motion-resource/find.rb
78
+ - lib/motion-resource/logger.rb
78
79
  - lib/motion-resource/requests.rb
80
+ - lib/motion-resource/spec_helpers.rb
79
81
  - lib/motion-resource/string.rb
80
82
  - lib/motion-resource/urls.rb
81
83
  - lib/motion-resource/version.rb
@@ -105,7 +107,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
105
107
  requirements:
106
108
  - - ">="
107
109
  - !ruby/object:Gem::Version
108
- hash: 1460069226748132029
110
+ hash: -3639208948173529629
109
111
  segments:
110
112
  - 0
111
113
  version: "0"
@@ -114,14 +116,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
114
116
  requirements:
115
117
  - - ">="
116
118
  - !ruby/object:Gem::Version
117
- hash: 1460069226748132029
119
+ hash: -3639208948173529629
118
120
  segments:
119
121
  - 0
120
122
  version: "0"
121
123
  requirements: []
122
124
 
123
125
  rubyforge_project:
124
- rubygems_version: 1.8.17
126
+ rubygems_version: 1.8.19
125
127
  signing_key:
126
128
  specification_version: 3
127
129
  summary: Access RESTful resources from your iOS app