acfs 0.40.1.rc1 → 0.41.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1f852836691662fa9073035cfc5a63c9bee7bee9
4
- data.tar.gz: 7e515c491ba534638dddf22f79df45d40246169f
3
+ metadata.gz: 1f7155842cf931c03b3f312ffe2eebcb67e7c307
4
+ data.tar.gz: fa10f1af70144fd949948c932c48027d03a3d2b2
5
5
  SHA512:
6
- metadata.gz: fc53f946379a3d75a368c50ed76315f96d3529eaa4ee816828801b5b19497230867acbe040d23a9f0835c0ed36f5e88ef4d0f87f411269128148e5085e7e9b4f
7
- data.tar.gz: f52b9cc11d7b34f04d8fa09cdc15d4ad9979a5a7e05a6f9814d81979c644d71ebcd5e4f1c5138d7a238ad82c8300787ce9505e49ac53e7955c25b4dfbe89bce8
6
+ metadata.gz: 65a0ef1e19da01ce233ec7f3dc4c1aa7bc7ca9e908577fcdebea882ae2ae5756eb9b1d0a01980a4f67ed5b34f1e0cbd76579761ac354affbfdf8798808232994
7
+ data.tar.gz: ee6ebab916d98f53e75d741408f7212e3c822d338c30bb4b950168cd2588d7c90d9c893c414390e9e3986fa6a8c8e048cdcb95a6cd1925fb8eeea1f43fdc4cb1
@@ -31,13 +31,16 @@ module Acfs
31
31
  module Middleware
32
32
  extend ActiveSupport::Autoload
33
33
  require 'acfs/middleware/base'
34
+ require 'acfs/middleware/serializer'
34
35
 
35
36
  autoload :Print
36
37
  autoload :Logger
37
- autoload :JsonDecoder
38
- autoload :MessagePackDecoder, 'acfs/middleware/msgpack_decoder'
39
- autoload :JsonEncoder
40
- autoload :MessagePackEncoder, 'acfs/middleware/msgpack_encoder'
38
+ autoload :JSON
39
+ autoload :JsonDecoder, 'acfs/middleware/json'
40
+ autoload :JsonEncoder, 'acfs/middleware/json'
41
+ autoload :MessagePack, 'acfs/middleware/msgpack'
42
+ autoload :MessagePackDecoder, 'acfs/middleware/msgpack'
43
+ autoload :MessagePackEncoder, 'acfs/middleware/msgpack'
41
44
  end
42
45
 
43
46
  module Adapter
@@ -0,0 +1,25 @@
1
+ require 'multi_json'
2
+
3
+ module Acfs
4
+ module Middleware
5
+
6
+ # A middleware to encore request data using JSON.
7
+ #
8
+ class JSON < Serializer
9
+ def mime
10
+ ::Mime::JSON
11
+ end
12
+
13
+ def encode(data)
14
+ ::MultiJson.dump data
15
+ end
16
+
17
+ def decode(body)
18
+ ::MultiJson.load body
19
+ end
20
+ end
21
+
22
+ JsonDecoder = JSON
23
+ JsonEncoder = JSON
24
+ end
25
+ end
@@ -0,0 +1,27 @@
1
+ require 'msgpack'
2
+ require 'action_dispatch'
3
+
4
+ module Acfs
5
+ module Middleware
6
+ class MessagePack < Serializer
7
+ unless defined?(::Mime::MSGPACK)
8
+ ::Mime::Type.register 'application/x-msgpack', :msgpack
9
+ end
10
+
11
+ def mime
12
+ ::Mime::MSGPACK
13
+ end
14
+
15
+ def encode(data)
16
+ ::MessagePack.pack data
17
+ end
18
+
19
+ def decode(body)
20
+ ::MessagePack.unpack body
21
+ end
22
+ end
23
+
24
+ MessagePackEncoder = MessagePack
25
+ MessagePackDecoder = MessagePack
26
+ end
27
+ end
@@ -0,0 +1,42 @@
1
+ module Acfs
2
+ module Middleware
3
+
4
+ # A base middleware that does not modify request or response.
5
+ # Can be used as super class for custom middleware implementations.
6
+ #
7
+ class Serializer < Base
8
+ def encode(data)
9
+ raise NotImplementedError
10
+ end
11
+
12
+ def decode(data)
13
+ raise NotImplementedError
14
+ end
15
+
16
+ def mime
17
+ raise NotImplementedError
18
+ end
19
+
20
+ def call(request)
21
+ unless request.headers['Content-Type']
22
+ request.body = encode request.data
23
+ request.headers['Content-Type'] = mime
24
+ end
25
+
26
+ accept = request.headers['Accept'].to_s.split(',')
27
+ accept << "#{mime};q=#{options.fetch(:q, 1)}"
28
+ request.headers['Accept'] = accept.join(',')
29
+
30
+ request.on_complete do |response, nxt|
31
+ if mime == response.content_type
32
+ response.data = decode response.body
33
+ end
34
+
35
+ nxt.call response
36
+ end
37
+
38
+ app.call(request)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -59,14 +59,10 @@ module Acfs
59
59
  return if running?
