lazy_resource 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,35 +6,66 @@ class SimpleLogger
6
6
  end
7
7
  end
8
8
 
9
- describe Typhoeus::Hydra do
9
+ describe "Logging added to Typhoeus" do
10
+ before :each do
11
+ LazyResource.debug = true
12
+ LazyResource.logger = SimpleLogger.new
13
+ @hydra = Thread.current[:request_queue] = Typhoeus::Hydra.new(:max_concurrency => LazyResource.max_concurrency)
14
+
15
+ LazyResource::HttpMock.respond_to do |responder|
16
+ responder.get('http://example.com', '')
17
+ responder.get('http://example.com/users/1', { :name => 'Andrew' }.to_json)
18
+ end
19
+ end
20
+
10
21
  describe '#run_with_logging' do
11
22
  before :each do
12
- LazyResource.debug = true
13
- LazyResource.logger = SimpleLogger.new
14
- @hydra = Typhoeus::Hydra.new
15
23
  @hydra.stub!(:run_without_logging)
16
24
  @multi = @hydra.send(:instance_variable_get, :"@multi")
17
25
  end
18
26
 
19
27
  it 'logs if logging is enabled, there are items to process, and the queue has not yet started processing' do
20
- @multi.stub!(:easy_handles).and_return([1,2,3])
21
- @multi.stub!(:running_count).and_return(0)
22
- LazyResource.logger.should_receive(:info).twice
28
+ @multi.stub(:easy_handles).and_return([1,2,3])
29
+ @multi.stub(:running_count).and_return(0)
30
+ ActiveSupport::Notifications.should_receive(:instrument).twice
23
31
  @hydra.run_with_logging
24
32
  end
25
33
 
26
34
  it 'does not log if there are no items to process' do
27
- @multi.stub!(:easy_handles).and_return([])
28
- @multi.stub!(:running_count).and_return(0)
29
- LazyResource.logger.should_not_receive(:info)
35
+ @multi.stub(:easy_handles).and_return([])
36
+ @multi.stub(:running_count).and_return(0)
37
+ ActiveSupport::Notifications.should_not_receive(:instrument)
30
38
  @hydra.run_with_logging
31
39
  end
32
40
 
33
41
  it 'does not log if the queue is already being processed' do
34
- @multi.stub!(:easy_handles).and_return([1,2,3])
35
- @multi.stub!(:running_count).and_return(3)
36
- LazyResource.logger.should_not_receive(:info)
42
+ @multi.stub(:easy_handles).and_return([1,2,3])
43
+ @multi.stub(:running_count).and_return(3)
44
+ ActiveSupport::Notifications.should_not_receive(:instrument)
37
45
  @hydra.run_with_logging
38
46
  end
39
47
  end
48
+
49
+ describe '#items_queued?' do
50
+ it 'returns true if there are items queued' do
51
+ 3.times { User.find(1) }
52
+ @hydra.items_queued?.should == true
53
+ end
54
+ end
55
+
56
+ describe 'logging' do
57
+ it 'logs if logging is enabled, there are items to process, and the queue has not yet started processing' do
58
+ users = []
59
+ 3.times { users << User.find(1) }
60
+ # twice for the start/end, and once each for every user (3)
61
+ ActiveSupport::Notifications.should_receive(:instrument).exactly(5).times
62
+ users.each { |u| u.name }
63
+ end
64
+
65
+ it 'does not log if there are no items to process' do
66
+ users = User.find(1)
67
+ users.name
68
+ ActiveSupport::Notifications.should_not_receive(:instrument)
69
+ end
70
+ end
40
71
  end
@@ -22,10 +22,35 @@ describe LazyResource do
22
22
 
23
23
  describe '#debug' do
24
24
  it 'logs when a request completes' do
25
- LazyResource.logger.should_receive(:info)
25
+ ActiveSupport::Notifications.should_receive(:instrument).with('request.lazy_resource', kind_of(Hash))
26
26
  request = LazyResource::Request.new('http://example.com', User.new)
27
27
  request.response = Typhoeus::Response.new
28
28
  request.execute_callbacks
29
29
  end
30
30
  end
