rom-http 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +26 -2
  3. data/LICENSE.txt +1 -1
  4. data/README.md +12 -15
  5. data/lib/rom-http.rb +2 -0
  6. data/lib/rom/http.rb +2 -0
  7. data/lib/rom/http/attribute.rb +10 -0
  8. data/lib/rom/http/commands.rb +2 -0
  9. data/lib/rom/http/commands/create.rb +2 -0
  10. data/lib/rom/http/commands/delete.rb +2 -0
  11. data/lib/rom/http/commands/update.rb +2 -0
  12. data/lib/rom/http/dataset.rb +152 -101
  13. data/lib/rom/http/error.rb +2 -0
  14. data/lib/rom/http/gateway.rb +44 -3
  15. data/lib/rom/http/handlers.rb +14 -0
  16. data/lib/rom/http/handlers/json.rb +65 -0
  17. data/lib/rom/http/mapper_compiler.rb +11 -0
  18. data/lib/rom/http/relation.rb +19 -64
  19. data/lib/rom/http/schema.rb +20 -0
  20. data/lib/rom/http/schema/dsl.rb +12 -0
  21. data/lib/rom/http/transformer.rb +2 -0
  22. data/lib/rom/http/types.rb +13 -0
  23. data/lib/rom/http/version.rb +3 -1
  24. metadata +32 -59
  25. data/.gitignore +0 -16
  26. data/.rspec +0 -3
  27. data/.rubocop.yml +0 -22
  28. data/.rubocop_todo.yml +0 -12
  29. data/.travis.yml +0 -20
  30. data/Gemfile +0 -24
  31. data/Rakefile +0 -24
  32. data/examples/repository_with_combine.rb +0 -154
  33. data/lib/rom/http/dataset/class_interface.rb +0 -33
  34. data/rakelib/rubocop.rake +0 -18
  35. data/rom-http.gemspec +0 -32
  36. data/spec/integration/abstract/commands/create_spec.rb +0 -119
  37. data/spec/integration/abstract/commands/delete_spec.rb +0 -52
  38. data/spec/integration/abstract/commands/update_spec.rb +0 -119
  39. data/spec/integration/abstract/relation_spec.rb +0 -78
  40. data/spec/shared/setup.rb +0 -18
  41. data/spec/shared/users_and_tasks.rb +0 -30
  42. data/spec/spec_helper.rb +0 -19
  43. data/spec/support/mutant.rb +0 -10
  44. data/spec/unit/rom/http/dataset_spec.rb +0 -824
  45. data/spec/unit/rom/http/gateway_spec.rb +0 -69
  46. data/spec/unit/rom/http/relation_spec.rb +0 -268