60
60
 
61
61
  enqueue_operations
62
-
63
- @running = true
64
- adapter.start
62
+ start_all
65
63
  rescue
66
64
  queue.clear
67
65
  raise
68
- ensure
69
- @running = false
70
66
  end
71
67
 
72
68
  def clear
@@ -76,6 +72,14 @@ module Acfs
76
72
  end
77
73
 
78
74
  private
75
+
76
+ def start_all
77
+ @running = true
78
+ adapter.start
79
+ ensure
80
+ @running = false
81
+ end
82
+
79
83
  def enqueue_operations
80
84
  while (op = queue.shift)
81
85
  op_request(op) { |req| adapter.queue req }
@@ -1,9 +1,9 @@
1
1
  module Acfs
2
2
  module VERSION
3
3
  MAJOR = 0
4
- MINOR = 40
5
- PATCH = 1
6
- STAGE = 'rc1'
4
+ MINOR = 41
5
+ PATCH = 0
6
+ STAGE = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, PATCH, STAGE].reject(&:nil?).join('.')
9
9
 
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe Acfs::Middleware::JSON do
4
+ let(:data) { [{id: 1, name: "Anon"},{id: 2, name:"John", friends: [ 1 ]}] }
5
+ let(:body) { '' }
6
+ let(:headers) { {} }
7
+ let(:request) { Acfs::Request.new 'url', method: 'GET', data: data }
8
+ let(:response) { Acfs::Response.new request, status: 200, headers: headers, body: body }
9
+ let(:decoder) { Acfs::Middleware::JSON.new lambda { |req| req } }
10
+
11
+ before do
12
+ decoder.call request
13
+ end
14
+
15
+ context 'API compatibility' do
16
+ subject { Acfs::Middleware::JSON }
17
+ it { is_expected.to eql Acfs::Middleware::JsonDecoder }
18
+ it { is_expected.to eql Acfs::Middleware::JsonEncoder }
19
+ end
20
+
21
+ describe 'encode' do
22
+ context 'with not serialized request' do
23
+ it 'should set Content-Type' do
24
+ expect(request.headers['Content-Type']).to eq 'application/json'
25
+ end
26
+
27
+ it 'should append Accept header' do
28
+ expect(request.headers['Accept']).to eq 'application/json;q=1'
29
+ end
30
+
31
+ it 'should serialize data to JSON' do
32
+ expect(JSON.parse(request.body)).to eq data.map(&:stringify_keys)
33
+ end
34
+ end
35
+ end
36
+
37
+ describe 'decode' do
38
+ context 'with JSON response' do
39
+ let(:headers) { {'Content-Type' => 'application/json; charset=utf-8'} }
40
+ let(:body) { data.to_json }
41
+
42
+ it 'should decode body data' do
43
+ request.complete! response
44
+
45
+ expect(response.data).to be == data.map(&:stringify_keys)
46
+ end
47
+ end
48
+
49
+ context 'with invalid JSON response' do
50
+ let(:headers) { {'Content-Type' => 'application/json'} }
51
+ let(:body) { data.to_json[4..-4] }
52
+
53
+ it 'should raise an error' do
54
+ expect { request.complete! response }.to raise_error(MultiJson::LoadError)
55
+ end
56
+ end
57
+
58
+ context 'without JSON response' do
59
+ let(:headers) { {'Content-Type' => 'application/text'} }
60
+ let(:body) { data.to_json }
61
+
62
+ it 'should not decode non-JSON encoded responses' do
63
+ request.complete! response
64
+
65
+ expect(response.data).to be_nil
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe Acfs::Middleware::MessagePack do
4
+ let(:data) { [{id: 1, name: 'Anon'}, {id: 2, name: 'John', friends: [ 1 ]}] }
5
+ let(:body) { '' }
6
+ let(:headers) { {} }
7
+ let(:request) { Acfs::Request.new 'url', data: data }
8
+ let(:response) { Acfs::Response.new request, status: 200, headers: headers, body: body }
9
+ let(:decoder) { Acfs::Middleware::MessagePack.new ->(req){ req } }
10
+
11
+ before do
12
+ decoder.call request
13
+ end
14
+
15
+ context 'API compatibility' do
16
+ subject { Acfs::Middleware::MessagePack }
17
+ it { is_expected.to eql Acfs::Middleware::MessagePackDecoder }
18
+ it { is_expected.to eql Acfs::Middleware::MessagePackEncoder }
19
+ end
20
+
21
+ describe 'encode' do
22
+ context 'with not serialized request' do
23
+ it 'should set Content-Type' do
24
+ expect(request.headers['Content-Type']).to eq 'application/x-msgpack'
25
+ end
26
+
27
+ it 'should append Accept header' do
28
+ expect(request.headers['Accept']).to eq 'application/x-msgpack;q=1'
29
+ end
30
+
31
+ context 'with JSON chained' do
32
+ let(:decoder) { Acfs::Middleware::JSON.new super(), q: 0.5 }
33
+
34
+ it 'should append to Accept header' do
35
+ expect(request.headers['Accept']).to eq 'application/json;q=0.5,application/x-msgpack;q=1'
36
+ end
37
+ end
38
+
39
+ it 'should serialize data to MessagePack' do
40
+ expect(MessagePack.unpack(request.body)).to eq data.map(&:stringify_keys)
41
+ end
42
+ end
43
+ end
44
+
45
+ context 'with Message Pack response' do
46
+ let(:headers) { { 'Content-Type' => 'application/x-msgpack' } }
47
+ let(:body) { MessagePack.pack data }
48
+
49
+ it 'should decode body data' do
50
+ request.complete! response
51
+
52
+ expect(response.data).to be == data.map(&:stringify_keys)
53
+ end
54
+ end
55
+
56
+ context 'without Message Pack response' do
57
+ let(:headers) { { 'Content-Type' => 'application/text' } }
58
+ let(:body) { data.to_json }
59
+
60
+ it 'should not decode non-MessagePack encoded responses' do
61
+ request.complete! response
62
+
63
+ expect(response.data).to be_nil
64
+ end
65
+ end
66
+ end
@@ -199,6 +199,18 @@ describe Acfs::Stub do
199
199
  }.to raise_error(::Acfs::InvalidResource)
