nano-service 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dc688f4a3949cd84be9b748db5f42c569d693522
4
+ data.tar.gz: b285cb214644219c6feed790455d83cab4788770
5
+ SHA512:
6
+ metadata.gz: 0dac73f7d3cd219a3cb6c90dda46d3a6e37ca617f6a05de900c19b5d6a0a774ec92ef56894fa1d633d6eb443f00aae280f1bc11d56b4d50691628b7c2fecd9bf
7
+ data.tar.gz: 38433e47d1dc439dc1635d96802b347912959a36a68b7112e43c121c68f20f27fbf194e0fce7fd0459f7ba740a2319513cfd1adc5b9807041313b88378bf827b
@@ -0,0 +1,4 @@
1
+ Gemfile.lock
2
+ .DS_Store
3
+ .ruby-version
4
+ .ruby-gemset
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at daveriess@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+ ruby '2.2.3'
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ gem 'rake'
7
+ gem 'rspec', '~> 3.4'
8
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Dave Riess
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,8 @@
1
+ [![Build Status](https://circleci.com/gh/wunderteam/nano-service.svg?style=svg)](https://circleci.com/gh/wunderteam/nano-service)
2
+
3
+ # NanoService Prototype
4
+ A thin module wrapper for helping enforce service boundaries.
5
+
6
+ # TODO
7
+ - additional test coverage for dynamic exception definition
8
+ - explore options for more flexible exception mapping
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "nano-service"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,11 @@
1
+ require 'active_support'
2
+ require 'active_record'
3
+ require 'globalid'
4
+
5
+ require 'nano-service/error_serializer'
6
+ require 'nano-service/exceptions'
7
+ require 'nano-service/version'
8
+ require 'nano-service/base'
9
+
10
+ module NanoService
11
+ end
@@ -0,0 +1,68 @@
1
+ module NanoService
2
+ module Base
3
+ extend ActiveSupport::Concern
4
+
5
+ # dynamically define NanoService exceptions on the including Service module
6
+ included do
7
+ @namespace = Kernel.const_get(name.split('::')[0])
8
+
9
+ nano_service_exceptions = NanoService.constants.select do |c|
10
+ klass = NanoService.const_get(c)
11
+ (klass.class === Class) && (klass < StandardError)
12
+ end
13
+
14
+ (nano_service_exceptions - @namespace.constants).each do |e|
15
+ @namespace.const_set(e, NanoService.const_get(e))
16
+ end
17
+ end
18
+
19
+ module ClassMethods
20
+ def caller_object
21
+ @caller_object ||= Class.new.extend(self)
22
+ end
23
+
24
+ def method_missing(method_name, *args, &block)
25
+ if instance_methods.include?(method_name)
26
+ begin
27
+ caller_object.send(method_name, *args)
28
+ rescue Exception => e
29
+ handle_exception(e)
30
+ end
31
+ else
32
+ super
33
+ end
34
+ end
35
+
36
+ def handle_exception(e)
37
+ log_error(e)
38
+
39
+ case e
40
+ when ActiveRecord::RecordNotFound
41
+ raise RecordNotFound, e.message
42
+ when ActiveRecord::RecordInvalid
43
+ raise RecordInvalid.new("#{e.record.class.name} #{e.message}", e.record.errors)
44
+ when ActiveRecord::RecordNotSaved
45
+ raise RecordNotSaved.new("#{e.record.class.name} #{e.message}", e.record.errors)
46
+ when URI::BadURIError
47
+ raise e.message.include?('gid') ? InvalidGlobalID.new(e.message) : e
48
+ when URI::GID::MissingModelIdError
49
+ raise InvalidGlobalID, e.message
50
+ else
51
+ raise e
52
+ end
53
+ end
54
+
55
+ def log_error(e, msg = nil)
56
+ logger.error "[#{@namespace}] #{msg}: #{e.message}"
57
+ end
58
+
59
+ def logger
60
+ @logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
61
+ end
62
+
63
+ def logger=(logger)
64
+ @logger = logger
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,11 @@
1
+ module NanoService
2
+ class ErrorSerializer
3
+ def self.serialize(errors)
4
+ if errors && errors.respond_to?(:to_hash) && errors.respond_to?(:full_messages)
5
+ errors.to_hash.merge(full_messages: errors.try(:full_messages))
6
+ else
7
+ {}
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,25 @@
1
+ module NanoService
2
+ class Error < StandardError
3
+ end
4
+
5
+ class InvalidRecordError < Error
6
+ attr_reader :errors
7
+
8
+ def initialize(msg = nil, errors = nil)
9
+ super(msg)
10
+ @errors = ErrorSerializer.serialize(errors)
11
+ end
12
+ end
13
+
14
+ class RecordNotFound < Error
15
+ end
16
+
17
+ class RecordInvalid < InvalidRecordError
18
+ end
19
+
20
+ class RecordNotSaved < InvalidRecordError
21
+ end
22
+
23
+ class InvalidGlobalID < Error
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module NanoService
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ $LOAD_PATH.push File.expand_path("../lib", __FILE__)
3
+ require "nano-service/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'nano-service'
7
+ s.version = NanoService::VERSION
8
+ s.license = "MIT"
9
+ s.authors = ['Dave Riess']
10
+ s.email = ['dave@wundercapital.com']
11
+ s.homepage = 'https://github.com/wunderteam/nano-service'
12
+ s.summary = 'A thin module wrapper for helping enforce service boundaries'
13
+ s.description = 'A thin module wrapper for helping enforce service boundaries'
14
+
15
+ s.required_ruby_version = '>= 2.2.0'
16
+ s.add_dependency 'activerecord', "~> 4.0"
17
+ s.add_dependency 'activesupport', "~> 4.0"
18
+ s.add_dependency 'globalid', "~> 0"
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.require_paths = ["lib"]
23
+ end
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+
3
+ MockService.logger = Logger.new('/dev/null')
4
+
5
+ describe NanoService::Base do
6
+ describe 'method proxy' do
7
+ it 'proxies instance methods through class method_missing' do
8
+ allow_any_instance_of(MockService).to receive(:foo) { 'bar' }
9
+ expect(MockService.foo).to eq('bar')
10
+ end
11
+
12
+ it 'does not proxy private instance methods through class method_missing' do
13
+ expect { MockService.private_foo }.to raise_error(NoMethodError)
14
+ end
15
+ end
16
+
17
+ describe 'exception handling' do
18
+ it 'passes exception to handle_exception' do
19
+ allow_any_instance_of(MockService).to receive(:exception!) { raise StandardError }
20
+ allow(MockService).to receive(:handle_exception) { true }
21
+ MockService.exception!
22
+
23
+ expect(MockService).to have_received(:handle_exception).with(StandardError)
24
+ end
25
+
26
+ # allows service to raise NanoService errors directly
27
+ describe 'NanoService exceptions' do
28
+ it 'catches and re-raises any NanoService exceptions' do
29
+ allow_any_instance_of(MockService).to receive(:exception!) do
30
+ raise NanoService::Error
31
+ end
32
+
33
+ expect { MockService.exception! }.to raise_error(NanoService::Error)
34
+ end
35
+ end
36
+
37
+ # dynamically defines NanoService errors within service namespace
38
+ describe 'NanoService exceptions' do
39
+ it 'catches and re-raises any NanoService exceptions' do
40
+ allow_any_instance_of(MockService).to receive(:exception!) do
41
+ raise MockService::Error
42
+ end
43
+
44
+ expect { MockService.exception! }.to raise_error(MockService::Error)
45
+ end
46
+ end
47
+
48
+ # re-packages known errors as NanoService errors
49
+ describe 'known exceptions' do
50
+ it 'catches ActiveRecord::RecordNotFound exceptions and returns RecordNotFound' do
51
+ allow_any_instance_of(MockService).to receive(:exception!) do
52
+ raise ActiveRecord::RecordNotFound
53
+ end
54
+
55
+ expect { MockService.exception! }.to raise_error(NanoService::RecordNotFound)
56
+ end
57
+
58
+ # TODO: do a better job mocking ActiveRecord errors
59
+ # it 'catches ActiveRecord::RecordInvalid exceptions and returns RecordInvalid' do
60
+ # allow_any_instance_of(MockService).to receive(:exception!) do
61
+ # raise ActiveRecord::RecordInvalid, 'foobar'
62
+ # end
63
+ #
64
+ # expect{ MockService.exception! }.to raise_error(NanoService::RecordInvalid)
65
+ # end
66
+ #
67
+ # it 'catches ActiveRecord::RecordNotSaved exceptions and returns RecordNotSaved' do
68
+ # allow_any_instance_of(MockService).to receive(:exception!) do
69
+ # raise ActiveRecord::RecordNotSaved
70
+ # end
71
+ #
72
+ # expect{ MockService.exception! }.to raise_error(NanoService::RecordNotSaved)
73
+ # end
74
+
75
+ it 'catches URI::BadURIError (gid) exceptions and returns InvalidGlobalID' do
76
+ allow_any_instance_of(MockService).to receive(:exception!) { GlobalID.new('foo') }
77
+
78
+ expect { MockService.exception! }.to raise_error(NanoService::InvalidGlobalID)
79
+ end
80
+
81
+ it 'catches URI::GID::MissingModelIdError exceptions and returns InvalidGlobalID' do
82
+ allow_any_instance_of(MockService).to receive(:exception!) { GlobalID.new('gid://foo/Bar') }
83
+
84
+ expect { MockService.exception! }.to raise_error(NanoService::InvalidGlobalID)
85
+ end
86
+ end
87
+
88
+ # re-rasies unknown errors
89
+ describe 'unknown exception' do
90
+ it 'catches StandardError and returns StandardError' do
91
+ allow_any_instance_of(MockService).to receive(:exception!) { raise StandardError }
92
+
93
+ expect { MockService.exception! }.to raise_error(StandardError)
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ class Fish
4
+ extend ActiveModel::Naming
5
+ extend ActiveModel::Translation
6
+ attr_accessor :name, :type
7
+ def errors
8
+ @errors ||= ActiveModel::Errors.new(self)
9
+ end
10
+ end
11
+
12
+ describe NanoService::ErrorSerializer do
13
+ subject { described_class }
14
+
15
+ let(:model) { Fish.new }
16
+
17
+ before do
18
+ model.errors.add(:name, 'is required')
19
+ model.errors.add(:type, 'is invalid')
20
+ model.errors.add(:type, 'must be one of (fighting,freshwater)')
21
+ end
22
+
23
+ describe '::serialize' do
24
+ it 'serializes model errors for public consumption' do
25
+ expect(subject.serialize(model.errors)).to eq(
26
+ name: ['is required'],
27
+ type: ['is invalid', 'must be one of (fighting,freshwater)'],
28
+ full_messages: [
29
+ 'Name is required',
30
+ 'Type is invalid',
31
+ 'Type must be one of (fighting,freshwater)'
32
+ ]
33
+ )
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ require 'nano-service'
2
+
3
+ Dir[File.expand_path(File.join(File.dirname(__FILE__), 'support', '**', '*.rb'))].each {|f| require f}
@@ -0,0 +1,9 @@
1
+ module MockService
2
+ include NanoService::Base
3
+
4
+ private
5
+
6
+ def private_foo
7
+ 'bar'
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ module MockService
2
+ class CustomError < NanoService::Error
3
+ end
4
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nano-service
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dave Riess
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: globalid
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A thin module wrapper for helping enforce service boundaries
56
+ email:
57
+ - dave@wundercapital.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - lib/nano-service.rb
71
+ - lib/nano-service/base.rb
72
+ - lib/nano-service/error_serializer.rb
73
+ - lib/nano-service/exceptions.rb
74
+ - lib/nano-service/version.rb
75
+ - nano-service.gemspec
76
+ - spec/base_spec.rb
77
+ - spec/error_serializer_spec.rb
78
+ - spec/spec_helper.rb
79
+ - spec/support/mock_service.rb
80
+ - spec/support/mock_service/exceptions.rb
81
+ homepage: https://github.com/wunderteam/nano-service
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 2.2.0
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.4.5.1
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: A thin module wrapper for helping enforce service boundaries
105
+ test_files:
106
+ - spec/base_spec.rb
107
+ - spec/error_serializer_spec.rb
108
+ - spec/spec_helper.rb
109
+ - spec/support/mock_service.rb
110
+ - spec/support/mock_service/exceptions.rb