31
+
32
+ describe '#deprecate' do
33
+ it 'logs a message about a deprecation' do
34
+ LazyResource.logger.should_receive(:info).with("a deprecation from file.rb#123")
35
+ LazyResource.deprecate('a deprecation', 'file.rb', 123)
36
+ end
37
+ end
38
+
39
+ describe '#max_concurrency=' do
40
+ it 'sets the max_concurrency' do
41
+ LazyResource.max_concurrency = 100
42
+ LazyResource.max_concurrency.should == 100
43
+ end
44
+ end
45
+
46
+ describe '#max_concurrency' do
47
+ before :each do
48
+ Thread.current[:request_queue] = nil
49
+ end
50
+
51
+ it 'determines the amount of maximum concurrent requests Hydra will make' do
52
+ LazyResource.max_concurrency = 100
53
+ User.request_queue.max_concurrency.should == 100
54
+ end
55
+ end
31
56
  end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe LazyResource::LogSubscriber do
4
+ let(:event) do
5
+ OpenStruct.new({
6
+ payload: {
7
+ code: 200,
8
+ time: 5,
9
+ url: "http://google.com"
10
+ }
11
+ })
12
+ end
13
+
14
+ describe '#request' do
15
+ subject { LazyResource::LogSubscriber.new }
16
+
17
+ before do
18
+ LazyResource::LogSubscriber.attach_to(:lazy_resource)
19
+
20
+ subject.stub(:info) do |message|
21
+ message
22
+ end
23
+ end
24
+
25
+ it 'logs the request using the info level' do
26
+ subject.should_receive(:info).with(any_args)
27
+ subject.request(event)
28
+ end
29
+
30
+ it 'includes the response code' do
31
+ subject.request(event).should =~ /200/
32
+ end
33
+
34
+ it 'includes the response time' do
35
+ subject.request(event).should =~ /5000ms/
36
+ end
37
+
38
+ it 'includes the request url' do
39
+ subject.request(event).should =~ /http:\/\/google\.com/
40
+ end
41
+
42
+ after do
43
+ ActiveSupport::LogSubscriber.log_subscribers.clear
44
+ end
45
+ end
46
+ end
@@ -173,5 +173,30 @@ describe LazyResource::Relation do
173
173
  relation = LazyResource::Relation.new(User)
174
174
  lambda { relation.foo }.should raise_error(NoMethodError)
175
175
  end
176
+
177
+ it 'raises an error if request_error is set' do
178
+ relation = LazyResource::Relation.new(User, fetched: true)
179
+ relation.request_error = StandardError.new
180
+ lambda { relation.to_a }.should raise_error(StandardError)
181
+ end
182
+ end
183
+
184
+ describe '#method' do
185
+ it 'returns the provided method as a symbol' do
186
+ relation = LazyResource::Relation.new(User)
187
+ relation.method = 'post'
188
+ relation.method.should == :post
189
+ end
190
+
191
+ it 'downcases the returned method' do
192
+ relation = LazyResource::Relation.new(User)
193
+ relation.method = 'POST'
194
+ relation.method.should == :post
195
+ end
196
+
197
+ it 'returns nil when no method has been provided' do
198
+ relation = LazyResource::Relation.new(User)
199
+ relation.method.should be_nil
200
+ end
176
201
  end
177
202
  end
@@ -18,6 +18,7 @@ describe LazyResource::Request do
18
18
 
19
19
  after :each do
20
20
  Thread.current[:default_headers] = nil
21
+ Thread.current[:default_params] = nil
21
22
  end
22
23
 
23
24
  it 'sets a default Accept header of application/json' do
@@ -35,6 +36,12 @@ describe LazyResource::Request do
35
36
  request = LazyResource::Request.new('http://example.com/api', nil)
36
37
  request.options[:headers][:"X-Access-Token"].should == 'abc'
37
38
  end
39
+
40
+ it 'merged the params from the current thread' do
41
+ Thread.current[:default_params] = { :"access_token" => 'abc' }
42
+ request = LazyResource::Request.new('https://example.com/api', nil)
43
+ request.options[:params][:"access_token"].should == 'abc'
44
+ end
38
45
  end
39
46
 
40
47
  describe '#parse' do
@@ -86,7 +93,7 @@ describe LazyResource::Request do
86
93
  end
87
94
  end
88
95
 
89
- describe '#handle_errors' do
96
+ describe '#error' do
90
97
  [
91
98
  [301, LazyResource::Redirection],
92
99
  [400, LazyResource::BadRequest],
@@ -100,11 +107,11 @@ describe LazyResource::Request do
100
107
  [500, LazyResource::ServerError]
101
108
  ].each do |error|