200
200
  end
201
201
  end
202
+
203
+ context 'with create action' do
204
+ before do
205
+ Acfs::Stub.resource MyUser, :create, with: {name: 'John Smith', age: 0}, raise: 422
206
+ end
207
+
208
+ it 'should allow to raise error' do
209
+ expect {
210
+ user = MyUser.create! name: 'John Smith', age: 0
211
+ }.to raise_error(::Acfs::InvalidResource)
212
+ end
213
+ end
202
214
  end
203
215
 
204
216
  describe '.allow_requests=' do
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.40.1.rc1
4
+ version: 0.41.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: 2014-06-17 00:00:00.000000000 Z
11
+ date: 2014-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -130,12 +130,11 @@ files:
130
130
  - lib/acfs/global.rb
131
131
  - lib/acfs/location.rb
132
132
  - lib/acfs/middleware/base.rb
133
- - lib/acfs/middleware/json_decoder.rb
134
- - lib/acfs/middleware/json_encoder.rb
133
+ - lib/acfs/middleware/json.rb
135
134
  - lib/acfs/middleware/logger.rb
136
- - lib/acfs/middleware/msgpack_decoder.rb
137
- - lib/acfs/middleware/msgpack_encoder.rb
135
+ - lib/acfs/middleware/msgpack.rb
138
136
  - lib/acfs/middleware/print.rb
