salestation 0.0.6 → 0.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0ad0a5c28c6ca1928c3bc706710d16a3bc0866c3
4
- data.tar.gz: fdaaedb5d408d3d4cd352ee19c54a084ee55ff0c
3
+ metadata.gz: 7af82fc0b7698f3f66945b5291d2a2d9ccf869bd
4
+ data.tar.gz: e0f635d0e541cb790124900cc7c6b2cc10a8fc5e
5
5
  SHA512:
6
- metadata.gz: ffaa1d8a34ea0d68084a7392579c97307cfed6561620e0de64a2970d4daddba3d8ba2ea2d25c4d168cba0d0e921715fc7cb85ec480e06fafd82477af484019fe
7
- data.tar.gz: a1cc884e2202b47881d7bda8e6c377bf761fbaca1a8882d8c6070fb66607b2e3097121e7823960eeadbef9b3e9ef175312a7fd74722a5bf19c7c612ce04ea0ec
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
- Or install it yourself as:
15
+ ## Usage
20
16
 
21
- $ gem install salestation
17
+ ### Using Salestation with Sinatra
22
18
 
23
- ## Usage
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
- TODO: Write usage instructions here
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
- module Web
3
- module ErrorMapper
4
- ERROR_TO_RESPONSE = {
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 self.map
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 = ERROR_TO_RESPONSE
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 "Unknown error #{error.class.name}"
41
+ raise UndefinedErrorClass, "Undefined error class: #{error.class.name}"
36
42
  end
37
43
  end
38
44
  end
@@ -1,5 +1,5 @@
1
1
  module Salestation
2
- module Web
2
+ class Web < Module
3
3
  module Responses
4
4
  def self.to_created
5
5
  -> (object) { Deterministic::Result::Success(Responses::Created.new(body: object)) }
@@ -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
- module Web
7
+ class Web < Module
7
8
  module Types
8
9
  include Dry::Types.module
9
10
  end
10
11
 
11
- def process(response)
12
- result = response.map_err(&ErrorMapper.map).value
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 create_request(env, input)
19
- App::Request.create(env: env, input: input)
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.6"
8
- spec.authors = ["SaleMove Developers"]
9
- spec.email = ["support@salemove.com"]
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.6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
- - SaleMove Developers
7
+ - SaleMove TechMovers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-24 00:00:00.000000000 Z
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
- - support@salemove.com
99
+ - techmovers@salemove.com
100
100
  executables: []
101
101
  extensions: []
102
102
  extra_rdoc_files: []