salestation 0.0.1 → 0.0.2

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: 91cc1ea2c779abccc3963933610f22c95318855d
4
- data.tar.gz: e939be79576d14892348daf92e63d3295609c0ed
3
+ metadata.gz: 0a0d7d199f2064988590ab8d627bc8f809e78b24
4
+ data.tar.gz: 310534a877c76635785ea428eb4f4d89a5c1ce6b
5
5
  SHA512:
6
- metadata.gz: e2089647f52e855db70555c8369dc59fd8ea5a840cdf1de86ba479b5dba5985017ea5c9fa1c7441aa26afdfd7f7be91efe2c9539c3aaaeb044a4e1c9e114922a
7
- data.tar.gz: 9b0c99f5c46212feb7b8af2fcc86f1329671ec8d4da6cf130047e1e31b201f202445b271d9b927f9bb9c0ac7ecfc1cadde0df74109ce2e7a8c04d847837b9212
6
+ metadata.gz: 2a555ce987565baaec4cb39ba51b2b4105aba8c77acd522bb80d6f40875673124f59214f22291cc46be5bd6209364a211bbe4689bbb57d45984045f249610e7c
7
+ data.tar.gz: 71b32ec7cf9e996b050636c2e50e7d4a3a6e655d311de6c89efccdcfef5dd7e3cd1609f2df7b1707ba8f3dfe5bb27c568fb6b64dbf2e9079c5003dca2d491f4e
@@ -1,21 +1,17 @@
1
1
  module Salestation
2
- module App
2
+ class App
3
3
  module Errors
4
- class InvalidInput
5
- include Virtus.value_object(strict: true)
4
+ class InvalidInput < Dry::Struct
5
+ constructor_type :strict_with_defaults
6
6
 
7
- values do
8
- attribute :errors, Hash
9
- attribute :hints, Hash
10
- end
7
+ attribute :errors, Types::Strict::Hash
8
+ attribute :hints, Types::Coercible::Hash.default({})
11
9
  end
12
10
 
13
- class DependencyCurrentlyUnavailable
14
- include Virtus.value_object(strict: true)
11
+ class DependencyCurrentlyUnavailable < Dry::Struct
12
+ constructor_type :strict
15
13
 
16
- values do
17
- attribute :message, String
18
- end
14
+ attribute :message, Types::Strict::String
19
15
  end
20
16
  end
21
17
  end
@@ -1,5 +1,5 @@
1
1
  module Salestation
2
- module App
2
+ class App
3
3
  module InputVerification
4
4
  def verify_input(schema)
5
5
  -> (request) do
@@ -1,8 +1,8 @@
1
1
  module Salestation
2
- module App
2
+ class App
3
3
  class Request
4
- def self.create(env:, input:)
5
- new(env: env, input: input).to_success
4
+ def self.create(env:, input:, initialize_hook: nil)
5
+ new(env: env, input: input, initialize_hook: initialize_hook).to_success
6
6
  end
7
7
 
8
8
  attr_reader :env, :input
@@ -12,7 +12,7 @@ module Salestation
12
12
  end
13
13
 
14
14
  def replace_input(new_input)
15
- self.class.new(env: env, input: new_input).to_success
15
+ self.class.new(env: env, input: new_input, initialize_hook: @initialize_hook).to_success
16
16
  end
17
17
 
18
18
  def to_success
@@ -23,11 +23,20 @@ module Salestation
23
23
  Deterministic::Result::Failure(input)
24
24
  end
25
25
 
26
+ # Initializes an asynchronous application hook
27
+ #
28
+ # Set a listener on App instance to receive a notification when the
29
+ # asynchronous process completes.
30
+ def initialize_hook(hook, payload)
31
+ @initialize_hook.call(hook, payload)
32
+ end
33
+
26
34
  private
27
35
 
28
- def initialize(env:, input:)
36
+ def initialize(env:, input:, initialize_hook:)
29
37
  @env = env
30
38
  @input = input
39
+ @initialize_hook = initialize_hook
31
40
  end
32
41
  end
33
42
  end
@@ -1,12 +1,47 @@
1
1
  require 'deterministic'
2
- require 'virtus'
3
-
4
- require_relative './app/errors'
5
- require_relative './app/request'
6
- require_relative './app/input_verification'
7
- require_relative './app/result_helper'
2
+ require 'dry-struct'
3
+ require 'dry-types'
8
4
 
9
5
  module Salestation
10
- module App
6
+ class App
7
+ module Types
8
+ include Dry::Types.module
9
+ end
10
+
11
+ def initialize(env:, hooks: {})
12
+ @environment = env
13
+ @hook_listeners = {}
14
+ @hooks = hooks
15
+ end
16
+
17
+ def start
18
+ @hooks.each do |hook_type, hook|
19
+ hook.start_listening do |payload|
20
+ @hook_listeners.fetch(hook_type, []).each { |handle| handle.call(payload) }
21
+ end
22
+ end
23
+ end
24
+
25
+ def create_request(input)
26
+ Request.create(env: @environment, input: input, initialize_hook: method(:initialize_hook))
27
+ end
28
+
29
+ def register_listener(hook_type, listener)
30
+ @hook_listeners[hook_type] ||= []
31
+ @hook_listeners[hook_type].push(listener)
32
+ end
33
+
34
+ private
35
+
36
+ def initialize_hook(hook_type, payload)
37
+ raise "Unknown hook_type #{hook_type}" unless @hooks[hook_type]
38
+
39
+ @hooks[hook_type].init(payload)
40
+ end
11
41
  end