137
+ - lib/acfs/middleware/serializer.rb
139
138
  - lib/acfs/model.rb
140
139
  - lib/acfs/model/attributes.rb
141
140
  - lib/acfs/model/attributes/base.rb
@@ -176,8 +175,8 @@ files:
176
175
  - spec/acfs/collection_spec.rb
177
176
  - spec/acfs/configuration_spec.rb
178
177
  - spec/acfs/global_spec.rb
179
- - spec/acfs/middleware/json_decoder_spec.rb
180
- - spec/acfs/middleware/msgpack_decoder_spec.rb
178
+ - spec/acfs/middleware/json_spec.rb
179
+ - spec/acfs/middleware/msgpack_spec.rb
181
180
  - spec/acfs/model/attributes/boolean_spec.rb
182
181
  - spec/acfs/model/attributes/date_time_spec.rb
183
182
  - spec/acfs/model/attributes/float_spec.rb
@@ -222,9 +221,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
222
221
  version: '0'
223
222
  required_rubygems_version: !ruby/object:Gem::Requirement
224
223
  requirements:
225
- - - ">"
224
+ - - ">="
226
225
  - !ruby/object:Gem::Version
227
- version: 1.3.1
226
+ version: '0'
228
227
  requirements: []
229
228
  rubyforge_project:
230
229
  rubygems_version: 2.2.2
@@ -236,8 +235,8 @@ test_files:
236
235
  - spec/acfs/collection_spec.rb
237
236
  - spec/acfs/configuration_spec.rb
238
237
  - spec/acfs/global_spec.rb
239
- - spec/acfs/middleware/json_decoder_spec.rb
240
- - spec/acfs/middleware/msgpack_decoder_spec.rb
238
+ - spec/acfs/middleware/json_spec.rb
239
+ - spec/acfs/middleware/msgpack_spec.rb
241
240
  - spec/acfs/model/attributes/boolean_spec.rb
242
241
  - spec/acfs/model/attributes/date_time_spec.rb
243
242
  - spec/acfs/model/attributes/float_spec.rb
