rom-http 0.1.2 → 0.2.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2ce3a61436daffaff61852afe728b5850da68b1b
4
- data.tar.gz: 8bc48f9985acd7b96d464df0d10c8c8976cc27c8
3
+ metadata.gz: 2a5ebdfa66d4abb9975b77abac585c68188d1871
4
+ data.tar.gz: f1544da6a74bc90e6a17124215611e02728dfee4
5
5
  SHA512:
6
- metadata.gz: 66e218d1b3750f3cab5e00fba20113843beca95aa2728a06ea71dad2c5a0a132369a0797ac09f1ecb751ebed66a1674e773f8042fcbbd88537bdec68e49567f1
7
- data.tar.gz: f72e94b554cec39f994d9bf85e6054906eebc8dc28f0d615f43130c764dc592bade46fe3f7cbe8530744ad2243be3b19c8f2afc950dde0a78c5a5db20a0cd870
6
+ metadata.gz: 18d392cdad3557b58474c0b3636a31ab0d49f05706cf09ecf91dbbd7005fe91370c685c60cce4454ec8801c1e9742b31d52d1cc738486d4eb096fcfc9bc6b15c
7
+ data.tar.gz: 93aee72ead5f1bf0244f1930e36502ac2cb4642348b0a39c8d3577ca55fac3336650eb1bb3c5dc094e63ca56fd2f241c6ee5b0a8b6603d06e6fcd643e24ded1b
data/.travis.yml CHANGED
@@ -8,7 +8,7 @@ rvm:
8
8
  - 2.1
9
9
  - 2.2
10
10
  - rbx-2
11
- - jruby
11
+ - jruby-9000
12
12
  - ruby-head
13
13
  - jruby-head
14
14
  env:
data/README.md CHANGED
@@ -42,13 +42,24 @@ end
42
42
  class Users < ROM::Relation[:http]
43
43
  dataset :users
44
44
 
45
+ # You can also define a schema block
46
+ # which will use dry-data's Dry::Data['hash']
47
+ # coercion to pre-process your data
48
+ schema do
49
+ attribute 'id', 'strict.int'
50
+ attribute 'name', 'strict.string'
51
+ attribute 'username', 'strict.string'
52
+ attribute 'email', 'strict.string'
53
+ attribute 'phone', 'strict.string'
54
+ attribute 'website', 'strict.string'
55
+ end
56
+
45
57
  def by_id(id)
46
58
  with_path(id.to_s)
47
59
  end
48
60
  end
49
61
 