data/.gitignore DELETED
@@ -1,16 +0,0 @@
1
- /.bundle
2
- /.yardoc
3
- /_yardoc
4
- /coverage
5
- /vendor
6
- /doc
7
- /pkg
8
- /spec/reports
9
- /tmp
10
- Gemfile.lock
11
- *.bundle
12
- *.so
13
- *.o
14
- *.a
15
- mkmf.log
16
- log/*.log
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --require=spec_helper
2
- --color
3
- --order random
@@ -1,22 +0,0 @@
1
- # Generated by `rubocop --auto-gen-config`
2
- inherit_from: .rubocop_todo.yml
3
-
4
- Metrics/LineLength:
5
- Max: 100
6
-
7
- Style/CaseEquality:
8
- Enabled: false
9
-
10
- # No need to handle LoadError in Rakefile
11
- Lint/HandleExceptions:
12
- Exclude:
13
- - Rakefile
14
- - rakelib/rubocop.rake
15
-
16
- # Documentation checked by Inch CI
17
- Style/Documentation:
18
- Enabled: false
19
-
20
- # Allow rom-http
21
- Style/FileName:
22
- Enabled: false
@@ -1,12 +0,0 @@
1
- # This configuration was generated by
2
- # `rubocop --auto-gen-config`
3
- # on 2015-09-16 21:35:53 +0100 using RuboCop version 0.34.1.
4
- # The point is for the user to remove these configuration records
5
- # one by one as the offenses are removed from the code base.
6
- # Note that changes in the inspected code, or installation of new
7
- # versions of RuboCop, may require this file to be generated again.
8
-
9
- # Offense count: 1
10
- # Configuration parameters: CountComments.
11
- Metrics/ClassLength:
12
- Max: 118
@@ -1,20 +0,0 @@
1
- language: ruby
2
- dist: trusty
3
- sudo: required
4
- cache: bundler
5
- bundler_args: --without sql benchmarks console tools
6
- script: "bundle exec rake ci"
7
- rvm:
8
- - 2.3.4
9
- - 2.4.1
10
- - jruby-9.1.13.0
11
- env:
12
- global:
13
- - JRUBY_OPTS='--dev -J-Xmx1024M'
14
- notifications:
15
- webhooks:
16
- urls:
17
- - https://webhooks.gitter.im/e/39e1225f489f38b0bd09
18
- on_success: change
19
- on_failure: always
20
- on_start: false
data/Gemfile DELETED
@@ -1,24 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec
4
-
5
- gem 'rom-repository', '~> 2.0'
6
-
7
- group :test do
8
- gem 'rom', '~> 4.0'
9
- gem 'faraday'
10
- gem 'inflecto'
11
- end
12
-
13
- group :tools do
14
- gem 'byebug'
15
- gem 'guard'
16
- gem 'guard-rspec'
17
- gem 'guard-rubocop'
18
- gem 'rubocop', '~> 0.28'
19
-
20
- platform :mri do
21
- gem 'mutant', '>= 0.8.0', github: 'mbj/mutant', branch: 'master'
22
- gem 'mutant-rspec'
23
- end
24
- end
data/Rakefile DELETED
@@ -1,24 +0,0 @@
1
- #!/usr/bin/env rake
2
- require 'bundler/gem_tasks'
3
-
4
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
5
-
6
- require 'rspec/core'
7
- require 'rspec/core/rake_task'
8
-
9
- RSpec::Core::RakeTask.new(:spec)
10
- task default: [:ci]
11
-
12
- desc 'Run CI tasks'
13
- task ci: [:spec]
14
-
15
- begin
16
- require 'rubocop/rake_task'
17
-
18
- Rake::Task[:default].enhance [:rubocop]
19
-
20
- RuboCop::RakeTask.new do |task|
21
- task.options << '--display-cop-names'
22
- end
23
- rescue LoadError
24
- end
@@ -1,154 +0,0 @@
1
- require 'inflecto'
2
- require 'json'
3
- require 'uri'
4
- require 'net/http'
5
- require 'rom-repository'
6
-
7
- class RequestHandler
8
- def call(dataset)
9
- uri = URI(dataset.uri)
10
- uri.path = dataset.absolute_path
11
- uri.query = URI.encode_www_form(dataset.params)
12
-
13
- http = Net::HTTP.new(uri.host, uri.port)
14
- request_klass = Net::HTTP.const_get(Inflecto.classify(dataset.request_method))
15
-
16
- request = request_klass.new(uri.request_uri)
17
- dataset.headers.each_with_object(request) do |(header, value), request|
18
- request[header.to_s] = value
19
- end
20
-
21
- response = http.request(request)
22
- end
23
- end
24
-
25
- class ResponseHandler
26
- def call(response, dataset)
27
- if %i(post put patch).include?(dataset.request_method)
28
- JSON.parse(response.body, symbolize_names: true)
29
- else
30
- Array([JSON.parse(response.body, symbolize_names: true)]).flatten
31
- end
32
- end
33
- end
34
-
35
- class Users < ROM::Relation[:http]
36
- schema(:users) do
37
- attribute :id, ROM::Types::Int.meta(primary_key: true)
38
- attribute :name, ROM::Types::String
39
- attribute :username, ROM::Types::String
40
- attribute :email, ROM::Types::String
41
- attribute :phone, ROM::Types::String
42
- attribute :website, ROM::Types::String
43
- end
44
-
45
- def by_id(id)
46
- with_path(id.to_s)
47
- end
48
- end
49
-
50
- class Posts < ROM::Relation[:http]
51
- schema(:posts) do
52
- attribute :id, ROM::Types::Int.meta(primary_key: true)
53
- attribute :userId, ROM::Types::Int.meta(alias: :user_id)
54
- attribute :title, ROM::Types::String
55
- attribute :body, ROM::Types::String
56
- end
57
-
58
- def by_id(id)
59
- with_path(id.to_s)
60
- end
61
-
62
- def for_user(user)
63
- with_options(
64
- base_path: 'users',
65
- path: "#{user.first[:id]}/posts"
66
- )
67
- end
68
- end
69
-
70
- class UserRepository < ROM::Repository[:users]
71
- relations :posts
72
-
73
- def find(id)
74
- users.by_id(id).first
75
- end
76
-
77
- def find_with_posts(user_id)
78
- users.by_id(user_id).combine_children(many: posts.for_user).first
79
- end
80
- end
81
-
82
- configuration = ROM::Configuration.new(:http, {
83
- uri: 'http://jsonplaceholder.typicode.com',
84
- headers: {
85
- Accept: 'application/json'
86
- },
87
- request_handler: RequestHandler.new,
88
- response_handler: ResponseHandler.new
89
- })
90
- configuration.register_relation(Users)
91
- configuration.register_relation(Posts)
92
- container = ROM.container(configuration)
93
-
94
- UserRepository.new(container).find_with_posts(1)
95
- # =>
96
- # #<ROM::Struct[User]
97
- # id=1
98
- # name="Leanne Graham"
99
- # username="Bret"
100
- # email="Sincere@april.biz"
101
- # phone="1-770-736-8031 x56442"
102
- # website="hildegard.org"
103
- # posts=[
104
- # #<ROM::Struct[Post]
105
- # id=1
106
- # user_id=1
107
- # title="sunt aut facere repellat provident occaecati excepturi optio reprehenderit"
108
- # body="quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto">,
109
- # #<ROM::Struct[Post]
110
- # id=2
111
- # user_id=1
112
- # title="qui est esse"
113
- # body="est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla">,
114
- # #<ROM::Struct[Post]
115
- # id=3
116
- # user_id=1
117
- # title="ea molestias quasi exercitationem repellat qui ipsa sit aut"
118
- # body="et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis pariatur\nmolestiae porro eius odio et labore et velit aut">,
119
- # #<ROM::Struct[Post]
120
- # id=4
121
- # user_id=1
122
- # title="eum et est occaecati"
123
- # body="ullam et saepe reiciendis voluptatem adipisci\nsit amet autem assumenda provident rerum culpa\nquis hic commodi nesciunt rem tenetur doloremque ipsam iure\nquis sunt voluptatem rerum illo velit">,
124
- # #<ROM::Struct[Post]
125
- # id=5
126
- # user_id=1
127
- # title="nesciunt quas odio"
128
- # body="repudiandae veniam quaerat sunt sed\nalias aut fugiat sit autem sed est\nvoluptatem omnis possimus esse voluptatibus quis\nest aut tenetur dolor neque">,
129
- # #<ROM::Struct[Post]
130
- # id=6
131
- # user_id=1
132
- # title="dolorem eum magni eos aperiam quia"
133
- # body="ut aspernatur corporis harum nihil quis provident sequi\nmollitia nobis aliquid molestiae\nperspiciatis et ea nemo ab reprehenderit accusantium quas\nvoluptate dolores velit et doloremque molestiae">,
134
- # #<ROM::Struct[Post]
135
- # id=7
136
- # user_id=1
137
- # title="magnam facilis autem"
138
- # body="dolore placeat quibusdam ea quo vitae\nmagni quis enim qui quis quo nemo aut saepe\nquidem repellat excepturi ut quia\nsunt ut sequi eos ea sed quas">,
139
- # #<ROM::Struct[Post]
140
- # id=8
141
- # user_id=1
142
- # title="dolorem dolore est ipsam"
143
- # body="dignissimos aperiam dolorem qui eum\nfacilis quibusdam animi sint suscipit qui sint possimus cum\nquaerat magni maiores excepturi\nipsam ut commodi dolor voluptatum modi aut vitae">,
144
- # #<ROM::Struct[Post]
145
- # id=9
146
- # user_id=1
147
- # title="nesciunt iure omnis dolorem tempora et accusantium"
148
- # body="consectetur animi nesciunt iure dolore\nenim quia ad\nveniam autem ut quam aut nobis\net est aut quod aut provident voluptas autem voluptas">,
149
- # #<ROM::Struct[Post]
150
- # id=10
151
- # user_id=1
152
- # title="optio molestias id quia eum"
153
- # body="quo et expedita modi cum officia vel magni\ndoloribus qui repudiandae\nvero nisi sit\nquos veniam quod sed accusamus veritatis error">
154
- # ]>
@@ -1,33 +0,0 @@
1
- module ROM
2
- module HTTP
3
- # HTTP Dataset
4
- #
5
- # Represents a specific HTTP collection resource
6
- #
7
- # @api public
8
- class Dataset
9
- # @api private
10
- module ClassInterface
11
- # TODO: Remove in favour of configuration
12
- def default_request_handler(handler = Undefined)
13
- ::Dry::Core::Deprecations.announce(
14
- __method__,
15
- 'use configuration instead'
16
- )
17
- return config.default_request_handler if Undefined === handler
18
- config.default_request_handler = handler
19
- end
20
-
21
- # TODO: Remove in favour of configuration
22
- def default_response_handler(handler = Undefined)
23
- ::Dry::Core::Deprecations.announce(
24
- __method__,
25
- 'use configuration instead'
26
- )
27
- return config.default_response_handler if Undefined === handler
28
- config.default_response_handler = handler
29
- end
30
- end
31
- end
32
- end
33
- end
@@ -1,18 +0,0 @@
1
- begin
2
- require 'rubocop/rake_task'
3
-
4
- Rake::Task[:default].enhance [:rubocop]
5
-
6
- RuboCop::RakeTask.new do |task|
7
- task.options << '--display-cop-names'
8
- end
9
-
10
- namespace :rubocop do
11
- desc 'Generate a configuration file acting as a TODO list.'
12
- task :auto_gen_config do
13
- exec 'bundle exec rubocop --auto-gen-config'
14
- end
15
- end
16
-
17
- rescue LoadError
18
- end
@@ -1,32 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'rom/http/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = 'rom-http'
8
- spec.version = ROM::HTTP::VERSION.dup
9
- spec.authors = ['Piotr Solnica', 'Andy Holland', 'Chris Flipse']
10
- spec.email = ['piotr.solnica@gmail.com', 'andyholland1991@aol.com', 'cflipse@gmail.com']
11
- spec.summary = 'HTTP support for ROM'
12
- spec.description = spec.summary
13
- spec.homepage = 'http://rom-rb.org'
14
- spec.license = 'MIT'
15
-
16
- spec.files = `git ls-files -z`.split("\x0")
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ['lib']
20
-
21
- spec.add_runtime_dependency 'concurrent-ruby'
22
- spec.add_runtime_dependency 'addressable'
23
- spec.add_runtime_dependency 'rom', '~> 4.0'
24
- spec.add_runtime_dependency 'dry-core', '~> 0.2', '>= 0.2.3'
25
- spec.add_runtime_dependency 'dry-equalizer', '~> 0.2'
26
- spec.add_runtime_dependency 'dry-configurable', '~> 0.6'
27
-
28
- spec.add_development_dependency 'bundler'
29
- spec.add_development_dependency 'rspec', '~> 3.1'
30
- spec.add_development_dependency 'rspec-its'
31
- spec.add_development_dependency 'rake', '~> 10.0'
32
- end
@@ -1,119 +0,0 @@
1
- RSpec.describe ROM::HTTP::Commands::Create do
2
- include_context 'setup'
3
- let(:relation) do
4
- Class.new(ROM::HTTP::Relation) do
5
- schema(:users) do
6
- attribute :id, ROM::Types::Int
7
- attribute :first_name, ROM::Types::String
8
- attribute :last_name, ROM::Types::String
9
- end
10
-
11
- def by_id(id)
12
- with_params(id: id)
13
- end
14
- end
15
- end
16
-
17
- context 'with single tuple' do
18
- let(:response) { double }
19
- let(:attributes) { { first_name: 'John', last_name: 'Jackson' } }
20
- let(:tuple) { attributes.merge(id: 1) }
21
- let(:command) do
22
- Class.new(ROM::HTTP::Commands::Create) do
23
- register_as :create
24
- relation :users
25
- result :one
26
- end
27
- end
28
- let(:dataset) do
29
- ROM::HTTP::Dataset.new(
30
- {
31
- uri: uri,
32
- headers: headers,
33
- request_handler: request_handler,
34
- response_handler: response_handler,
35
- name: :users
36
- },
37
- request_method: :post,
38
- params: attributes
39
- )
40
- end
41
-
42
- before do
43
- configuration.register_relation(relation)
44
- configuration.register_command(command)
45
-
46
- allow(request_handler).to receive(:call).and_return(response)
47
- allow(response_handler).to receive(:call).and_return(tuple)
48
- end
49
-
50
- subject! { container.commands[:users].create.call(attributes) }
51
-
52
- it do
53
- expect(request_handler).to have_received(:call).with(dataset)
54
- expect(response_handler).to have_received(:call).with(response, dataset)
55
- is_expected.to eq(tuple)
56
- end
57
- end
58
-
59
- context 'with a collection' do
60
- let(:response_1) { double }
61
- let(:response_2) { double }
62
- let(:attributes_1) { { first_name: 'John', last_name: 'Jackson' } }
63
- let(:attributes_2) { { first_name: 'Jill', last_name: 'Smith' } }
64
- let(:tuple_1) { attributes_1.merge(id: 1) }
65
- let(:tuple_2) { attributes_2.merge(id: 2) }
66
- let(:attributes) { [attributes_1, attributes_2] }
67
- let(:command) do
68
- Class.new(ROM::HTTP::Commands::Create) do
69
- register_as :create
70
- relation :users
71
- result :many
72
- end
73
- end
74
- let(:dataset_1) do
75
- ROM::HTTP::Dataset.new(
76
- {
77
- uri: uri,
78
- headers: headers,
79
- request_handler: request_handler,
80
- response_handler: response_handler,
81
- name: :users
82
- },
83
- request_method: :post,
84
- params: attributes_1
85
- )
86
- end
87
- let(:dataset_2) do
88
- ROM::HTTP::Dataset.new(
89
- {
90
- uri: uri,
91
- headers: headers,
92
- request_handler: request_handler,
93
- response_handler: response_handler,
94
- name: :users
95
- },
96
- request_method: :post,
97
- params: attributes_2
98
- )
99
- end
100
-
101
- before do
102
- configuration.register_relation(relation)
103
- configuration.register_command(command)
104
-
105
- allow(request_handler).to receive(:call).and_return(response_1, response_2)
106
- allow(response_handler).to receive(:call).and_return(tuple_1, tuple_2)
107
- end
108
-
109
- subject! { container.commands[:users].create.call(attributes) }
110
-
111
- it do
112
- expect(request_handler).to have_received(:call).with(dataset_1)
113
- expect(response_handler).to have_received(:call).with(response_1, dataset_1)
114
- expect(request_handler).to have_received(:call).with(dataset_2)
115
- expect(response_handler).to have_received(:call).with(response_2, dataset_2)
116
- is_expected.to eq([tuple_1, tuple_2])
117
- end
118
- end
119
- end