102
109
  describe "status: #{error[0]}" do
103
- it "raises #{error[1]}" do
110
+ it "returns #{error[1]}" do
104
111
  request = LazyResource::Request.new('http://example.com', nil)
105
112
  response = Typhoeus::Response.new(:code => error[0], :headers => {}, :body => '', :time => 0.3)
106
113
  request.response = response
107
- lambda { request.handle_errors }.should raise_error(error[1])
114
+ request.error.should be_a(error[1])
108
115
  end
109
116
  end
110
117
  end
@@ -114,7 +121,7 @@ describe LazyResource::Request do
114
121
  request = LazyResource::Request.new('http://example.com', nil)
115
122
  response = Typhoeus::Response.new(:code => 402, :headers => {}, :body => '', :time => 0.3)
116
123
  request.response = response
117
- lambda { request.handle_errors }.should raise_error(LazyResource::ClientError)
124
+ request.error.should be_a(LazyResource::ClientError)
118
125
  end
119
126
  end
120
127
  end
@@ -44,6 +44,7 @@ describe LazyResource::ResourceQueue do
44
44
  it 'sends the requests to the request queue and runs the request queue' do
45
45
  @queue.queue(@relation)
46
46
  @queue.should_receive(:send_to_request_queue!)
47
+ @queue.request_queue.stub(:items_queued?).and_return(true)
47
48
  @queue.request_queue.should_receive(:run)
48
49
  @queue.run
49
50
  end
@@ -56,11 +57,57 @@ describe LazyResource::ResourceQueue do
56
57
  @queue.send_to_request_queue!
57
58
  @queue.instance_variable_get("@queue").should == []
58
59
  end
60
+
61
+ it 'sends the request to the queue using the relation\'s specified method' do
62
+ LazyResource::Request
63
+ .should_receive(:new)
64
+ .with(anything(), anything(), hash_including(:method => :post))
65
+ .and_call_original
66
+
67
+ @relation.method = :post
68
+ @queue.queue(@relation)
69
+ @queue.send_to_request_queue!
70
+ end
59
71
  end
60
72
 
61
73
  describe '#url_for' do
62
74
  it 'creates a URL for the given resource' do
63
75
  @queue.url_for(@relation).should == 'http://example.com/users'
64
76
  end
77
+
78
+ it 'respects the "from" option when set on a Relation object' do
79
+ @relation.from = 'people'
80
+ @queue.url_for(@relation).should == 'http://example.com/people'
81
+ end
82
+
83
+ it 'respects the "from" option when set on a Resource class' do
84
+ User.from = 'people'
85
+ @queue.url_for(@relation).should == 'http://example.com/people'
86
+ User.from = nil
87
+ end
88
+
89
+ context 'using Relation#route' do
90
+ before :each do
91
+ @relation = User.where(:_route => '/people/:name')
92
+ end
93
+
94
+ it 'respects the route' do
95
+ @queue.url_for(@relation).should == 'http://example.com/people/:name'
96
+ end
97
+
98
+ it 'interpolates the route with where_values' do
99
+ @relation.where(:name => 'Andrew')
100
+ @queue.url_for(@relation).should == 'http://example.com/people/Andrew'
101
+ end
102
+
103
+ it 'adds the Relation\'s host if one is not already present' do
104
+ @queue.url_for(@relation).should == 'http://example.com/people/:name'
105
+ end
106
+
107
+ it 'does not add the Relation\'s host if one is already present' do
108
+ @relation = User.where(:_route => 'http://another_example.com/people/:name')
109
+ @queue.url_for(@relation).should == 'http://another_example.com/people/:name'
110
+ end
111
+ end
65
112
  end
66
113
  end
@@ -117,7 +117,7 @@ describe LazyResource::Resource do
117
117
  user = User.new(:name => 'Andrew')
118
118
  params = ['http://example.com/users', user, {
119
119
  :method => :post,
120
- :params => { :user => { 'name' => 'Andrew' } }
120
+ :body => { 'user' => { 'name' => 'Andrew' } }.to_json
121
121
  }]
122
122
  request = LazyResource::Request.new(*params)
123
123
  LazyResource::Request.should_receive(:new).with(*params).and_return(request)
@@ -148,7 +148,7 @@ describe LazyResource::Resource do
148
148
  user.name = 'James'
