Skip to content

Commit

Permalink
First implementation of library
Browse files Browse the repository at this point in the history
  • Loading branch information
samstickland committed May 24, 2023
1 parent e5d8304 commit d2e5b73
Show file tree
Hide file tree
Showing 22 changed files with 451 additions and 55 deletions.
58 changes: 3 additions & 55 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,56 +1,4 @@
.bundle/
Gemfile.lock
*.gem
*.rbc
/.config
/coverage/
/InstalledFiles
/pkg/
/spec/reports/
/spec/examples.txt
/test/tmp/
/test/version_tmp/
/tmp/

# Used by dotenv library to load environment variables.
# .env

# Ignore Byebug command history file.
.byebug_history

## Specific to RubyMotion:
.dat*
.repl_history
build/
*.bridgesupport
build-iPhoneOS/
build-iPhoneSimulator/

## Specific to RubyMotion (use of CocoaPods):
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# vendor/Pods/

## Documentation cache and generated files:
/.yardoc/
/_yardoc/
/doc/
/rdoc/

## Environment normalization:
/.bundle/
/vendor/bundle
/lib/bundler/man/

# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# Gemfile.lock
# .ruby-version
# .ruby-gemset

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc

# Used by RuboCop. Remote config files pulled in from inherit_from directive.
# .rubocop-https?--*
gems
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
inherit_gem:
hubbado-style:
- default.yml
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.2
9 changes: 9 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Change Log
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

# [1.0.0 - 2023-05-23]
### Changed
- First release
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source "https://rubygems.org"
source "https://rubygems.pkg.github.com/hubbado"

gemspec name: "hubbado-log_ignored_message"

gem 'json' # Ruby 3.2 bug work-around
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Log Ignored Message

Using evt-log to log that an Eventide Message has been logged

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'hubbado-log-ignored-message'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install hubbado-log-ignored-message

## Usage

Use it inside an Eventide handler to log when a messge has been ignored

```ruby
class SomeHandler
dependency :log_ignored, LogIgnoredMessage

def configure
LogIgnoredMessage.configure(self)
end

handle SomeMessage do |some_message|
...
if some_entity.already_processed?(some_message)
log_ignored_message.(some_message, "already processed")

return
end
...
end
end
```

This will call:

```ruby
logger.info(
"SomeMessage #{some_message.metadata.global_position} ignored, " \
"already processed",
tag: :ignored
)
```
22 changes: 22 additions & 0 deletions hubbado-log_ignored-message.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Gem::Specification.new do |s|
s.name = "hubbado-log_ignored_message"
s.version = "1.0.0"
s.summary = "Logs that a Eventide message has been ignored "

s.authors = ["sam@hubbado.com"]
s.homepage = "https://github.com/hubbado/hubbado-log_ignored_message"
s.licenses = ["MIT"]

s.require_paths = ["lib"]
s.files = Dir.glob("{lib}/**/*")
s.platform = Gem::Platform::RUBY
s.required_ruby_version = ">= 3.0"

s.add_runtime_dependency 'evt-log'

s.add_development_dependency 'eventide-postgres'
s.add_development_dependency 'evt-identifier-uuid'
s.add_development_dependency 'evt-messaging'
s.add_development_dependency "hubbado-style"
s.add_development_dependency 'test_bench'
end
3 changes: 3 additions & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require_relative "load_path"

require "log_ignored_message"
94 changes: 94 additions & 0 deletions install-gems.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/usr/bin/env bash

set -euo pipefail

function boolean-var {
variable_name=$1
default=${2:-no}

val=${!variable_name:=$default}

if [[ "n|no|f|false|off|0" =~ $val ]]; then
echo 'false'
elif [[ "y|yes|t|true|on|1" =~ $val ]]; then
echo 'true'
else
echo "Variable \$$variable_name is set to \`$val' which is not a boolean value" >&2
echo >&2
exit 1
fi
}

if [ -z ${REMOVE_GEMS+x} ]; then
echo
echo "REMOVE_GEMS is not set. Using \"on\" by default."
remove_gems="on"
else
remove_gems=$REMOVE_GEMS
fi

gem_dir="./gems"

echo
echo "Install Gems"
echo "= = ="

if [ -z ${POSTURE+x} ]; then
echo "POSTURE is not set. Using \"operational\" by default."
posture="operational"
else
posture=$POSTURE
fi

echo "Posture: $posture"
echo "Gem Directory: $gem_dir"
echo "Remove Gems: $remove_gems"

echo

echo "Removing bundler configuration"
echo "- - -"

cmd="rm -rfv ./.bundle"

echo $cmd
($cmd)

echo
echo "Removing Gemfile.lock"
echo "- - -"

cmd="rm -fv Gemfile.lock"

echo $cmd
($cmd)

remove_gems=$(boolean-var remove_gems)

if $remove_gems; then
echo
echo "Removing installed gems"
echo "- - -"

cmd="rm -rf $gem_dir"

echo $cmd
($cmd)
fi

echo
echo "Installing bundle"
echo "- - -"

cmd="bundle install --standalone --path=./gems"

if [ operational == "$posture" ]; then
cmd="$cmd --without=development"
fi

echo $cmd
($cmd)

echo "- - -"
echo "(done)"
echo
46 changes: 46 additions & 0 deletions lib/log_ignored_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'configure'
require 'log'

class LogIgnoredMessage
extend Configure::Macro
include Log::Dependency

configure :log_ignored_message

def self.build
new
end

def call(message, reason)
logger.info(
"#{message.message_type} #{message.metadata.global_position} ignored, #{reason}",
tag: :ignored
)
end

module Substitute
def self.build
LogIgnored.new
end

class LogIgnored
def call(message, reason)
invocations << [message, reason]
end

def called?
invocations.any?
end

def called_with?(message, reason)
invocations.include? [message, reason]
end

private

def invocations
@invocations ||= []
end
end
end
end
6 changes: 6 additions & 0 deletions lib/log_ignored_message/controls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "eventide/postgres" # Requireed to define Read, which is used in messaging/controls
require "identifier/uuid/controls"
require "messaging/controls"

require "log_ignored_message/controls/message"
require "log_ignored_message/controls/reason"
15 changes: 15 additions & 0 deletions lib/log_ignored_message/controls/message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class LogIgnoredMessage
module Controls
module Message
def self.example
Messaging::Controls::Message.example
end

module Other
def self.example
Messaging::Controls::Message.example(some_attribute: 'other')
end
end
end
end
end
15 changes: 15 additions & 0 deletions lib/log_ignored_message/controls/reason.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class LogIgnoredMessage
module Controls
module Reason
def self.example
'already processed'
end

module Other
def self.example
'some other reason'
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/log_ignored_message/log_ignored_message
18 changes: 18 additions & 0 deletions load_path.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
bundler_standalone_loader = ENV["BUNDLER_STANDALONE_LOADER"] || "gems/bundler/setup"

begin
require_relative bundler_standalone_loader
rescue LoadError
warn "WARNING: Standalone bundle loader is not at #{bundler_standalone_loader}. Using Bundler to load gems."
require "bundler/setup"
Bundler.require
end

lib_dir = File.expand_path("lib", __dir__)
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)

libraries_dir = ENV["LIBRARIES_HOME"]
unless libraries_dir.nil?
libraries_dir = File.expand_path(libraries_dir)
$LOAD_PATH.unshift libraries_dir unless $LOAD_PATH.include?(libraries_dir)
end
Loading

0 comments on commit d2e5b73

Please sign in to comment.