12
42
  end
43
+
44
+ require_relative './app/errors'
45
+ require_relative './app/request'
46
+ require_relative './app/input_verification'
47
+ require_relative './result_helper'
@@ -0,0 +1,16 @@
1
+ module Salestation
2
+ module ResultHelper
3
+ def observe(&block)
4
+ -> (result_value) do
5
+ block.call(result_value)
6
+ Deterministic::Result::Success(result_value)
7
+ end
8
+ end
9
+
10
+ def empty_success_response
11
+ -> (request) do
12
+ Deterministic::Result::Success({})
13
+ end
14
+ end
15
+ end
16
+ end
@@ -12,37 +12,33 @@ module Salestation
12
12
  module Response
13
13
  def with_code(code)
14
14
  Class.new(self) do
15
- define_method :initialize do |attrs|
15
+ define_singleton_method :new do |attrs|
16
16
  super(attrs.merge(status: code))
17
17
  end
18
18
  end
19
19
  end
20
20
  end
21
21
 
22
- class Error
22
+ class Error < Dry::Struct
23
23
  extend Response
24
- include Virtus.value_object(strict: true)
24
+ constructor_type :strict_with_defaults
25
25
 
26
- values do
27
- attribute :status, Integer
28
- attribute :message, String
29
- attribute :debug_message, String, default: ''
30
- attribute :context, Hash, default: {}
31
- end
26
+ attribute :status, Types::Strict::Int
27
+ attribute :message, Types::Strict::String
28
+ attribute :debug_message, Types::String.default('')
29
+ attribute :context, Types::Hash.default({})
32
30
 
33
31
  def body
34
- {message: message}
32
+ {message: message, debug_message: debug_message}
35
33
  end
36
34
  end
37
35
 
38
- class Success
36
+ class Success < Dry::Struct
39
37
  extend Response
40
- include Virtus.value_object(strict: true)
38
+ constructor_type :strict
41
39
 
42
- values do
43
- attribute :status, Integer
44
- attribute :body, Hash
45
- end
40
+ attribute :status, Types::Strict::Int
41
+ attribute :body, Types::Strict::Hash
46
42
  end
47
43
 
48
44
  class UnprocessableEntityFromSchemaErrors
@@ -1,19 +1,12 @@
1
- require_relative './web/responses'
2
- require_relative './web/error_mapper'
1
+ require 'deterministic'
2
+ require 'dry-struct'
3
+ require 'dry-types'
3
4
 
4
5
  module Salestation
5
6
  module Web
6
- ERROR_MAPPER = {
7
- App::Errors::InvalidInput => -> (error) {
8
- Responses::UnprocessableEntityFromSchemaErrors.create(error)
9
- },
10
- App::Errors::DependencyCurrentlyUnavailable => -> (error) {
11
- Responses::ServiceUnavailable.new(
12
- message: error.message,
13
- debug_message: "Please try again later"
14
- )
15
- }
16
- }.freeze
7
+ module Types
8
+ include Dry::Types.module
9
+ end
17
10
 
18
11
  def process(response)
19
12
  result = response.map_err(&ErrorMapper.map).value
@@ -27,3 +20,7 @@ module Salestation
27
20
  end
28
21
  end
29
22
  end
23
+
24
+ require_relative './web/responses'
25
+ require_relative './web/error_mapper'
26
+ require_relative './result_helper'
data/salestation.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "salestation"
7
- spec.version = "0.0.1"
7
+ spec.version = "0.0.2"
8
8
  spec.authors = ["SaleMove Developers"]
9
9
  spec.email = ["support@salemove.com"]
10
10
 
@@ -24,5 +24,6 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "rspec", "~> 3.0"
25
25
 
26
26
  spec.add_dependency 'deterministic'
27
- spec.add_dependency 'virtus'
27
+ spec.add_dependency 'dry-struct'
28
+ spec.add_dependency 'dry-types'
28
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: salestation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - SaleMove Developers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-11 00:00:00.000000000 Z
11
+ date: 2016-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,7 +67,21 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: virtus
70
+ name: dry-struct
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: dry-types
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
87
  - - ">="
@@ -99,7 +113,7 @@ files:
99
113
  - lib/salestation/app/errors.rb
100
114
  - lib/salestation/app/input_verification.rb
101
115
  - lib/salestation/app/request.rb
102
- - lib/salestation/app/result_helper.rb
116
+ - lib/salestation/result_helper.rb
103
117
  - lib/salestation/web.rb
104
118
  - lib/salestation/web/error_mapper.rb
105
119
  - lib/salestation/web/responses.rb
@@ -1,18 +0,0 @@
1
- module Salestation
2
- module App
3
- module ResultHelper
4
- def observe(&block)
5
- -> (result_value) do
6
- block.call(result_value)
7
- Deterministic::Result::Success(result_value)
8
- end
9
- end
10
-
11
- def empty_success_response
12
- -> (request) do
13
- Deterministic::Result::Success({})
14
- end
15
- end
16
- end
17
- end
18
- end