149
149
  params = ['http://example.com/users/1', user, {
150
150
  :method => :put,
151
- :params => { :user => { 'name' => 'James' } }
151
+ :body => { 'user' => { 'name' => 'James' } }.to_json
152
152
  }]
153
153
  request = LazyResource::Request.new(*params)
154
154
  LazyResource::Request.should_receive(:new).with(*params).and_return(request)
@@ -185,7 +185,7 @@ describe LazyResource::Resource do
185
185
  user = User.load(:name => 'Andrew', :id => 1)
186
186
  params = ['http://example.com/users/1', user, {
187
187
  :method => :put,
188
- :params => { :user => { 'name' => 'James' } }
188
+ :body => { 'user' => { 'name' => 'James' } }.to_json
189
189
  }]
190
190
  request = LazyResource::Request.new(*params)
191
191
  LazyResource::Request.should_receive(:new).with(*params).and_return(request)
@@ -243,8 +243,7 @@ describe LazyResource::Resource do
243
243
 
244
244
  params = { :id => 1, :created_at => DateTime.now.to_s }
245
245
  user = User.load(params)
246
- params[:created_at_in_words] = "1 second ago"
247
- user.as_json(:include_time_ago_in_words => true).should == params
246
+ user.as_json(:include_time_ago_in_words => true)[:created_at_in_words].should =~ /\d second(s)? ago/
248
247
  end
249
248
  end
250
249
 
@@ -290,13 +289,28 @@ describe LazyResource::Resource do
290
289
  LazyResource::Request.should_receive(:new).with(anything, anything, :headers => { :foo => 'buzz' }).and_return(request)
291
290
  User.find(1, {}, { :headers => { :foo => 'buzz' } })
292
291
  end
292
+
293
+ it 'does not trigger fetch_all' do
294
+ User.should_not_receive(:fetch_all)
295
+ 10.times { User.find(1) }
296
+ end
293
297
  end
294
298
 
295
299
  describe '.where' do
300
+ after :each do
301
+ User.from = nil
302
+ end
303
+
296
304
  it 'creates a new relation with the passed where values' do
297
305
  users = User.where(:name => 'Andrew')
298
306
  users.where_values.should == { :name => 'Andrew' }
299
307
  end
308
+
309
+ it 'forwards the from value' do
310
+ User.from = :bars
311
+ users = User.where(:foo => 'bar')
312
+ users.from.should == :bars
313
+ end
300
314
  end
301
315
 
302
316
  describe '.order' do
@@ -350,7 +364,7 @@ describe LazyResource::Resource do
350
364
  user = User.new(:name => 'Andrew')
351
365
  params = ['http://example.com/users', user, {
352
366
  :method => :post,
353
- :params => { :user => { 'name' => 'Andrew' } }
367
+ :body => { 'user' => { 'name' => 'Andrew' } }.to_json
354
368
  }]
355
369
  request = LazyResource::Request.new(*params)
356
370
  LazyResource::Request.should_receive(:new).with(*params).and_return(request)
@@ -13,15 +13,16 @@ class Item
13
13
  def site
14
14
  'http://example.com/'
15
15
  end
16
+
17
+ def primary_key_name
18
+ 'id'
19
+ end
16
20
  end
17
21
 
18
22
  def initialize
23
+ @id = 1
19
24
  @attributes = {}
20
25
  end
21
-
22
- def primary_key
23
- 1
24
- end
25
26
  end
26
27
 
27
28
  describe LazyResource::UrlGeneration do
@@ -151,5 +152,10 @@ describe LazyResource::UrlGeneration do
151
152
  prefix_options.should == { :comment_id => 1 }
152
153
  query_options.should == { :name => 'Andrew' }
153
154
  end
155
+
156
+ it 'ignores _ids params' do
157
+ prefix_options, query_options = Item.split_options(:name => 'Andrew', :comment_ids => '1,2')
158
+ query_options.should == { :name => 'Andrew', :comment_ids => '1,2' }
159
+ end
154
160
  end
155
161
  end
@@ -21,5 +21,5 @@ LazyResource.configure do |config|
21
21
  end
22
22
 
23
23
  RSpec.configure do |config|
24
- config.mock_with nil
24
+ config.mock_with :rspec
25
25
  end
metadata CHANGED
@@ -1,52 +1,46 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazy_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
5
- prerelease:
4
+ version: 0.5.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Andrew Latimer
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-02-15 00:00:00.000000000 Z
11
+ date: 2014-10-23 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activemodel
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ~>
20
18
  - !ruby/object:Gem::Version
