hc-service 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ae92c9d8cf0bfc66161c4dcb308e25c381a0058b60c1999589c3408fcd4a3ef4
4
+ data.tar.gz: bd9454567992da45f6028fef9ec84893ac1ede4d87fde86864b1bc6af132d402
5
+ SHA512:
6
+ metadata.gz: 2cbf8229515832284bf0bccc10ca8d22e126dc7cc99f5b24a65b14636afd0649eee675cc7ac689dc6dacc0d139f766f7c1ad326487e18f796e6f2c76eb6b2016
7
+ data.tar.gz: d196996dcdf80423f7b6db30f38e43e9c39edb891c3b7b4363ac12a8e71c9bf90031732aab7f2cc2c12ae9ef9d1dfc2e3b8e87ae67079731166f4d51c1198c77
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in hc-permalink.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Hockey Community
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.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # HC::Service
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/hc/service`. 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
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'hc-service'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install hc-service
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hc-service.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hc/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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "hc/service/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hc-service"
8
+ spec.version = "0.1.0"
9
+ spec.authors = ["Jack Hayter"]
10
+ spec.email = ["jack@hockey-community.com"]
11
+
12
+ spec.summary = "A super simple object serializer"
13
+ spec.description = "Transforms objects and collections into hashes for rendering as JSON"
14
+ spec.homepage = "https://github.com/HockeyCommunity/service"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler"
25
+ end
data/lib/hc/service.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "hc/service/base"
2
+ require "hc/service/version"
3
+
4
+ module HC
5
+ module Service
6
+
7
+ end
8
+ end
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HC
4
+ module Service
5
+
6
+ # Used for abstraction and clarity of service objects
7
+ #
8
+ class Base
9
+
10
+ include ActiveModel::Validations
11
+ include ActiveModel::Callbacks
12
+
13
+ attr_accessor :result, :manual_status_code
14
+
15
+ # Set instance variables based on arg smearing
16
+ #
17
+ def initialize(args)
18
+ args.each do |k, v|
19
+ instance_variable_set("@#{k}", v)
20
+ end
21
+ end
22
+
23
+ # Abstraction for getting a stable service code
24
+ #
25
+ def status_code
26
+ return manual_status_code if manual_status_code
27
+ return 422 if errors.present?
28
+ return 200 if result
29
+ return 0
30
+ end
31
+
32
+ def raise_error!(field:, message:, code: 0)
33
+ errors.add(field, message)
34
+ self.manual_status_code = code
35
+ raise ActiveRecord::Rollback
36
+ end
37
+
38
+ def raise_errors!(child_errors)
39
+ Array.wrap(child_errors).each do |error|
40
+ error.messages.each_key do |field|
41
+ error.messages[field].each do |message|
42
+ errors.add(field, message)
43
+ end
44
+ end
45
+ end
46
+ raise ActiveRecord::Rollback
47
+ end
48
+
49
+ def unauthorized!
50
+ errors.add(:user, 'Unauthorized')
51
+ self.manual_status_code = 401
52
+ raise ActiveRecord::Rollback
53
+ end
54
+
55
+ def present(object, method: nil, options: {})
56
+ HC::Presentation.present(object, method: method, options: options)
57
+ end
58
+
59
+ def errors?
60
+ errors.count.positive?
61
+ end
62
+
63
+ def enqueue_job(job_class:, params:, current_user:)
64
+ HC::Jobs.enqueue(job_class: job_class, user: current_user, params: params)
65
+ end
66
+
67
+ # Abstracted execution instanciates class, sets variables, runs validations, and handles errors
68
+ #
69
+ def self.execute(args)
70
+
71
+ # Build the new service
72
+ #
73
+ service = new(args)
74
+
75
+ # Service executions should be wrapped in a transaction, and gracefully handle errors
76
+ #
77
+ ActiveRecord::Base.transaction do
78
+
79
+ # Validate service args
80
+ #
81
+ service.try(:before_validation)
82
+ return service unless service.valid?
83
+
84
+ # Attempt execution of service
85
+ #
86
+ begin
87
+ service.execute
88
+ rescue ActiveRecord::RecordInvalid => ex
89
+ if service.errors.blank?
90
+ service.errors.add(:validation, ex.record.errors)
91
+ service.manual_status_code = 422
92
+ end
93
+ raise ActiveRecord::Rollback
94
+ rescue ActiveRecord::Rollback => ex
95
+ raise ex
96
+ rescue StandardError => ex
97
+ Rails.logger.debug '================================='
98
+ Rails.logger.debug ex.inspect
99
+ Rails.logger.debug ex.backtrace.join("\n")
100
+ Rails.logger.debug '================================='
101
+ if service.errors.blank?
102
+ service.errors.add(:exception, 'An error occurred')
103
+ service.manual_status_code = 500
104
+ end
105
+ raise ActiveRecord::Rollback
106
+ end
107
+ end
108
+ return service
109
+
110
+ end
111
+
112
+ end
113
+
114
+ end
115
+ end
@@ -0,0 +1,5 @@
1
+ module HC
2
+ module Service
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hc-service
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jack Hayter
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-05-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Transforms objects and collections into hashes for rendering as JSON
28
+ email:
29
+ - jack@hockey-community.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - LICENSE.txt
37
+ - README.md
38
+ - Rakefile
39
+ - bin/console
40
+ - bin/setup
41
+ - hc-service.gemspec
42
+ - lib/hc/service.rb
43
+ - lib/hc/service/base.rb
44
+ - lib/hc/service/version.rb
45
+ homepage: https://github.com/HockeyCommunity/service
46
+ licenses:
47
+ - MIT
48
+ metadata: {}
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubygems_version: 3.0.3
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: A super simple object serializer
68
+ test_files: []