acfs 0.6.0 → 0.7.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.
- checksums.yaml +4 -4
- data/README.md +15 -8
- data/lib/acfs.rb +0 -26
- data/lib/acfs/model/query_methods.rb +4 -3
- data/lib/acfs/request.rb +2 -1
- data/lib/acfs/service.rb +6 -4
- data/lib/acfs/service/middleware.rb +51 -0
- data/lib/acfs/service/queue.rb +16 -0
- data/lib/acfs/version.rb +1 -1
- data/spec/{service_spec.rb → acfs/service_spec.rb} +0 -0
- data/spec/acfs_spec.rb +19 -11
- data/spec/spec_helper.rb +3 -1
- data/spec/support/service.rb +3 -0
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 07e4fa8c5a501299db87e1fd63da042f42b4064d
|
4
|
+
data.tar.gz: 267732ea7fae5e6d110038ff8769ceb8ee3fb9dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1ef40e8272f8396f232e14efd645ded71b393cdeeea7ddd4057b3e671d212ab627d03e39b58e32e62f90e478b26656b1856ad48e9ad8cdf747c707ae284d5001
|
7
|
+
data.tar.gz: d5693941256b767e34fe7934bdfba838156af64f00d1c3d7bd29fc24a1483b4473d3adcfeba616d709183c14bbbe7db8790c8864a3d8918eb2db2b74ec3ea58d
|
data/README.md
CHANGED
@@ -10,7 +10,7 @@ TODO: Write a gem description
|
|
10
10
|
|
11
11
|
Add this line to your application's Gemfile:
|
12
12
|
|
13
|
-
gem 'acfs', '0.
|
13
|
+
gem 'acfs', '0.7.0'
|
14
14
|
|
15
15
|
**Note:** Acfs is under development. I'll try to avoid changes to the public
|
16
16
|
API but internal APIs may change quite often.
|
@@ -30,6 +30,12 @@ First you need to define your service(s):
|
|
30
30
|
```ruby
|
31
31
|
class UserService < Acfs::Service
|
32
32
|
self.base_url = 'http://users.myapp.org'
|
33
|
+
|
34
|
+
# You can configure middlewares you want use for the service here.
|
35
|
+
# Each service has it own middleware stack.
|
36
|
+
#
|
37
|
+
use Acfs::Middleware::JsonDecoder
|
38
|
+
use Acfs::Middleware::MessagePackDecoder
|
33
39
|
end
|
34
40
|
```
|
35
41
|
|
@@ -114,24 +120,25 @@ Acfs.run # This call will fire all request as parallel as possible.
|
|
114
120
|
@user.name # => "John
|
115
121
|
@comments.size # => 25
|
116
122
|
@friends[0].name # => "Miraculix"
|
123
|
+
```
|
117
124
|
|
118
125
|
## TODO
|
119
126
|
|
120
127
|
* Create/Update operations
|
121
128
|
* High level features
|
122
|
-
|
123
|
-
|
129
|
+
* Pagination? Filtering? (If service API provides such features.)
|
130
|
+
* Messaging Queue support for services and models
|
124
131
|
* Documentation
|
125
132
|
|
126
133
|
## Contributing
|
127
134
|
|
128
135
|
1. Fork it
|
129
136
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
137
|
+
4. Add specs for your feature
|
138
|
+
5. Implement your feature
|
139
|
+
6. Commit your changes (`git commit -am 'Add some feature'`)
|
140
|
+
7. Push to the branch (`git push origin my-new-feature`)
|
141
|
+
8. Create new Pull Request
|
135
142
|
|
136
143
|
## License
|
137
144
|
|
data/lib/acfs.rb
CHANGED
@@ -33,35 +33,9 @@ module Acfs
|
|
33
33
|
adapter.run
|
34
34
|
end
|
35
35
|
|
36
|
-
def queue(req, &block)
|
37
|
-
request = Request.new req
|
38
|
-
request.on_complete &block if block_given?
|
39
|
-
middleware.call request
|
40
|
-
end
|
41
|
-
|
42
36
|
def adapter
|
43
37
|
@adapter ||= Adapter::Typhoeus.new
|
44
38
|
end
|
45
|
-
|
46
|
-
def middleware
|
47
|
-
@middleware ||= proc do |request|
|
48
|
-
adapter.queue request
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
def use(klass, options = {})
|
53
|
-
@middlewares ||= []
|
54
|
-
|
55
|
-
return false if @middlewares.include? klass
|
56
|
-
|
57
|
-
@middlewares << klass
|
58
|
-
@middleware = klass.new(middleware, options)
|
59
|
-
end
|
60
|
-
|
61
|
-
def clear
|
62
|
-
@middleware = nil
|
63
|
-
@middlewares = nil
|
64
|
-
end
|
65
39
|
end
|
66
40
|
end
|
67
41
|
|
@@ -37,7 +37,7 @@ module Acfs::Model
|
|
37
37
|
def all(params = {}, &block)
|
38
38
|
collection = ::Acfs::Collection.new
|
39
39
|
|
40
|
-
|
40
|
+
request = Acfs::Request.new(url, params: params) do |response|
|
41
41
|
response.data.each do |obj|
|
42
42
|
collection << self.new.tap do |m|
|
43
43
|
m.attributes = obj
|
@@ -47,6 +47,7 @@ module Acfs::Model
|
|
47
47
|
collection.loaded!
|
48
48
|
block.call collection unless block.nil?
|
49
49
|
end
|
50
|
+
service.queue request
|
50
51
|
|
51
52
|
collection
|
52
53
|
end
|
@@ -56,12 +57,12 @@ module Acfs::Model
|
|
56
57
|
def find_single(id, opts, &block)
|
57
58
|
model = self.new
|
58
59
|
|
59
|
-
request = Acfs::Request.new url(id.to_s)
|
60
|
-
service.queue(request) do |response|
|
60
|
+
request = Acfs::Request.new url(id.to_s) do |response|
|
61
61
|
model.attributes = response.data
|
62
62
|
model.loaded!
|
63
63
|
block.call model unless block.nil?
|
64
64
|
end
|
65
|
+
service.queue request
|
65
66
|
|
66
67
|
model
|
67
68
|
end
|
data/lib/acfs/request.rb
CHANGED
@@ -11,7 +11,7 @@ module Acfs
|
|
11
11
|
|
12
12
|
include Request::Callbacks
|
13
13
|
|
14
|
-
def initialize(url, options = {})
|
14
|
+
def initialize(url, options = {}, &block)
|
15
15
|
@url = URI.parse(url).tap do |url|
|
16
16
|
@data = options.delete(:data) || nil
|
17
17
|
@format = options.delete(:format) || :json
|
@@ -20,6 +20,7 @@ module Acfs
|
|
20
20
|
|
21
21
|
url.query = nil # params.any? ? params.to_param : nil
|
22
22
|
end.to_s
|
23
|
+
on_complete &block if block_given?
|
23
24
|
end
|
24
25
|
|
25
26
|
def data?
|
data/lib/acfs/service.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
require 'acfs/service/middleware'
|
2
|
+
require 'acfs/service/queue'
|
3
|
+
|
1
4
|
module Acfs
|
2
5
|
|
3
6
|
# Service object.
|
@@ -6,6 +9,9 @@ module Acfs
|
|
6
9
|
attr_accessor :options
|
7
10
|
class_attribute :base_url
|
8
11
|
|
12
|
+
include Service::Queue
|
13
|
+
include Service::Middleware
|
14
|
+
|
9
15
|
def initialize(options = {})
|
10
16
|
@options = options
|
11
17
|
end
|
@@ -22,9 +28,5 @@ module Acfs
|
|
22
28
|
url += "/#{options[:suffix].to_s}" if options[:suffix]
|
23
29
|
url
|
24
30
|
end
|
25
|
-
|
26
|
-
def queue(request, &block)
|
27
|
-
Acfs.queue request, &block
|
28
|
-
end
|
29
31
|
end
|
30
32
|
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Acfs
|
2
|
+
class Service
|
3
|
+
|
4
|
+
# Module providing all function to register middlewares
|
5
|
+
# on services and process queued request through the
|
6
|
+
# middleware stack.
|
7
|
+
#
|
8
|
+
module Middleware
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
# Queue a new request. The request will travel through
|
12
|
+
# all registered middleware.
|
13
|
+
#
|
14
|
+
def queue(req)
|
15
|
+
super self.class.middleware.call req
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
|
20
|
+
# Register a new middleware to be used for this service.
|
21
|
+
#
|
22
|
+
# class MyService < Acfs::Service
|
23
|
+
# self.base_url = 'http://my.srv'
|
24
|
+
# use Acfs::Middleware::JsonDecoder
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
def use(klass, options = {})
|
28
|
+
@middlewares ||= []
|
29
|
+
|
30
|
+
return false if @middlewares.include? klass
|
31
|
+
|
32
|
+
@middlewares << klass
|
33
|
+
@middleware = klass.new(middleware, options)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Return top most middleware.
|
37
|
+
#
|
38
|
+
def middleware
|
39
|
+
@middleware ||= proc { |request| request}
|
40
|
+
end
|
41
|
+
|
42
|
+
# Clear all registered middlewares.
|
43
|
+
#
|
44
|
+
def clear
|
45
|
+
@middleware = nil
|
46
|
+
@middlewares = nil
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Acfs
|
2
|
+
class Service
|
3
|
+
|
4
|
+
# Allows to queue request on this service that will be
|
5
|
+
# delegated to global Acfs adapter queue.
|
6
|
+
#
|
7
|
+
module Queue
|
8
|
+
|
9
|
+
# Queue a new request on global adapter queue.
|
10
|
+
#
|
11
|
+
def queue(request)
|
12
|
+
Acfs.adapter.queue request
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/acfs/version.rb
CHANGED
File without changes
|
data/spec/acfs_spec.rb
CHANGED
@@ -3,17 +3,25 @@ require 'spec_helper'
|
|
3
3
|
describe "Acfs" do
|
4
4
|
|
5
5
|
before do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
stub_request(:get, "http://users.example.org/users/3").to_return(
|
14
|
-
|
15
|
-
|
16
|
-
stub_request(:get, "http://
|
6
|
+
headers = {}
|
7
|
+
stub_request(:get, "http://users.example.org/users").to_return(
|
8
|
+
body: MessagePack.dump([{ id: 1, name: "Anon", age: 12 }, { id: 2, name: "John", age: 26 }]),
|
9
|
+
headers: headers.merge({'Content-Type' => 'application/x-msgpack'}))
|
10
|
+
stub_request(:get, "http://users.example.org/users/2").to_return(
|
11
|
+
body: MessagePack.dump({ id: 2, name: "John", age: 26 }),
|
12
|
+
headers: headers.merge({'Content-Type' => 'application/x-msgpack'}))
|
13
|
+
stub_request(:get, "http://users.example.org/users/3").to_return(
|
14
|
+
body: MessagePack.dump({ id: 2, name: "Miraculix", age: 122 }),
|
15
|
+
headers: headers.merge({'Content-Type' => 'application/x-msgpack'}))
|
16
|
+
stub_request(:get, "http://users.example.org/users/100").to_return(
|
17
|
+
body: '{"id":2,"name":"Jimmy","age":45}',
|
18
|
+
headers: headers.merge({'Content-Type' => 'application/json'}))
|
19
|
+
stub_request(:get, "http://users.example.org/users/2/friends").to_return(
|
20
|
+
body: '[{"id":1,"name":"Anon","age":12}]',
|
21
|
+
headers: headers.merge({'Content-Type' => 'application/json'}))
|
22
|
+
stub_request(:get, "http://comments.example.org/comments?user=2").to_return(
|
23
|
+
body: '[{"id":1,"text":"Comment #1"},{"id":2,"text":"Comment #2"}]',
|
24
|
+
headers: headers.merge({'Content-Type' => 'application/json'}))
|
17
25
|
end
|
18
26
|
|
19
27
|
it 'should load single resource' do
|
data/spec/spec_helper.rb
CHANGED
data/spec/support/service.rb
CHANGED
@@ -1,10 +1,13 @@
|
|
1
1
|
|
2
2
|
class UserService < Acfs::Service
|
3
3
|
self.base_url = 'http://users.example.org'
|
4
|
+
use Acfs::Middleware::MessagePackDecoder
|
5
|
+
use Acfs::Middleware::JsonDecoder
|
4
6
|
end
|
5
7
|
|
6
8
|
class CommentService < Acfs::Service
|
7
9
|
self.base_url = 'http://comments.example.org'
|
10
|
+
use Acfs::Middleware::JsonDecoder
|
8
11
|
end
|
9
12
|
|
10
13
|
class MyUser
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acfs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan Graichen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-04-
|
11
|
+
date: 2013-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -220,17 +220,19 @@ files:
|
|
220
220
|
- lib/acfs/response.rb
|
221
221
|
- lib/acfs/response/formats.rb
|
222
222
|
- lib/acfs/service.rb
|
223
|
+
- lib/acfs/service/middleware.rb
|
224
|
+
- lib/acfs/service/queue.rb
|
223
225
|
- lib/acfs/version.rb
|
224
226
|
- spec/acfs/middleware/json_decoder_spec.rb
|
225
227
|
- spec/acfs/middleware/msgpack_decoder_spec.rb
|
226
228
|
- spec/acfs/request/callbacks_spec.rb
|
227
229
|
- spec/acfs/request_spec.rb
|
228
230
|
- spec/acfs/response/formats_spec.rb
|
231
|
+
- spec/acfs/service_spec.rb
|
229
232
|
- spec/acfs_spec.rb
|
230
233
|
- spec/model/attributes_spec.rb
|
231
234
|
- spec/model/initialization_spec.rb
|
232
235
|
- spec/model/locatable_spec.rb
|
233
|
-
- spec/service_spec.rb
|
234
236
|
- spec/spec_helper.rb
|
235
237
|
- spec/support/service.rb
|
236
238
|
homepage: ''
|
@@ -263,10 +265,10 @@ test_files:
|
|
263
265
|
- spec/acfs/request/callbacks_spec.rb
|
264
266
|
- spec/acfs/request_spec.rb
|
265
267
|
- spec/acfs/response/formats_spec.rb
|
268
|
+
- spec/acfs/service_spec.rb
|
266
269
|
- spec/acfs_spec.rb
|
267
270
|
- spec/model/attributes_spec.rb
|
268
271
|
- spec/model/initialization_spec.rb
|
269
272
|
- spec/model/locatable_spec.rb
|
270
|
-
- spec/service_spec.rb
|
271
273
|
- spec/spec_helper.rb
|
272
274
|
- spec/support/service.rb
|