21
- version: 3.1.0
19
+ version: '3.1'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ~>
28
25
  - !ruby/object:Gem::Version
29
- version: 3.1.0
26
+ version: '3.1'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: activesupport
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ~>
36
32
  - !ruby/object:Gem::Version
37
- version: 3.1.0
33
+ version: '3.1'
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ~>
44
39
  - !ruby/object:Gem::Version
45
- version: 3.1.0
40
+ version: '3.1'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: json
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ! '>='
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ! '>='
60
53
  - !ruby/object:Gem::Version
@@ -62,19 +55,17 @@ dependencies:
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: typhoeus
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
59
  - - '='
68
60
  - !ruby/object:Gem::Version
69
- version: 0.6.1
61
+ version: 0.6.6
70
62
  type: :runtime
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
66
  - - '='
76
67
  - !ruby/object:Gem::Version
77
- version: 0.6.1
68
+ version: 0.6.6
78
69
  description: ActiveResource with its feet up. The write less, do more consumer of
79
70
  delicious APIs.
80
71
  email:
@@ -86,9 +77,11 @@ files:
86
77
  - .gitignore
87
78
  - .rspec
88
79
  - .rvmrc
80
+ - .travis.yml
89
81
  - Gemfile
90
82
  - Guardfile
91
83
  - LICENSE
84
+ - NOTES.md
92
85
  - README.md
93
86
  - Rakefile
94
87
  - examples/github.rb
@@ -99,6 +92,7 @@ files:
99
92
  - lib/lazy_resource/errors.rb
100
93
  - lib/lazy_resource/ext/typhoeus.rb
101
94
  - lib/lazy_resource/http_mock.rb
95
+ - lib/lazy_resource/log_subscriber.rb
102
96
  - lib/lazy_resource/mapping.rb
103
97
  - lib/lazy_resource/relation.rb
104
98
  - lib/lazy_resource/request.rb
@@ -115,6 +109,7 @@ files:
115
109
  - spec/lazy_resource/errors_spec.rb
116
110
  - spec/lazy_resource/ext/typhoeus_spec.rb
117
111
  - spec/lazy_resource/lazy_resource_spec.rb
112
+ - spec/lazy_resource/log_subscriber_spec.rb
118
113
  - spec/lazy_resource/mapping_spec.rb
119
114
  - spec/lazy_resource/relation_spec.rb
120
115
  - spec/lazy_resource/request_spec.rb
@@ -125,33 +120,26 @@ files:
125
120
  - spec/spec_helper.rb
126
121
  homepage: http://github.com/ahlatimer/lazy_resource
127
122
  licenses: []
123
+ metadata: {}
128
124
  post_install_message:
129
125
  rdoc_options: []
130
126
  require_paths:
131
127
  - lib
132
128
  required_ruby_version: !ruby/object:Gem::Requirement
133
- none: false
134
129
  requirements:
135
130
  - - ! '>='
136
131
  - !ruby/object:Gem::Version
137
132
  version: '0'
138
- segments:
139
- - 0
140
- hash: -3444122050560505401
141
133
  required_rubygems_version: !ruby/object:Gem::Requirement
142
- none: false
143
134
  requirements:
144
135
  - - ! '>='
145
136
  - !ruby/object:Gem::Version
146
137
  version: '0'
147
- segments:
148
- - 0
149
- hash: -3444122050560505401
150
138
  requirements: []
151
139
  rubyforge_project:
152
- rubygems_version: 1.8.24
140
+ rubygems_version: 2.2.1
153
141
  signing_key:
154
- specification_version: 3
142
+ specification_version: 4
155
143
  summary: ActiveResource with its feet up. The write less, do more consumer of delicious
156
144
  APIs.
157
145
  test_files:
@@ -163,6 +151,7 @@ test_files:
163
151
  - spec/lazy_resource/errors_spec.rb
164
152
  - spec/lazy_resource/ext/typhoeus_spec.rb
165
153
  - spec/lazy_resource/lazy_resource_spec.rb
154
+ - spec/lazy_resource/log_subscriber_spec.rb
166
155
  - spec/lazy_resource/mapping_spec.rb
167
156
  - spec/lazy_resource/relation_spec.rb
168
157
  - spec/lazy_resource/request_spec.rb