@@ -1,16 +0,0 @@
1
- require 'multi_json'
2
-
3
- module Acfs
4
- module Middleware
5
-
6
- # A middleware to automatically decode JSON responses.
7
- #
8
- class JsonDecoder < Base
9
-
10
- def response(response, nxt)
11
- response.data = ::MultiJson.load(response.body) if response.json?
12
- nxt.call response
13
- end
14
- end
15
- end
16
- end
@@ -1,20 +0,0 @@
1
- require 'multi_json'
2
-
3
- module Acfs
4
- module Middleware
5
-
6
- # A middleware to encore request data using JSON.
7
- #
8
- class JsonEncoder < Base
9
-
10
- def call(request)
11
- unless request.method == :get or request.data.nil?
12
- request.body = ::MultiJson.dump(request.data)
13
- request.headers['Content-Type'] = 'application/json'
14
- end
15
-
16
- app.call request
17
- end
18
- end
19
- end
20
- end
@@ -1,26 +0,0 @@
1
- require 'msgpack'
2
- require 'action_dispatch'
3
-
4
- module Acfs
5
- module Middleware
6
-
7
- # Register msgpack mime type
8
- ::Mime::Type.register 'application/x-msgpack', :msgpack
9
-
10
- # A middleware to decode Message Pack responses.
11
- #
12
- class MessagePackDecoder < Base
13
-
14
- CONTENT_TYPES = %w(application/x-msgpack)
15
-
16
- def response(response, nxt)
17
- response.data = ::MessagePack.unpack(response.body) if message_pack?(response)
18
- nxt.call response
19
- end
20
-
21
- def message_pack?(response)
22
- CONTENT_TYPES.include? response.content_type
23
- end
24
- end
25
- end
26
- end
@@ -1,19 +0,0 @@
1
- require 'msgpack'
2
- require 'action_dispatch'
3
-
4
- module Acfs
5
- module Middleware
6
-
7
- # A middleware to encode request data with Message Pack.
8
- #
9
- class MessagePackEncoder < Base
10
-
11
- def call(request)
12
- request.body = ::MessagePack.dump(request.data)
13
- request.headers['Content-Type'] = 'application/x-msgpack'
14
-
15
- app.call request
16
- end
17
- end
18
- end
19
- end
@@ -1,45 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Acfs::Middleware::JsonDecoder do
4
- let(:data) { [{id: 1, name: "Anon"},{id: 2, name:"John", friends: [ 1 ]}] }
5
- let(:body) { data.to_param }
6
- let(:headers) { {} }
7
- let(:request) { Acfs::Request.new "fubar" }
8
- let(:response) { Acfs::Response.new request, status: 200, headers: headers, body: body }
9
- let(:decoder) { Acfs::Middleware::JsonDecoder.new lambda { |req| req } }
10
-
11
- before do
12
- decoder.call request
13
- end
14
-
15
- context 'with JSON response' do
16
- let(:headers) { { 'Content-Type' => 'application/json; charset=utf-8' } }
17
- let(:body) { data.to_json }
18
-
19
- it 'should decode body data' do
20
- request.complete! response
21
-
22
- expect(response.data).to be == data.map(&:stringify_keys)
23
- end
24
- end
25
-
26
- context 'with invalid JSON response' do
27
- let(:headers) { { 'Content-Type' => 'application/json' } }
28
- let(:body) { data.to_json[4..-4] }
29
-
30
- it 'should raise an error' do
31
- expect { request.complete! response }.to raise_error(MultiJson::LoadError)
32
- end
33
- end
34
-
35
- context 'without JSON response' do
36
- let(:headers) { { 'Content-Type' => 'application/text' } }
37
- let(:body) { data.to_json }
38
-
39
- it 'should not decode non-JSON encoded responses' do
40
- request.complete! response
41
-
42
- expect(response.data).to be_nil
43
- end
44
- end
45
- end
@@ -1,36 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Acfs::Middleware::MessagePackDecoder do
4
- let(:data) { [{id: 1, name: "Anon"},{id: 2, name:"John", friends: [ 1 ]}] }
5
- let(:body) { data.to_param }
6
- let(:headers) { {} }
7
- let(:request) { Acfs::Request.new "fubar" }
8
- let(:response) { Acfs::Response.new request, status: 200, headers: headers, body: body }
9
- let(:decoder) { Acfs::Middleware::MessagePackDecoder.new lambda { |req| req } }
10
-
11
- before do
12
- decoder.call request
13
- end
14
-
15
- context 'with Message Pack response' do
16
- let(:headers) { { 'Content-Type' => 'application/x-msgpack' } }
17
- let(:body) { MessagePack.pack data }
18
-
19
- it 'should decode body data' do
20
- request.complete! response
21
-
22
- expect(response.data).to be == data.map(&:stringify_keys)
23
- end
24
- end
25
-
26
- context 'without Message Pack response' do
27
- let(:headers) { { 'Content-Type' => 'application/text' } }
28
- let(:body) { data.to_json }
29
-
30
- it 'should not decode non-MessagePack encoded responses' do
31
- request.complete! response
32
-
33
- expect(response.data).to be_nil
34
- end
35
- end
36
- end