salestation 0.0.6 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +57 -8
- data/lib/salestation/web/error_mapper.rb +12 -6
- data/lib/salestation/web/responses.rb +1 -1
- data/lib/salestation/web.rb +17 -8
- data/salestation.gemspec +3 -3
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7af82fc0b7698f3f66945b5291d2a2d9ccf869bd
|
4
|
+
data.tar.gz: e0f635d0e541cb790124900cc7c6b2cc10a8fc5e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: deab697cdfd27d779bdf67e09508e0dbadb602e0351b6727592f9d695e54de121223133f01d8e92af3ca818fe5952b3f8831f49c776b5e508ea8f61c23081db5
|
7
|
+
data.tar.gz: 818726078b70b2e9a44b6af528e81a8e2aaef8ceffba89ec299b61c345c67a002152d164f6200fd31dfa5f33ca46f584118760728c70ad8fd713f6cca194f0b3
|
data/README.md
CHANGED
@@ -1,9 +1,5 @@
|
|
1
1
|
# Salestation
|
2
2
|
|
3
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/salestation`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
6
|
-
|
7
3
|
## Installation
|
8
4
|
|
9
5
|
Add this line to your application's Gemfile:
|
@@ -16,13 +12,66 @@ And then execute:
|
|
16
12
|
|
17
13
|
$ bundle
|
18
14
|
|
19
|
-
|
15
|
+
## Usage
|
20
16
|
|
21
|
-
|
17
|
+
### Using Salestation with Sinatra
|
22
18
|
|
23
|
-
|
19
|
+
First include `Salestation::Web`. This will provide a method called `process` to execute a request and Responses module for return codes.
|
20
|
+
```ruby
|
21
|
+
class Webapp < Sinatra::Base
|
22
|
+
include Salestation::Web.new
|
23
|
+
end
|
24
|
+
```
|
24
25
|
|
25
|
-
|
26
|
+
Create Salestation application:
|
27
|
+
```ruby
|
28
|
+
def app
|
29
|
+
@_app ||= Salestation::App.new(env: ENVIRONMENT)
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
Define a route
|
34
|
+
```ruby
|
35
|
+
post '/hello/:name' do
|
36
|
+
process(
|
37
|
+
HelloUser.call(app.create_request(
|
38
|
+
name: params['name']
|
39
|
+
)).map(Responses.to_ok)
|
40
|
+
)
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
Define chain
|
45
|
+
```ruby
|
46
|
+
class HelloUser
|
47
|
+
def self.call(request)
|
48
|
+
request >> upcase >> format
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.upcase
|
52
|
+
-> (request) do
|
53
|
+
input.with_input(name: input.fetch(:name).upcase)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.format
|
58
|
+
-> (request) do
|
59
|
+
name = request.input.fetch(:name)
|
60
|
+
Deterministic::Result::Success(message: "Hello #{name}")
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
### Using custom errors in error mapper
|
67
|
+
|
68
|
+
Salestation allows and recommends you to define your own custom errors. This is useful when your app has error classes that are not general enough for the salestation library.
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
include Salestation::Web.new(errors: {
|
72
|
+
CustomError => -> (error) { CustomResponse.new(error) }
|
73
|
+
})
|
74
|
+
```
|
26
75
|
|
27
76
|
## Development
|
28
77
|
|
@@ -1,7 +1,9 @@
|
|
1
1
|
module Salestation
|
2
|
-
|
3
|
-
|
4
|
-
|
2
|
+
class Web < Module
|
3
|
+
class ErrorMapper
|
4
|
+
UndefinedErrorClass = Class.new(StandardError)
|
5
|
+
|
6
|
+
ERROR_TO_RESPONSE_DEFAULTS = {
|
5
7
|
App::Errors::InvalidInput => -> (error) {
|
6
8
|
Responses::UnprocessableEntityFromSchemaErrors.create(error)
|
7
9
|
},
|
@@ -19,9 +21,13 @@ module Salestation
|
|
19
21
|
}
|
20
22
|
}.freeze
|
21
23
|
|
22
|
-
def
|
24
|
+
def initialize(map = {})
|
25
|
+
@error_to_response_map = ERROR_TO_RESPONSE_DEFAULTS.merge(map)
|
26
|
+
end
|
27
|
+
|
28
|
+
def map
|
23
29
|
-> (error) do
|
24
|
-
_, error_mapper =
|
30
|
+
_, error_mapper = @error_to_response_map
|
25
31
|
.find {|error_type, _| error.kind_of?(error_type) }
|
26
32
|
|
27
33
|
# Interpret a Failure from the application layer as Success in the web
|
@@ -32,7 +38,7 @@ module Salestation
|
|
32
38
|
elsif error.is_a?(Hash)
|
33
39
|
Deterministic::Result::Success(Responses::InternalError.new(error))
|
34
40
|
else
|
35
|
-
raise "
|
41
|
+
raise UndefinedErrorClass, "Undefined error class: #{error.class.name}"
|
36
42
|
end
|
37
43
|
end
|
38
44
|
end
|
data/lib/salestation/web.rb
CHANGED
@@ -1,22 +1,31 @@
|
|
1
1
|
require 'deterministic'
|
2
2
|
require 'dry-struct'
|
3
3
|
require 'dry-types'
|
4
|
+
require 'json'
|
4
5
|
|
5
6
|
module Salestation
|
6
|
-
|
7
|
+
class Web < Module
|
7
8
|
module Types
|
8
9
|
include Dry::Types.module
|
9
10
|
end
|
10
11
|
|
11
|
-
def
|
12
|
-
|
13
|
-
|
14
|
-
status result.status
|
15
|
-
json JSON.dump(result.body)
|
12
|
+
def initialize(errors: {})
|
13
|
+
@error_mapper = ErrorMapper.new(errors)
|
16
14
|
end
|
17
15
|
|
18
|
-
def
|
19
|
-
|
16
|
+
def included(base)
|
17
|
+
error_mapper = @error_mapper
|
18
|
+
|
19
|
+
base.class_eval do
|
20
|
+
const_set :Responses, Salestation::Web::Responses
|
21
|
+
|
22
|
+
define_method(:process) do |response|
|
23
|
+
result = response.map_err(error_mapper.map).value
|
24
|
+
|
25
|
+
status result.status
|
26
|
+
json JSON.dump(result.body)
|
27
|
+
end
|
28
|
+
end
|
20
29
|
end
|
21
30
|
end
|
22
31
|
end
|
data/salestation.gemspec
CHANGED
@@ -4,9 +4,9 @@ $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
|
8
|
-
spec.authors = ["SaleMove
|
9
|
-
spec.email = ["
|
7
|
+
spec.version = "0.1.0"
|
8
|
+
spec.authors = ["SaleMove TechMovers"]
|
9
|
+
spec.email = ["techmovers@salemove.com"]
|
10
10
|
|
11
11
|
spec.summary = %q{}
|
12
12
|
spec.description = %q{}
|
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
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
- SaleMove
|
7
|
+
- SaleMove TechMovers
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-03-
|
11
|
+
date: 2017-03-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -96,7 +96,7 @@ dependencies:
|
|
96
96
|
version: '0'
|
97
97
|
description: ''
|
98
98
|
email:
|
99
|
-
-
|
99
|
+
- techmovers@salemove.com
|
100
100
|
executables: []
|
101
101
|
extensions: []
|
102
102
|
extra_rdoc_files: []
|