50
- rom = ROM::Environment.new
51
- rom.setup(:http, {
62
+ configuration = ROM::Configuration.new(:http, {
52
63
  uri: 'http://jsonplaceholder.typicode.com',
53
64
  headers: {
54
65
  Accept: 'application/json'
@@ -56,9 +67,9 @@ rom.setup(:http, {
56
67
  request_handler: RequestHandler.new,
57
68
  response_handler: ResponseHandler.new
58
69
  })
59
- rom.register_relation(Users)
70
+ configuration.register_relation(Users)
71
+ container = ROM.container(configuration)
60
72
 
61
- container = rom.finalize.env
62
73
  container.relation(:users).by_id(1).to_a
63
74
  # => GET http://jsonplaceholder.typicode.com/users/1 [ Accept: application/json ]
64
75
  ```
@@ -118,8 +129,7 @@ end
118
129
 
119
130
  ROM.register_adapter(:my_adapter, ROM::MyAdapter)
120
131
 
121
- rom = ROM::Environment.new
122
- rom.setup(:my_adapter, {
132
+ configuration = ROM::Configuration.new(:my_adapter, {
123
133
  uri: 'http://jsonplaceholder.typicode.com',
124
134
  headers: {
125
135
  Accept: 'application/json'
@@ -134,9 +144,9 @@ class Users < ROM::Relation[:my_adapter]
134
144
  end
135
145
  end
136
146
 
137
- rom.register_relation(Users)
147
+ configuration.register_relation(Users)
148
+ container = ROM.container(configuration)
138
149
 
139
- container = rom.finalize.env
140
150
  container.relation(:users).by_id(1).to_a
141
151
  # => GET http://jsonplaceholder.typicode.com/users/1 [ Accept: application/json ]
142
152
  ```
@@ -0,0 +1,37 @@
1
+ require 'rom/http/support/transformations'
2
+
3
+ module ROM
4
+ module HTTP
5
+ class Dataset
6
+ module ResponseTransformers
7
+ class Schemad
8
+ attr_reader :schema
9
+
10
+ def initialize(schema)
11
+ @schema = schema
12
+ end
13
+
14
+ def call(response, dataset)
15
+ t(:map_array,
16
+ t(:accept_keys, projections(dataset.projections)) >> ->(tuple) { schema.apply(tuple) }
17
+ ).call(response)
18
+ end
19
+
20
+ private
21
+
22
+ def t(*args)
23
+ ROM::HTTP::Support::Transformations[*args]
24
+ end
25
+
26
+ def projections(projections)
27
+ if projections.empty?
28
+ schema.attribute_names
29
+ else
30
+ projections.reject { |attr| !schema.attribute_names.include?(attr) }
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,25 @@
1
+ require 'rom/http/support/transformations'
2
+
3
+ module ROM
4
+ module HTTP
5
+ class Dataset
6
+ module ResponseTransformers
7
+ class Schemaless
8
+ def call(response, dataset)
9
+ if dataset.projections.empty?
10
+ response
11
+ else
12
+ t(:map_array, t(:accept_keys, dataset.projections)).call(response)
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def t(*args)
19
+ ROM::HTTP::Support::Transformations[*args]
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,10 +1,11 @@
1
- require 'rom/http/support/transformations'
1
+ require 'rom/http/dataset/response_transformers/schemad'
2
+ require 'rom/http/dataset/response_transformers/schemaless'
2
3
 
3
4
  module ROM
4
5
  module HTTP
5
6
  class Dataset
6
7
  include Enumerable
7
- include Equalizer.new(:config, :options)
8
+ include Dry::Equalizer(:config, :options)
8
9
  include ROM::Options
9
10
 
10
11
  attr_reader :config
@@ -29,9 +30,15 @@ module ROM
29
30
 
30
31
  def initialize(config, options = {})
31
32
  @config = config
33
+ @response_transformer = ResponseTransformers::Schemaless.new
32
34
  super(options)
33
35
  end
34
36
 
37
+ def response_transformer(transformer = Undefined)
38
+ return @response_transformer if Undefined === transformer
39
+ @response_transformer = transformer
40
+ end
41
+
35
42
  def uri
36
43
  config.fetch(:uri) { fail Error, ':uri configuration missing' }
37
44
  end
@@ -115,38 +122,23 @@ module ROM
115
122
 
116
123
  def response
117
124
  response_transformer.call(
118
- response_handler.call(request_handler.call(self), self)
125
+ response_handler.call(request_handler.call(self), self),
126
+ self
119
127
  )
120
128
  end
121
129
 
122
130
  private
123
131
 
124
132
  def response_handler
125
- config.fetch(:response_handler, default_response_handler).tap do |response_handler|
126
- fail Error, ':response_handler configuration missing' if response_handler.nil?
127
- end
133
+ response_handler = config.fetch(:response_handler, self.class.default_response_handler)
134
+ fail Error, ':response_handler configuration missing' if response_handler.nil?
135
+ response_handler
128
136
  end
129
137
 
130
138
  def request_handler
131
- config.fetch(:request_handler, default_request_handler).tap do |request_handler|
132
- fail Error, ':response_handler configuration missing' if request_handler.nil?
133
- end
134
- end
135
-
136
- def default_response_handler
137
- self.class.default_response_handler
138
- end
139
-
140
- def default_request_handler
141
- self.class.default_request_handler
142
- end
143
-
144
- def response_transformer
145
- if projections.empty?
146
- ROM::HTTP::Support::Transformations[:noop]
147
- else
148
- ROM::HTTP::Support::Transformations[:project, projections]
149
- end
139
+ request_handler = config.fetch(:request_handler, self.class.default_request_handler)
140
+ fail Error, ':response_handler configuration missing' if request_handler.nil?
141
+ request_handler
150
142
  end
151
143
 
152
144
  def __new__(*args, &block)
@@ -1,13 +1,23 @@
1
+ require 'rom/plugins/relation/schema'
2
+
1
3
  module ROM
2
4
  module HTTP
3
5
  class Relation < ROM::Relation
4
6
  include Enumerable
5
7
 
6
8
  adapter :http
9
+ use :schema
7
10
 
8
11
  forward :with_request_method, :with_path, :append_path, :with_options,
9
12
  :with_params, :clear_params, :project
10
13
 
14
+ def initialize(*)
15
+ super
16
+ dataset.response_transformer(
17
+ Dataset::ResponseTransformers::Schemad.new(self.class.schema)
18
+ ) if self.class.schema
19
+ end
20
+
11
21
  def insert(*args)
12
22
  dataset.insert(*args)
13
23
  end
@@ -4,13 +4,8 @@ module ROM
4
4
  class Transformations
5
5
  extend Transproc::Registry
6
6
 
7
- uses :map_array, from: ::Transproc::ArrayTransformations
8
- uses :accept_keys, from: ::Transproc::HashTransformations
9
- import :identity, from: ::Transproc::Coercions, as: :noop
10
-
11
- def self.project(value, projections)
12
- t(:map_array, t(:accept_keys, projections)).call(value)
13
- end
7
+ import :map_array, from: ::Transproc::ArrayTransformations
8
+ import :accept_keys, from: ::Transproc::HashTransformations
14
9
  end
15
10
  end
16
11
  end
@@ -1,5 +1,5 @@
1
1
  module ROM
2
2
  module HTTP
3
- VERSION = '0.1.2'.freeze
3
+ VERSION = '0.2.0.beta1'.freeze
4
4
  end
5
5
  end
@@ -0,0 +1,53 @@
1
+ require 'dry/data'
2
+
3
+ module ROM
4
+ module Plugins
5
+ module Relation
6
+ module Schema
7
+ def self.included(klass)
8
+ super
9
+
10
+ klass.class_eval do
11
+ def self.schema(&block)
12
+ @__schema__ = Schema.create(&block) if block_given?
13
+ @__schema__
14
+ end
15
+ end
16
+ end
17
+
18
+ class Schema
19
+ attr_reader :schema
20
+ attr_reader :coercer
21
+
22
+ def self.create(&block)
23
+ new.tap { |schema| schema.instance_eval(&block) }
24
+ end
25
+
26
+ def initialize(schema = {}, coercer = Dry::Data['hash'])
27
+ @schema = schema
28
+ @coercer = coercer
29
+ end
30
+
31
+ def attribute_names
32
+ schema.keys
33
+ end
34
+
35
+ def apply(attributes = {})
36
+ coercer.schema(schema).call(attributes)
37
+ end
38
+
39
+ private
40
+
41
+ def attribute(name, type)
42
+ schema[name] = type
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+
51
+ ROM.plugins do
52
+ register :schema, ROM::Plugins::Relation::Schema, type: :relation
53
+ end
data/rom-http.gemspec CHANGED
@@ -18,9 +18,10 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ['lib']
20
20
 
21
- spec.add_runtime_dependency 'rom', '~> 0.9', '>= 0.9.0'
22
- spec.add_runtime_dependency 'equalizer', '~> 0.0', '>= 0.0.9'
23
21
  spec.add_runtime_dependency 'thread_safe'
22
+ spec.add_runtime_dependency 'dry-data'
23
+ spec.add_runtime_dependency 'rom', '~> 1.0.0.beta2'
24
+ spec.add_runtime_dependency 'dry-equalizer'
24
25
 
25
26
  spec.add_development_dependency 'bundler'
26
27
  spec.add_development_dependency 'rspec', '~> 3.1'
@@ -1,26 +1,11 @@
1
1
  RSpec.describe ROM::HTTP::Commands::Create do
2
- let(:uri) { 'http://localhost:3000' }
3
- let(:headers) { { accept: 'application/json' } }
4
- let(:rom) { ROM::Environment.new }
5
- let(:container) { rom.finalize.env }
6
- let(:request_handler) { double(Proc, freeze: self) }
7
- let(:response_handler) { double(Proc, freeze: self) }
2
+ include_context 'setup'
8
3
  let(:relation) do
9
4
  Class.new(ROM::HTTP::Relation) do
10
5
  dataset :users
11
6
  end
12
7
  end
13
8
 
14
- before do
15
- rom.setup(
16
- :http,
17
- uri: uri,
18
- headers: headers,
19
- request_handler: request_handler,
20
- response_handler: response_handler
21
- )
22
- end
23
-
24
9
  context 'with single tuple' do
25
10
  let(:response) { double }
26
11
  let(:tuple) { double }
@@ -47,8 +32,8 @@ RSpec.describe ROM::HTTP::Commands::Create do
47
32
  end
48
33
 
49
34
  before do
50
- rom.register_relation(relation)
51
- rom.register_command(command)
35
+ configuration.register_relation(relation)
36
+ configuration.register_command(command)
52
37
 
53
38
  allow(request_handler).to receive(:call).and_return(response)
54
39
  allow(response_handler).to receive(:call).and_return(tuple)
@@ -106,8 +91,8 @@ RSpec.describe ROM::HTTP::Commands::Create do
106
91
  end
107
92
 
108
93
  before do
109
- rom.register_relation(relation)
110
- rom.register_command(command)
94
+ configuration.register_relation(relation)
95
+ configuration.register_command(command)
111
96
 
112
97
  allow(request_handler).to receive(:call).and_return(response_1, response_2)
113
98
  allow(response_handler).to receive(:call).and_return(tuple_1, tuple_2)
@@ -1,10 +1,5 @@
1
1
  RSpec.describe ROM::HTTP::Commands::Delete do
2
- let(:uri) { 'http://localhost:3000' }
3
- let(:headers) { { accept: 'application/json' } }
4
- let(:rom) { ROM::Environment.new }
5
- let(:container) { rom.finalize.env }
6
- let(:request_handler) { double(Proc, freeze: self) }
7
- let(:response_handler) { double(Proc, freeze: self) }
2
+ include_context 'setup'
8
3
  let(:relation) do
9
4
  Class.new(ROM::HTTP::Relation) do
10
5
  dataset :users
@@ -34,18 +29,8 @@ RSpec.describe ROM::HTTP::Commands::Delete do
34
29
  end
35
30
 
36
31
  before do
37
- rom.setup(
38
- :http,
39
- uri: uri,
40
- headers: headers,
41
- request_handler: request_handler,
42
- response_handler: response_handler
43
- )
44
- end
45
-
46
- before do
47
- rom.register_relation(relation)
48
- rom.register_command(command)
32
+ configuration.register_relation(relation)
33
+ configuration.register_command(command)
49
34
 
50
35
  allow(request_handler).to receive(:call).and_return(response)
51
36
  allow(response_handler).to receive(:call).and_return(tuples)
@@ -1,26 +1,11 @@
1
1
  RSpec.describe ROM::HTTP::Commands::Update do
2
- let(:uri) { 'http://localhost:3000' }
3
- let(:headers) { { accept: 'application/json' } }
4
- let(:rom) { ROM::Environment.new }
5
- let(:container) { rom.finalize.env }
6
- let(:request_handler) { double(Proc, freeze: self) }
7
- let(:response_handler) { double(Proc, freeze: self) }
2
+ include_context 'setup'
8
3
  let(:relation) do
9
4
  Class.new(ROM::HTTP::Relation) do
10
5
  dataset :users
11
6
  end
12
7
  end
13
8
 
14
- before do
15
- rom.setup(
16
- :http,
17
- uri: uri,
18
- headers: headers,
19
- request_handler: request_handler,
20
- response_handler: response_handler
21
- )
22
- end
23
-
24
9
  context 'with single tuple' do
25
10
  let(:response) { double }
26
11
  let(:tuple) { double }
@@ -47,8 +32,8 @@ RSpec.describe ROM::HTTP::Commands::Update do
47
32
  end
48
33
 
49
34
  before do
50
- rom.register_relation(relation)
51
- rom.register_command(command)
35
+ configuration.register_relation(relation)
36
+ configuration.register_command(command)
52
37
 
53
38
  allow(request_handler).to receive(:call).and_return(response)
54
39
  allow(response_handler).to receive(:call).and_return(tuple)
@@ -106,8 +91,8 @@ RSpec.describe ROM::HTTP::Commands::Update do
106
91
  end
107
92
 
108
93
  before do
109
- rom.register_relation(relation)
110
- rom.register_command(command)
94
+ configuration.register_relation(relation)
95
+ configuration.register_command(command)
111
96
 
112
97
  allow(request_handler).to receive(:call).and_return(response_1, response_2)
113
98
  allow(response_handler).to receive(:call).and_return(tuple_1, tuple_2)
@@ -1,10 +1,5 @@
1
1
  RSpec.describe ROM::HTTP::Relation do
2
- let(:uri) { 'http://localhost:3000' }
3
- let(:headers) { { accept: 'application/json' } }
4
- let(:rom) { ROM::Environment.new }
5
- let(:container) { rom.finalize.env }
6
- let(:request_handler) { double(Proc, freeze: self) }
7
- let(:response_handler) { double(Proc, freeze: self) }
2
+ include_context 'setup'
8
3
  let(:relation) do
9
4
  Class.new(ROM::HTTP::Relation) do
10
5
  dataset :users
@@ -38,17 +33,7 @@ RSpec.describe ROM::HTTP::Relation do
38
33
  let(:params) { { filters: { first_name: 'John' } } }
39
34
 
40
35
  before do
41
- rom.setup(
42
- :http,
43
- uri: uri,
44
- headers: headers,
45
- request_handler: request_handler,
46
- response_handler: response_handler
47
- )
48
- end
49
-
50
- before do
51
- rom.register_relation(relation)
36
+ configuration.register_relation(relation)
52
37
 
53
38
  allow(request_handler).to receive(:call).and_return(response)
54
39
  allow(response_handler).to receive(:call).and_return(tuples)
data/spec/shared/setup.rb CHANGED
@@ -1,14 +1,18 @@
1
1
  RSpec.shared_context 'setup' do
2
- let(:env) { ROM::Environment.new }
3
- let(:setup) do
4
- env.setup(
2
+ let(:configuration) do
3
+ ROM::Configuration.new(
5
4
  :http,
6
- uri: 'http://localhost:3000',
5
+ uri: uri,
7
6
  request_handler: request_handler,
8
- response_handler: response_handler
7
+ response_handler: response_handler,
8
+ headers: headers
9
9
  )
10
10
  end
11
- let(:rom) { setup.finalize }
11
+ let(:container) { ROM.container(configuration) }
12
+ let(:rom) { container }
13
+ let(:gateway) { container.gateways.fetch(:default) }
14
+ let(:uri) { 'http://localhost:3000' }
12
15
  let(:request_handler) { double(Proc, freeze: self) }
13
16
  let(:response_handler) { double(Proc, freeze: self) }
17
+ let(:headers) { { accept: 'application/json' } }
14
18
  end
@@ -1,10 +1,26 @@
1
1
  RSpec.shared_context 'users and tasks' do
2
2
  include_context 'setup'
3
+ let(:users_relation) do
4
+ Class.new(ROM::HTTP::Relation) do
5
+ dataset :users
3
6
 
4
- before do
5
- gateway = setup.default
7
+ def by_id(id)
8
+ with_params(id: id)
9
+ end
10
+ end
11
+ end
12
+ let(:tasks_relation) do
13
+ Class.new(ROM::HTTP::Relation) do
14
+ dataset :tasks
6
15
 
7
- gateway.dataset(:users)
8
- gateway.dataset(:tasks)
16
+ def by_id(id)
17
+ with_params(id: id)
18
+ end
19
+ end
20
+ end
21
+
22
+ before do
23
+ configuration.register_relation(users_relation)
24
+ configuration.register_relation(tasks_relation)
9
25
  end
10
26
  end
@@ -0,0 +1,52 @@
1
+ RSpec.describe ROM::HTTP::Dataset::ResponseTransformers::Schemad do
2
+ let(:transformer) { ROM::HTTP::Dataset::ResponseTransformers::Schemad.new(schema) }
3
+ let(:schema) do
4
+ ROM::Plugins::Relation::Schema::Schema.create do
5
+ attribute :id, 'form.int'
6
+ attribute :name, 'strict.string'
7
+ attribute :active, 'form.bool'
8
+ end
9
+ end
10
+
11
+ describe '#call' do
12
+ let(:response) do
13
+ [
14
+ {
15
+ id: '1',
16
+ name: 'Jill',
17
+ email: 'jill@fakemail.com',
18
+ active: 'true'
19
+ }
20
+ ]
21
+ end
22
+ let(:dataset) do
23
+ double('ROM::HTTP::Dataset', projections: projections)
24
+ end
25
+
26
+ subject! { transformer.call(response, dataset) }
27
+
28
+ context 'with no projections' do
29
+ let(:projections) { [] }
30
+
31
+ it do
32
+ is_expected.to eq([
33
+ id: 1,
34
+ name: 'Jill',
35
+ active: true
36
+ ])
37
+ end
38
+ end
39
+
40
+ context 'with projections' do
41
+ let(:projections) { [:id, :name, :active] }
42
+
43
+ it do
44
+ is_expected.to eq([
45
+ id: 1,
46
+ name: 'Jill',
47
+ active: true
48
+ ])
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,39 @@
1
+ RSpec.describe ROM::HTTP::Dataset::ResponseTransformers::Schemaless do
2
+ let(:transformer) { ROM::HTTP::Dataset::ResponseTransformers::Schemaless.new }
3
+
4
+ describe '#call' do
5
+ let(:response) do
6
+ [
7
+ {
8
+ id: 1,
9
+ name: 'Jill',
10
+ email: 'jill@fakemail.com',
11
+ active: true
12
+ }
13
+ ]
14
+ end
15
+ let(:dataset) do
16
+ double('ROM::HTTP::Dataset', projections: projections)
17
+ end
18
+
19
+ subject! { transformer.call(response, dataset) }
20
+
21
+ context 'with no projections' do
22
+ let(:projections) { [] }
23
+
24
+ it { is_expected.to eq(response) }
25
+ end
26
+
27
+ context 'with projections' do
28
+ let(:projections) { [:id, :name, :active] }
29
+
30
+ it do
31
+ is_expected.to eq([
32
+ id: 1,
33
+ name: 'Jill',
34
+ active: true
35
+ ])
36
+ end
37
+ end
38
+ end
39
+ end
@@ -110,6 +110,40 @@ RSpec.describe ROM::HTTP::Dataset do
110
110
  end
111
111
  end
112
112
 
113
+ describe '#response_transformer' do
114
+ context 'with argument' do
115
+ let(:transformer) { double('ResponseTransformer') }
116
+
117
+ subject! { dataset.response_transformer(transformer) }
118
+
119
+ it do
120
+ expect(dataset.instance_variable_get(:@response_transformer))
121
+ .to eq(transformer)
122
+ end
123
+ it { is_expected.to eq(transformer) }
124
+ end
125
+
126
+ context 'without argument' do
127
+ context 'when transformer not set' do
128
+ subject! { dataset.response_transformer }
129
+
130
+ it { is_expected.to be_a(ROM::HTTP::Dataset::ResponseTransformers::Schemaless) }
131
+ end
132
+
133
+ context 'when transformer set' do
134
+ let(:transformer) { double('ResponseTransformer') }
135
+
136
+ before do
137
+ dataset.response_transformer(transformer)
138
+ end
139
+
140
+ subject! { dataset.response_transformer }
141
+
142
+ it { is_expected.to eq(transformer) }
143
+ end
144
+ end
145
+ end
146
+
113
147
  describe '#uri' do
114
148
  context 'when no uri configured' do
115
149
  let(:config) { {} }
@@ -3,8 +3,6 @@ require 'rom/lint/spec'
3
3
  RSpec.describe ROM::HTTP::Gateway do
4
4
  include_context 'users and tasks'
5
5
 
6
- let(:gateway) { rom.gateways[:default] }
7
-
8
6
  it_behaves_like 'a rom gateway' do
9
7
  let(:identifier) { :http }
10
8
  let(:gateway) { ROM::HTTP::Gateway }
@@ -0,0 +1,32 @@
1
+ RSpec.describe ROM::HTTP::Relation do
2
+ describe '#initialize' do
3
+ let(:relation) { relation_klass.new(dataset) }
4
+ let(:dataset) { ROM::HTTP::Dataset.new(nil, {}) }
5
+
6
+ context 'when relation has schema' do
7
+ let(:relation_klass) do
8
+ Class.new(ROM::HTTP::Relation) do
9
+ schema do
10
+ attribute 'id', 'strict.int'
11
+ end
12
+ end
13
+ end
14
+
15
+ it 'sets the dataset response transformer' do
16
+ expect(relation.dataset.response_transformer)
17
+ .to be_a(ROM::HTTP::Dataset::ResponseTransformers::Schemad)
18
+ end
19
+ end
20
+
21
+ context 'when relation does not have schema' do
22
+ let(:relation_klass) do
23
+ Class.new(ROM::HTTP::Relation)
24
+ end
25
+
26
+ it 'keeps the default (schemaless) transformer' do
27
+ expect(relation.dataset.response_transformer)
28
+ .to be_a(ROM::HTTP::Dataset::ResponseTransformers::Schemaless)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,30 @@
1
+ RSpec.describe ROM::Plugins::Relation::Schema::Schema do
2
+ let(:klass) { ROM::Plugins::Relation::Schema::Schema }
3
+ let(:schema) do
4
+ klass.create do
5
+ attribute :id, 'form.int'
6
+ attribute :name, 'strict.string'
7
+ attribute :active, 'form.bool'
8
+ end
9
+ end
10
+
11
+ describe '#attribute_names' do
12
+ subject! { schema.attribute_names }
13
+
14
+ it { is_expected.to match_array([:id, :name, :active])}
15
+ end
16
+
17
+ describe '#apply' do
18
+ let(:attributes) do
19
+ {
20
+ id: '1',
21
+ name: 'John',
22
+ active: 'true'
23
+ }
24
+ end
25
+
26
+ subject! { schema.apply(attributes) }
27
+
28
+ it { is_expected.to eq(id: 1, name: 'John', active: true) }
29
+ end
30
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rom-http
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Solnica
@@ -9,50 +9,52 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-09-16 00:00:00.000000000 Z
12
+ date: 2015-12-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: rom
15
+ name: thread_safe
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - "~>"
19
- - !ruby/object:Gem::Version
20
- version: '0.9'
21
18
  - - ">="
22
19
  - !ruby/object:Gem::Version
23
- version: 0.9.0
20
+ version: '0'
24
21
  type: :runtime
25
22
  prerelease: false
26
23
  version_requirements: !ruby/object:Gem::Requirement
27
24
  requirements:
28
- - - "~>"
29
- - !ruby/object:Gem::Version
30
- version: '0.9'
31
25
  - - ">="
32
26
  - !ruby/object:Gem::Version
33
- version: 0.9.0
27
+ version: '0'
34
28
  - !ruby/object:Gem::Dependency
35
- name: equalizer
29
+ name: dry-data
36
30
  requirement: !ruby/object:Gem::Requirement
37
31
  requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '0.0'
41
32
  - - ">="
42
33
  - !ruby/object:Gem::Version
43
- version: 0.0.9
34
+ version: '0'
44
35
  type: :runtime
45
36
  prerelease: false
46
37
  version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rom
44
+ requirement: !ruby/object:Gem::Requirement
47
45
  requirements:
48
46
  - - "~>"
49
47
  - !ruby/object:Gem::Version
50
- version: '0.0'
51
- - - ">="
48
+ version: 1.0.0.beta2
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
52
54
  - !ruby/object:Gem::Version
53
- version: 0.0.9
55
+ version: 1.0.0.beta2
54
56
  - !ruby/object:Gem::Dependency
55
- name: thread_safe
57
+ name: dry-equalizer
56
58
  requirement: !ruby/object:Gem::Requirement
57
59
  requirements:
58
60
  - - ">="
@@ -132,27 +134,30 @@ files:
132
134
  - lib/rom/http/commands/delete.rb
133
135
  - lib/rom/http/commands/update.rb
134
136
  - lib/rom/http/dataset.rb
137
+ - lib/rom/http/dataset/response_transformers/schemad.rb
138
+ - lib/rom/http/dataset/response_transformers/schemaless.rb
135
139
  - lib/rom/http/error.rb
136
140
  - lib/rom/http/gateway.rb
137
141
  - lib/rom/http/relation.rb
138
142
  - lib/rom/http/support/transformations.rb
139
143
  - lib/rom/http/version.rb
144
+ - lib/rom/plugins/relation/schema.rb
140
145
  - rakelib/rubocop.rake
141
146
  - rom-http.gemspec
142
147
  - spec/integration/abstract/commands/create_spec.rb
143
148
  - spec/integration/abstract/commands/delete_spec.rb
144
149
  - spec/integration/abstract/commands/update_spec.rb
145
150
  - spec/integration/abstract/relation_spec.rb
146
- - spec/shared/command_behaviour.rb
147
151
  - spec/shared/setup.rb
148
152
  - spec/shared/users_and_tasks.rb
149
153
  - spec/spec_helper.rb
150
154
  - spec/support/mutant.rb
151
- - spec/unit/rom/http/commands/create_spec.rb
152
- - spec/unit/rom/http/commands/delete_spec.rb
153
- - spec/unit/rom/http/commands/update_spec.rb
155
+ - spec/unit/rom/http/dataset/response_transformers/schemad_spec.rb
156
+ - spec/unit/rom/http/dataset/response_transformers/schemaless_spec.rb
154
157
  - spec/unit/rom/http/dataset_spec.rb
155
158
  - spec/unit/rom/http/gateway_spec.rb
159
+ - spec/unit/rom/http/relation_spec.rb
160
+ - spec/unit/rom/plugins/relation/schema/schema_spec.rb
156
161
  homepage: http://rom-rb.org
157
162
  licenses:
158
163
  - MIT
@@ -168,9 +173,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
168
173
  version: '0'
169
174
  required_rubygems_version: !ruby/object:Gem::Requirement
170
175
  requirements:
171
- - - ">="
176
+ - - ">"
172
177
  - !ruby/object:Gem::Version
173
- version: '0'
178
+ version: 1.3.1
174
179
  requirements: []
175
180
  rubyforge_project:
176
181
  rubygems_version: 2.4.5.1
@@ -182,13 +187,13 @@ test_files:
182
187
  - spec/integration/abstract/commands/delete_spec.rb
183
188
  - spec/integration/abstract/commands/update_spec.rb
184
189
  - spec/integration/abstract/relation_spec.rb
185
- - spec/shared/command_behaviour.rb
186
190
  - spec/shared/setup.rb
187
191
  - spec/shared/users_and_tasks.rb
188
192
  - spec/spec_helper.rb
189
193
  - spec/support/mutant.rb
190
- - spec/unit/rom/http/commands/create_spec.rb
191
- - spec/unit/rom/http/commands/delete_spec.rb
192
- - spec/unit/rom/http/commands/update_spec.rb
194
+ - spec/unit/rom/http/dataset/response_transformers/schemad_spec.rb
195
+ - spec/unit/rom/http/dataset/response_transformers/schemaless_spec.rb
193
196
  - spec/unit/rom/http/dataset_spec.rb
194
197
  - spec/unit/rom/http/gateway_spec.rb
198
+ - spec/unit/rom/http/relation_spec.rb
199
+ - spec/unit/rom/plugins/relation/schema/schema_spec.rb
@@ -1,18 +0,0 @@
1
- shared_examples_for 'a command' do
2
- describe '#method_missing' do
3
- it 'forwards to relation and wraps response if it returned another relation' do
4
- new_command = command.with_params({})
5
-
6
- expect(new_command).to be_instance_of(command.class)
7
- expect(new_command.relation).to eq(command.with_params({}).relation)
8
- end
9
-
10
- it 'returns original response if it was not a relation' do
11
- expect(command.name).to eq(command.relation.name)
12
- end
13
-
14
- it 'raises error when message is not known' do
15
- expect { command.not_here }.to raise_error(NoMethodError, /not_here/)
16
- end
17
- end
18
- end
@@ -1,22 +0,0 @@
1
- describe ROM::HTTP::Commands::Create do
2
- include_context 'users and tasks'
3
-
4
- subject(:command) { ROM::HTTP::Commands::Create.build(users) }
5
-
6
- let(:users_relation_klass) do
7
- Class.new(ROM::HTTP::Relation) do
8
- dataset :users
9
-
10
- def by_id(id)
11
- with_params(id: id)
12
- end
13
- end
14
- end
15
- let(:users) { rom.relations[:users] }
16
-
17
- before do
18
- setup.register_relation(users_relation_klass)
19
- end
20
-
21
- it_behaves_like 'a command'
22
- end
@@ -1,22 +0,0 @@
1
- describe ROM::HTTP::Commands::Delete do
2
- include_context 'users and tasks'
3
-
4
- subject(:command) { ROM::HTTP::Commands::Delete.build(users) }
5
-
6
- let(:users_relation_klass) do
7
- Class.new(ROM::HTTP::Relation) do
8
- dataset :users
9
-
10
- def by_id(id)
11
- with_params(id: id)
12
- end
13
- end
14
- end
15
- let(:users) { rom.relations[:users] }
16
-
17
- before do
18
- setup.register_relation(users_relation_klass)
19
- end
20
-
21
- it_behaves_like 'a command'
22
- end
@@ -1,22 +0,0 @@
1
- describe ROM::HTTP::Commands::Update do
2
- include_context 'users and tasks'
3
-
4
- subject(:command) { ROM::HTTP::Commands::Update.build(users) }
5
-
6
- let(:users_relation_klass) do
7
- Class.new(ROM::HTTP::Relation) do
8
- dataset :users
9
-
10
- def by_id(id)
11
- with_params(id: id)
12
- end
13
- end
14
- end
15
- let(:users) { rom.relations[:users] }
16
-
17
- before do
18
- setup.register_relation(users_relation_klass)
19
- end
20
-
21
- it_behaves_like 'a command'
22
- end