hecks-adapters-resource-server 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2f5842d1b07e0f94bd094964157cd209b82e89b3
4
+ data.tar.gz: 2473467ae31a8fdef75dd4176d4eef11ea17a18d
5
+ SHA512:
6
+ metadata.gz: 456fb2eb90f538d140a182b3fd00dd5c96234b6d7531838cd04358b99d385542d54a3fba21261a8344e381f3cd076fc2be4518d1f895646b40a43b08417a76b6
7
+ data.tar.gz: 70cffbe83cab8088fbef840c3686848ca4075630762d83e845d5d5a77adb39a6b35c10cdb1fa800a11b31bf12ba2d42f7b71874d1bbc17bafff0dd4353b8d53f
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+ require 'sinatra'
3
+ require 'hecks-application'
4
+ require 'json'
5
+ require_relative 'methods'
6
+
7
+ module Hecks
8
+ module Adapters
9
+ class ResourceServer < Sinatra::Base
10
+ configure do
11
+ set :raise_errors, true
12
+ set :show_exceptions, false
13
+ end
14
+
15
+ def initialize(app: nil, application_adapter:)
16
+ super(app)
17
+ @methods = Methods.new(application_adapter: application_adapter)
18
+ end
19
+
20
+ get '/:module_name/:id' do |module_name, id|
21
+ command = methods.read.call(id: id, module_name: module_name)
22
+ body command.result
23
+ status command.status
24
+ end
25
+
26
+ post '/:module_name' do |module_name|
27
+ command = methods.create.call(body: request.body, module_name: module_name)
28
+ status command.status
29
+ body command.result
30
+ end
31
+
32
+ put '/:module_name/:id' do |module_name, id|
33
+ command = methods.update.call(
34
+ id: id,
35
+ body: request.body,
36
+ module_name: module_name
37
+ )
38
+ status command.status
39
+ body command.result
40
+ end
41
+
42
+ delete '/:module_name/:id' do |module_name, id|
43
+ command = methods.delete.call(id: id, module_name: module_name)
44
+ status command.status
45
+ body command.result
46
+ end
47
+
48
+ private
49
+
50
+ attr_reader :methods
51
+ end
52
+ end
53
+ end
data/lib/methods.rb ADDED
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ require_relative 'methods/create'
3
+ require_relative 'methods/read'
4
+ require_relative 'methods/update'
5
+ require_relative 'methods/delete'
6
+
7
+ module Hecks
8
+ module Adapters
9
+ class ResourceServer
10
+ class Methods
11
+ attr_reader :create, :read, :update, :delete
12
+
13
+ def initialize(application_adapter:)
14
+ @create = Create.new(application_adapter: application_adapter)
15
+ @read = Read.new(application_adapter: application_adapter)
16
+ @update = Update.new(application_adapter: application_adapter)
17
+ @delete = Delete.new(application_adapter: application_adapter)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+ module Hecks
3
+ module Adapters
4
+ class ResourceServer < Sinatra::Base
5
+ class Methods
6
+ class Create
7
+ attr_reader :result
8
+
9
+ def initialize(application_adapter:)
10
+ @application_adapter = application_adapter
11
+ end
12
+
13
+ def call(body:, module_name:)
14
+ @body = body.read
15
+ @module_name = module_name.to_sym
16
+ run_command
17
+ build_json
18
+ self
19
+ end
20
+
21
+ def status
22
+ return 500 if command_result.errors.count.positive?
23
+ 200
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :application_adapter, :body, :module_name, :command_result
29
+
30
+ def build_json
31
+ @result = JSON.generate(command_result.to_h)
32
+ end
33
+
34
+ def run_command
35
+ @command_result = application_adapter.call(
36
+ module_name: module_name,
37
+ command_name: :create,
38
+ args: params
39
+ )
40
+ end
41
+
42
+ def params
43
+ JSON.parse(body, symbolize_names: true)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+ module Hecks
3
+ module Adapters
4
+ class ResourceServer < Sinatra::Base
5
+ class Methods
6
+ class Delete
7
+ attr_reader :result
8
+
9
+ def initialize(application_adapter:)
10
+ @application_adapter = application_adapter
11
+ end
12
+
13
+ def call(id:, module_name:)
14
+ @id = id.to_i
15
+ @module_name = module_name.to_sym
16
+ run_command
17
+ convert_to_json
18
+ self
19
+ end
20
+
21
+ def status
22
+ return 500 if command_result.errors.count.positive?
23
+ 200
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :application_adapter, :module_name, :id, :command_result
29
+
30
+ def convert_to_json
31
+ @result = JSON.generate(command_result.to_h)
32
+ end
33
+
34
+ def run_command
35
+ @command_result = application_adapter.call(
36
+ module_name: module_name,
37
+ command_name: :delete,
38
+ args: { id: id }
39
+ )
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+ module Hecks
3
+ module Adapters
4
+ class ResourceServer < Sinatra::Base
5
+ class Methods
6
+ class Read
7
+ attr_reader :result, :status
8
+ def initialize(application_adapter:)
9
+ @application_adapter = application_adapter
10
+ end
11
+
12
+ def call(id:, module_name:)
13
+ @id = id.to_i
14
+ @module_name = module_name.to_sym
15
+ run_query
16
+ convert_to_json
17
+ self
18
+ end
19
+
20
+ def status
21
+ return 404 if command_result.nil?
22
+ 200
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :application_adapter, :id, :module_name, :command_result
28
+
29
+ def convert_to_json
30
+ @result = command_result.to_json
31
+ end
32
+
33
+ def run_query
34
+ @command_result = application_adapter.query(
35
+ query_name: :find_by_id,
36
+ module_name: module_name,
37
+ args: { id: id }
38
+ )
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+ module Hecks
3
+ module Adapters
4
+ class ResourceServer < Sinatra::Base
5
+ class Methods
6
+ class Update
7
+ attr_reader :result
8
+
9
+ def initialize(application_adapter:)
10
+ @application_adapter = application_adapter
11
+ end
12
+
13
+ def call(id:, body:, module_name:)
14
+ @id = id.to_i
15
+ @body = body.read
16
+ @module_name = module_name.to_sym
17
+ run_command
18
+ convert_to_json
19
+ self
20
+ end
21
+
22
+ def status
23
+ return 500 if command_result.errors.count.positive?
24
+ 200
25
+ end
26
+
27
+ private
28
+
29
+ attr_accessor :application_adapter, :id, :body, :module_name, :command_result
30
+
31
+ def convert_to_json
32
+ @result = JSON.generate(command_result.to_h)
33
+ end
34
+
35
+ def run_command
36
+ @command_result = application_adapter.call(
37
+ module_name: module_name,
38
+ command_name: :update,
39
+ args: params
40
+ )
41
+ end
42
+
43
+ def params
44
+ JSON.parse(body, symbolize_names: true).merge(id: id)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hecks-adapters-resource-server
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Young
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Make the Domain the center of your programming world
14
+ email: chris@example.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/hecks-adapters-resource-server.rb
20
+ - lib/methods.rb
21
+ - lib/methods/create.rb
22
+ - lib/methods/delete.rb
23
+ - lib/methods/read.rb
24
+ - lib/methods/update.rb
25
+ homepage: https://github.com/chrisyoung/hecks-domain
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.6.10
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: DDD and Hexagonal Code Generators
49
+ test_files: []