Initial commit

This commit is contained in:
Senad Uka
2022-03-23 05:44:42 +01:00
parent 1405281a5c
commit eea10dd03b
113 changed files with 3617 additions and 81 deletions

26
.env Normal file
View File

@@ -0,0 +1,26 @@
STACK_NAME=local-dev
# Rails
LOG_LEVEL=debug
RAILS_ENV=production
RAILS_MAX_THREADS=5
PORT=5090
SECRET_KEY_BASE=5e24529bc64ff2fccfadb21b086d9a27caf03fc052190b2261c1e05e1f84f0ce783c5a4e5b93cb5748092882ddf0d6fd3ea2a57ba4bdfb25de704c790d4248f2
TMPDIR=/tmp/vendor-scheduler-service
# Persistence
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=postgres
# rabbitmq
RABBITMQ_HOST=rabbitmq
RABBITMQ_PORT=5672
RABBITMQ_VHOST=/
RABBITMQ_USER=roger
RABBITMQ_PASSWORD=rabbit
# onex
ONEX_TARGET_PROJECT_ID=1
ONEX_CENTRAL_URL=http://localhost:5070

8
.env.development Normal file
View File

@@ -0,0 +1,8 @@
# Rails
RAILS_ENV=development
# Persistence
DATABASE_NAME=vendor-scheduler-service-dev
# Vistar
VISTAR_BASE_URL=https://sandbox-api.vistarmedia.com

8
.env.test Normal file
View File

@@ -0,0 +1,8 @@
# Rails
RAILS_ENV=test
# Persistence
DATABASE_NAME=vendor-scheduler-service-test
# Vistar
VISTAR_BASE_URL=https://sandbox-api.vistarmedia.com

8
.gitattributes vendored Normal file
View File

@@ -0,0 +1,8 @@
# See https://git-scm.com/docs/gitattributes for more about git attribute files.
# Mark the database schema as having been generated.
db/schema.rb linguist-generated
# Mark any vendored files as having been vendored.
vendor/* linguist-vendored

27
.gitignore vendored Normal file
View File

@@ -0,0 +1,27 @@
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'
# Ignore bundler config.
/.bundle
# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
/coverage
/out
/public/assets
.byebug_history
# Ignore master key for decrypting credentials and more.
/config/master.key
# intellij
*.iml
.idea/
# environment files
.env.*local

3
.rspec Normal file
View File

@@ -0,0 +1,3 @@
--require spec_helper
--color

34
.rubocop.yml Normal file
View File

@@ -0,0 +1,34 @@
require: rubocop-rspec
Rails:
Enabled: True
AllCops:
TargetRubyVersion: 2.5
Exclude:
- 'bin/**/*'
- 'db/**/*'
- 'config/**/*'
- 'lib/templates/**/*'
- 'script/**/*'
Style/AccessModifierIndentation:
EnforcedStyle: outdent
Style/EmptyLinesAroundAccessModifier:
Enabled: false
Style/Documentation:
Enabled: false
Lint/Debugger:
Enabled: false
Metrics/LineLength:
Max: 140
Style/StringLiterals:
EnforcedStyle: single_quotes
SupportedStyles:
- single_quotes
- double_quotes

1
.ruby-version Normal file
View File

@@ -0,0 +1 @@
2.5.5

53
Gemfile Normal file
View File

@@ -0,0 +1,53 @@
# frozen_string_literal: true
#
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.5.5'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main'
gem 'rails', '~> 6.1.4', '>= 6.1.4.1'
# Use postgresql as the database for Active Record
gem 'pg', '~> 1.1'
# Use Puma as the app server
gem 'puma', '~> 5.0'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Config
gem 'dotenv-rails', require: 'dotenv/rails-now', groups: %i[development test]
gem 'settingslogic'
gem 'bunny'
gem 'foreman', '~> 0.87.0'
gem 'faraday'
gem 'rest-client'
source 'https://nexus.onsmartengineering.com/repository/rubygems-hosted' do
gem 'auth-client', '~> 2.1.0'
end
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
gem 'listen', '~> 3.3'
end
group :test do
gem 'database_cleaner'
gem 'factory_bot_rails', '~> 6.1.0'
gem 'rspec', '~> 3.10.0'
gem 'rspec-rails', '~> 4.1.0'
gem 'shoulda-matchers', '~> 4.0'
gem 'simplecov', require: false
gem 'simplecov-cobertura', require: false
gem 'webmock'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

292
Gemfile.lock Normal file
View File

@@ -0,0 +1,292 @@
GEM
remote: https://rubygems.org/
remote: https://nexus.onsmartengineering.com/repository/rubygems-hosted/
specs:
actioncable (6.1.4.1)
actionpack (= 6.1.4.1)
activesupport (= 6.1.4.1)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (6.1.4.1)
actionpack (= 6.1.4.1)
activejob (= 6.1.4.1)
activerecord (= 6.1.4.1)
activestorage (= 6.1.4.1)
activesupport (= 6.1.4.1)
mail (>= 2.7.1)
actionmailer (6.1.4.1)
actionpack (= 6.1.4.1)
actionview (= 6.1.4.1)
activejob (= 6.1.4.1)
activesupport (= 6.1.4.1)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (6.1.4.1)
actionview (= 6.1.4.1)
activesupport (= 6.1.4.1)
rack (~> 2.0, >= 2.0.9)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (6.1.4.1)
actionpack (= 6.1.4.1)
activerecord (= 6.1.4.1)
activestorage (= 6.1.4.1)
activesupport (= 6.1.4.1)
nokogiri (>= 1.8.5)
actionview (6.1.4.1)
activesupport (= 6.1.4.1)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activejob (6.1.4.1)
activesupport (= 6.1.4.1)
globalid (>= 0.3.6)
activemodel (6.1.4.1)
activesupport (= 6.1.4.1)
activerecord (6.1.4.1)
activemodel (= 6.1.4.1)
activesupport (= 6.1.4.1)
activestorage (6.1.4.1)
actionpack (= 6.1.4.1)
activejob (= 6.1.4.1)
activerecord (= 6.1.4.1)
activesupport (= 6.1.4.1)
marcel (~> 1.0.0)
mini_mime (>= 1.1.0)
activesupport (6.1.4.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
addressable (2.8.0)
public_suffix (>= 2.0.2, < 5.0)
amq-protocol (2.3.2)
auth-client (2.1.1)
builder (3.2.4)
bunny (2.19.0)
amq-protocol (~> 2.3, >= 2.3.1)
sorted_set (~> 1, >= 1.0.2)
byebug (11.1.3)
concurrent-ruby (1.1.9)
crack (0.4.5)
rexml
crass (1.0.6)
database_cleaner (2.0.1)
database_cleaner-active_record (~> 2.0.0)
database_cleaner-active_record (2.0.1)
activerecord (>= 5.a)
database_cleaner-core (~> 2.0.0)
database_cleaner-core (2.0.1)
diff-lcs (1.4.4)
docile (1.4.0)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
dotenv (2.7.6)
dotenv-rails (2.7.6)
dotenv (= 2.7.6)
railties (>= 3.2)
erubi (1.10.0)
factory_bot (6.1.0)
activesupport (>= 5.0.0)
factory_bot_rails (6.1.0)
factory_bot (~> 6.1.0)
railties (>= 5.0.0)
faraday (1.5.1)
faraday-em_http (~> 1.0)
faraday-em_synchrony (~> 1.0)
faraday-excon (~> 1.1)
faraday-httpclient (~> 1.0.1)
faraday-net_http (~> 1.0)
faraday-net_http_persistent (~> 1.1)
faraday-patron (~> 1.0)
multipart-post (>= 1.2, < 3)
ruby2_keywords (>= 0.0.4)
faraday-em_http (1.0.0)
faraday-em_synchrony (1.0.0)
faraday-excon (1.1.0)
faraday-httpclient (1.0.1)
faraday-net_http (1.0.1)
faraday-net_http_persistent (1.2.0)
faraday-patron (1.0.0)
ffi (1.15.4)
foreman (0.87.2)
globalid (0.5.2)
activesupport (>= 5.0)
hashdiff (1.0.1)
http-accept (1.7.0)
http-cookie (1.0.4)
domain_name (~> 0.5)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
listen (3.7.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
loofah (2.12.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.7.1)
mini_mime (>= 0.1.1)
marcel (1.0.2)
method_source (1.0.0)
mime-types (3.3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2021.0901)
mini_mime (1.1.1)
mini_portile2 (2.6.1)
minitest (5.14.4)
multipart-post (2.1.1)
netrc (0.11.0)
nio4r (2.5.8)
nokogiri (1.12.5)
mini_portile2 (~> 2.6.1)
racc (~> 1.4)
pg (1.2.3)
public_suffix (4.0.6)
puma (5.5.0)
nio4r (~> 2.0)
racc (1.5.2)
rack (2.2.3)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (6.1.4.1)
actioncable (= 6.1.4.1)
actionmailbox (= 6.1.4.1)
actionmailer (= 6.1.4.1)
actionpack (= 6.1.4.1)
actiontext (= 6.1.4.1)
actionview (= 6.1.4.1)
activejob (= 6.1.4.1)
activemodel (= 6.1.4.1)
activerecord (= 6.1.4.1)
activestorage (= 6.1.4.1)
activesupport (= 6.1.4.1)
bundler (>= 1.15.0)
railties (= 6.1.4.1)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.4.2)
loofah (~> 2.3)
railties (6.1.4.1)
actionpack (= 6.1.4.1)
activesupport (= 6.1.4.1)
method_source
rake (>= 0.13)
thor (~> 1.0)
rake (13.0.6)
rb-fsevent (0.11.0)
rb-inotify (0.10.1)
ffi (~> 1.0)
rbtree (0.4.4)
rest-client (2.1.0)
http-accept (>= 1.7.0, < 2.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
rexml (3.2.5)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
rspec-mocks (~> 3.10.0)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-mocks (3.10.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-rails (4.1.2)
actionpack (>= 4.2)
activesupport (>= 4.2)
railties (>= 4.2)
rspec-core (~> 3.10)
rspec-expectations (~> 3.10)
rspec-mocks (~> 3.10)
rspec-support (~> 3.10)
rspec-support (3.10.2)
ruby2_keywords (0.0.5)
sass-rails (6.0.0)
sassc-rails (~> 2.1, >= 2.1.1)
sassc (2.4.0)
ffi (~> 1.9)
sassc-rails (2.1.2)
railties (>= 4.0.0)
sassc (>= 2.0)
sprockets (> 3.0)
sprockets-rails
tilt
set (1.0.1)
settingslogic (2.0.9)
shoulda-matchers (4.5.1)
activesupport (>= 4.2.0)
simplecov (0.21.2)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-cobertura (1.4.2)
simplecov (~> 0.8)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.3)
sorted_set (1.0.3)
rbtree
set (~> 1.0)
sprockets (4.0.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.2.2)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
thor (1.1.0)
tilt (2.0.10)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
unf (0.1.4)
unf_ext
unf_ext (0.0.8)
webmock (3.13.0)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.4.2)
PLATFORMS
ruby
DEPENDENCIES
auth-client (~> 2.1.0)!
bunny
byebug
database_cleaner
dotenv-rails
factory_bot_rails (~> 6.1.0)
faraday
foreman (~> 0.87.0)
listen (~> 3.3)
pg (~> 1.1)
puma (~> 5.0)
rails (~> 6.1.4, >= 6.1.4.1)
rest-client
rspec (~> 3.10.0)
rspec-rails (~> 4.1.0)
sass-rails (>= 6)
settingslogic
shoulda-matchers (~> 4.0)
simplecov
simplecov-cobertura
tzinfo-data
webmock
RUBY VERSION
ruby 2.5.5p157
BUNDLED WITH
1.17.3

72
Jenkinsfile vendored Normal file
View File

@@ -0,0 +1,72 @@
@Library('jenkins-common') _
utils.initProperties()
def appName = 'vendor-scheduler'
def databaseName = 'vendor-scheduler'
def databaseHost = 'localhost'
def databasePort = '5432'
def databasePool = '10'
def databaseUser = 'postgres'
def databasePass = 'vaequ7Luah2gup7aaz3U'
testEnv = [
"DATABASE_NAME=${databaseName}",
"DATABASE_HOST=${databaseHost}",
"DATABASE_PORT=${databasePort}",
"DATABASE_POOL=${databasePool}",
"DATABASE_USERNAME=${databaseUser}",
"DATABASE_PASSWORD=${databasePass}",
"APP_NAME=${appName}",
"LOG_LEVEL=debug",
"RAILS_MAX_THREADS=5",
"PORT=5090",
"SECRET_KEY_BASE=abc123",
"TMPDIR=/tmp"
]
utils.onNode(nodeLabel: 'ubuntu-ruby-255') {
String version = utils.stagesGitCheckout()
stage('Save version') {
writeFile file: 'VERSION', text: version
}
stage('Run tests') {
withEnv(testEnv) {
sh '''
apt-get update
apt-get -y install postgresql-client libcurl3
gem install bundler --version=`sed -e '$!d' Gemfile.lock | xargs` --no-ri --no-rdoc
bundle install --path vendor --without production --with test development
docker run --rm --name postgres-${APP_NAME} -p${DATABASE_PORT}:5432 -e POSTGRES_PASSWORD=${DATABASE_PASSWORD} -d postgres:11
bundle exec rake db:create db:schema:load db:migrate RAILS_ENV=test
bundle exec rspec
docker stop postgres-${APP_NAME}
'''.stripIndent()
}
ruby.publishCoberturaReports()
}
//ruby.stagesRunCheckstyle() // TODO: reenable when we move to Ruby 2.2/later
stage('Create artifact for S3') {
withEnv([
"appName=${appName}",
"version=${version}"
]) {
sh '''
mkdir ${appName}-synced
rsync -av --exclude 'vendor' --exclude ${appName}-synced --exclude '.git' --include '*/' --include '*' . ${appName}-synced/
mv ${appName}-synced ${appName}
zip -r ${appName}-${version}.zip ${appName} -x '.git/*'
'''.stripIndent()
}
}
withAWS(credentials:'aws-s3') {
utils.stagesUploadArtifactToS3(includePathPattern:"${appName}*.zip", "${appName}", 'vle')
}
}

View File

@@ -1,92 +1,24 @@
# Vendor Scheduler Service
# README
This README would normally document whatever steps are necessary to get the
application up and running.
Things you may want to cover:
## Getting started
* Ruby version
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
* System dependencies
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
* Configuration
## Add your files
* Database creation
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
* Database initialization
```
cd existing_repo
git remote add origin https://gitlab.com/ukacorp/vozovi/vendor-scheduler-service.git
git branch -M main
git push -uf origin main
```
* How to run the test suite
## Integrate with your tools
* Services (job queues, cache servers, search engines, etc.)
- [ ] [Set up project integrations](https://gitlab.com/ukacorp/vozovi/vendor-scheduler-service/-/settings/integrations)
* Deployment instructions
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
* ...

6
Rakefile Normal file
View File

@@ -0,0 +1,6 @@
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require_relative "config/application"
Rails.application.load_tasks

View File

@@ -0,0 +1,2 @@
//= link_tree ../images
//= link_directory ../stylesheets .css

0
app/assets/images/.keep Normal file
View File

View File

@@ -0,0 +1,15 @@
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, or any plugin's
* vendor/assets/stylesheets directory can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
* files in this directory. Styles in this file should be added after the last require_* statement.
* It is generally better to create a new file per style scope.
*
*= require_tree .
*= require_self
*/

View File

@@ -0,0 +1,2 @@
class ApplicationController < ActionController::Base
end

View File

View File

@@ -0,0 +1,2 @@
module ApplicationHelper
end

0
app/javascript/.keep Normal file
View File

View File

@@ -0,0 +1,3 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end

View File

View File

@@ -0,0 +1,38 @@
# frozen_string_literal: true
module Amqp
class AmqpService
include Singleton
def queue(queue_name, options = {})
@queue ||= {}
@queue[queue_name] ||= channel.queue queue_name, options
end
def exchange(exchange_name, exchange_type, options = {})
@exchange ||= {}
@exchange[exchange_name] ||= channel.exchange exchange_name, options.merge(type: exchange_type)
end
def default_exchange
channel.default_exchange
end
private
def channel
@channel ||= connection.create_channel
end
def connection
@connection ||= begin
Bunny.new(amqp_config).tap do |conn|
conn.start
end
end
end
def amqp_config
AppConfig.amqp
end
end
end

View File

@@ -0,0 +1,48 @@
# frozen_string_literal: true
require 'auth/token_service'
# Manages tokens for this App
module Auth
class AuthTokenService
include Singleton
def refresh
@tokens = mutex.synchronize { obtain_tokens }
end
def access_token
ensure_tokens
@tokens[:access_token]
end
private
def ensure_tokens
@tokens ||= mutex.synchronize { obtain_tokens }
end
def obtain_tokens
token_source.fetch('client_credentials', client_id, client_secret)
end
def mutex
@mutex ||= Mutex.new
end
def token_source
@token_source ||= Auth::TokenService.new(auth_url)
end
def client_id
AppConfig.auth.client_id
end
def client_secret
AppConfig.auth.client_secret
end
def auth_url
URI(AppConfig.auth.url)
end
end
end

111
app/services/log/log.rb Normal file
View File

@@ -0,0 +1,111 @@
# frozen_string_literal: true
module Log
class Log
class LogFormatter < Logger::Formatter
attr_reader :options
def initialize(options = {})
@options = options
super() # superclass does not take parameters
end
def call(severity, timestamp, progname, msg)
super(severity, timestamp, progname, "#{log_tags}#{msg}")
end
def log_tags
tags = formatted_tags.join ' '
tags.blank? ? '' : "[#{tags}] "
end
private
def formatted_tags
elems = [Thread.current[:name].to_s]
elems += (options[:tags] || []).map { |e| e.is_a?(Proc) ? e.call : e }
elems.reject(&:blank?)
end
end
attr_reader :options # default = {}
# Options may include:
# tags: an array of static elements and/or callable Procs to include in log output
# filename: specifies filename for log in Rails log directory
# stream: specifies stream for log output (included for test support)
def initialize(options = {})
@options = options
end
def logger
@logger ||= begin
new_logger = Logger.new log_destination
new_logger.level = numeric_log_level
new_logger.formatter = LogFormatter.new options
new_logger
end
end
def numeric_log_level
AppConfig.numeric_log_level
end
# Returns filename for log or stream object if specified in options.
def log_destination
options[:stream].present? ? options[:stream] : log_filepath
end
def log_filepath
@log_filepath ||= log_dir.join log_filename
end
def log_filename
options[:filename].present? ? options[:filename].to_s : "#{Rails.env.to_s}.log"
end
def log_dir
@log_dir ||= begin
dir = Rails.root.join 'log'
FileUtils.mkdir_p dir
dir
end
end
# Same semantics as Log.error except uses current instance of logger.
def error(msg, exception = nil, options = {})
Log.log_error logger, msg, exception, options
end
# Same semantics as Log.raise_error except uses current instance of logger.
def raise_error(msg, exception, options = {})
error msg, exception, options
raise exception
end
class << self
# Logs and re-raises exception.
def raise_error (msg, exception, options = {})
error msg, exception, options
raise exception
end
# Logs error with exception backtrace detail if available. Always uses Rails.logger.
#
# Options may include:
# no_backtrace: true suppresses backtrace
#
# Returns formatted msg.
def error(msg, exception = nil, options = {})
log_error Rails.logger, msg, exception, options
end
# Same semantics as Log.error except uses specified logger.
def log_error(logger, msg, exception = nil, options = {})
message = exception.nil? ? msg : "Exception #{exception.class.name}: #{exception.inspect} #{msg}"
logger.error message
logger.error exception.backtrace if exception.try(:backtrace) && !options[:no_backtrace]
message
end
end
end
end

View File

@@ -0,0 +1,11 @@
# frozen_string_literal: true
require_relative 'logging'
module Log
module Loggable
def logger
Logging.instance.logger
end
end
end

View File

@@ -0,0 +1,11 @@
# frozen_string_literal: true
require 'singleton'
module Log
class Logging
include Singleton
attr_accessor :logger
end
end

View File

@@ -0,0 +1,13 @@
# frozen_string_literal: true
require_relative '../log/loggable'
module SchedulePipeline
class Errors
include Log::Loggable
def push(error)
logger.error error
end
end
end

View File

@@ -0,0 +1,56 @@
# frozen_string_literal: true
require_relative '../log/loggable'
require_relative '../vendors/broad_sign/broad_sign_tokens'
require_relative '../vendors/broad_sign/broad_sign_fetch_schedule'
require_relative '../vendors/vistar/vistar_tokens'
require_relative '../vendors/vistar/vistar_fetch_schedule'
module SchedulePipeline
class FetchSchedule
include Log::Loggable
def initialize(in_queue, out_queue, errors)
@in_queue = in_queue
@out_queue = out_queue
@errors = errors
end
def start
@in_queue.subscribe { |msg| process_msg(msg) }
end
def process_msg(msg)
logger.debug("fetch schedule: #{msg}")
vendor = msg[:vendor]
vendor_fetch = for_vendor(vendor)
unless vendor_fetch
@errors.push("FetchSchedule: Unknown vendor #{vendor}")
return
end
params = msg[:params]
fetched = vendor_fetch.call(params)
unless fetched
@errors.push("FetchSchedule: No ads returned from vendor #{vendor}")
return
end
Models::ScheduleProcessMsg.new(vendor, params[:player], fetched).push(@out_queue) if fetched
true
rescue StandardError => e
@errors.push(e.message)
end
private
def for_vendor(vendor)
vendor_map[vendor&.to_sym]
end
def vendor_map
@vendor_map ||= {
broad_sign: Vendors::BroadSign::BroadSignFetchSchedule.new(Vendors::BroadSign::BroadSignTokens.instance),
vistar: Vendors::Vistar::VistarFetchSchedule.new(Vendors::Vistar::VistarTokens.instance)
}
end
end
end

View File

@@ -0,0 +1,37 @@
# frozen_string_literal: true
module SchedulePipeline
module Models
class Schedule
attr_reader :name, :vendor, :player, :start_time, :items
def initialize(name, vendor, player, start_time, items)
@name = name
@vendor = vendor
@player = player
@start_time = start_time
@items = items
end
def to_hash
{
name: name,
vendor: vendor,
player: player,
start_time: start_time,
items: items.collect(&:to_hash)
}.with_indifferent_access
end
def self.from_hash(input)
self.new(
input[:name],
input[:vendor],
input[:player],
input[:start_time],
input[:items].collect { |i| ScheduleItem.from_hash(i) }
)
end
end
end
end

View File

@@ -0,0 +1,26 @@
# frozen_string_literal: true
module SchedulePipeline
module Models
class ScheduleFetchMsg
attr_reader :vendor, :params
def initialize(vendor, params)
@vendor = vendor
@params = params
end
def send(queue)
queue.push(serialized_msg)
end
private
def serialized_msg
{
vendor: @vendor,
params: @params
}
end
end
end
end

View File

@@ -0,0 +1,31 @@
# frozen_string_literal: true
module SchedulePipeline
module Models
class ScheduleItem
attr_reader :duration, :content_key, :pop_data
def initialize(duration, content_key, pop_data)
@duration = duration
@content_key = content_key
@pop_data = pop_data
end
def to_hash
{
duration: @duration,
content_key: @content_key,
pop_data: @pop_data
}.with_indifferent_access
end
def self.from_hash(input)
self.new(
input[:duration],
input[:content_key],
input[:pop_data]
)
end
end
end
end

View File

@@ -0,0 +1,35 @@
# frozen_string_literal: true
module SchedulePipeline
module Models
class ScheduleProcessMsg
attr_reader :vendor, :player, :vendor_schedule
def initialize(vendor, player, schedule)
@vendor = vendor
@player = player
@vendor_schedule = schedule.with_indifferent_access
end
def push(queue)
queue.push(to_hash)
end
def to_hash
{
vendor: @vendor,
player: @player,
vendor_schedule: @vendor_schedule
}
end
def self.from_hash(input)
self.new(
input[:vendor],
input[:player],
input[:vendor_schedule],
)
end
end
end
end

View File

@@ -0,0 +1,85 @@
# frozen_string_literal: true
require_relative '../log/loggable'
require_relative '../vle/vle_settings'
require_relative '../vle/vle_create_asset'
require_relative '../vle/vle_ingest_asset'
require_relative '../vendors/broad_sign/broad_sign_transform_schedule'
require_relative '../vendors/vistar/vistar_transform_schedule'
require_relative '../auth/auth_token_service'
require 'auth/client/request/auth_aware_request'
module SchedulePipeline
class ProcessSchedule
include Log::Loggable
def initialize(in_queue, out_queue, errors, tokens = Auth::AuthTokenService.instance)
@in_queue = in_queue
@out_queue = out_queue
@errors = errors
@tokens = tokens
end
def start
@in_queue.subscribe do |msg|
msg = Models::ScheduleProcessMsg.from_hash(msg) if msg.is_a? Hash
process_msg(msg)
end
end
def process_msg(msg)
logger.info("Process Schedule")
vendor = msg.vendor
schedule_transform = for_vendor(vendor)
unless schedule_transform
@errors.push("ProcessSchedule: Unknown vendor #{vendor}")
return
end
content_map = []
msg.vendor_schedule[:contents].each do |content|
# TODO: optimization: first check whether the content has already been ingested into VLE
vle_asset = create_asset(content)[:asset]
content_map << vle_asset
ingest_content(content, vle_asset[:meta][:presigned_url])
end
schedule = schedule_transform.call(vendor, msg.player, msg.vendor_schedule, content_map)
@out_queue.push(schedule)
true
rescue StandardError => e
@errors.push(e.message)
end
private
def create_asset(content)
sanitized_name = content[:name].gsub(/\s+/, '_')
Auth::Client::Request::AuthAwareRequest.new(
Vle::VleCreateAsset.new(
{
name: sanitized_name,
file: sanitized_name,
project_id: Vle::VleSettings.instance.target_project
}
),
@tokens
).call
end
def ingest_content(content, destination)
Vle::VleIngestAsset.new.call(content, destination)
end
def for_vendor(vendor)
vendor_map[vendor&.to_sym]
end
def vendor_map
@vendor_map ||= {
broad_sign: Vendors::BroadSign::BroadSignTransformSchedule.new,
vistar: Vendors::Vistar::VistarTransformSchedule.new
}
end
end
end

View File

@@ -0,0 +1,41 @@
# frozen_string_literal: true
require_relative '../log/loggable'
require_relative '../vle/vle_vendor_schedule'
require_relative 'models/schedule'
require_relative '../auth/auth_token_service'
require 'auth/client/request/auth_aware_request'
module SchedulePipeline
class PublishSchedule
include Log::Loggable
def initialize(queue, errors, tokens = Auth::AuthTokenService.instance)
@queue = queue
@errors = errors
@tokens = tokens
end
def start
@queue.subscribe do |msg|
msg = Models::Schedule.from_hash(msg) if msg.is_a? Hash
process_msg(msg)
end
end
def process_msg(msg)
logger.info("Publishing schedule: #{msg.name}...")
Auth::Client::Request::AuthAwareRequest.new(
Vle::VleVendorSchedule.new(
msg.vendor,
msg.player,
{ name: msg.name, start_time: msg.start_time, items: msg.to_hash[:items] }
),
@tokens
).call
logger.info("Published schedule: #{msg.name}")
rescue StandardError => e
@errors.push(e.message)
end
end
end

View File

@@ -0,0 +1,32 @@
# frozen_string_literal: true
require_relative '../../amqp/amqp_service'
module SchedulePipeline
module Queue
class BunnyQueue
def initialize(destination)
@destination = destination
end
def push(msg)
amqp.default_exchange.publish(msg.to_json, routing_key: @destination)
end
def subscribe(&block)
amqp_queue.subscribe do |_delivery_info, _metadata, payload|
block.call(JSON.parse(payload).with_indifferent_access)
end
end
private
def amqp
Amqp::AmqpService.instance
end
def amqp_queue
@queue ||= amqp.queue(@destination.to_s, auto_delete: false, durable: true)
end
end
end
end

View File

@@ -0,0 +1,15 @@
# frozen_string_literal: true
require_relative 'bunny_queue'
module SchedulePipeline
module Queue
class BunnyQueueFactory
include Singleton
def for_name(name)
BunnyQueue.new(name)
end
end
end
end

View File

@@ -0,0 +1,44 @@
# frozen_string_literal: true
require_relative '../log/loggable'
require_relative 'errors'
require_relative 'publish_schedule'
require_relative 'process_schedule'
require_relative 'fetch_schedule'
module SchedulePipeline
# Schedule Processing Pipeline
class SchedulePipeline
include Log::Loggable
def initialize(queue_factory)
errors = Errors.new
processed_schedules_queue = queue_factory.for_name(:processed_schedules)
publish_schedule = PublishSchedule.new(processed_schedules_queue, errors)
unprocessed_schedules_queue = queue_factory.for_name(:unprocessed_schedules)
process_schedule = ProcessSchedule.new(unprocessed_schedules_queue, processed_schedules_queue, errors)
fetch_schedule_queue = queue_factory.for_name(:fetch_vendor_schedules)
fetch_schedule = FetchSchedule.new(fetch_schedule_queue, unprocessed_schedules_queue, errors)
@queues = [processed_schedules_queue, unprocessed_schedules_queue, fetch_schedule_queue]
@stages = [publish_schedule, process_schedule, fetch_schedule]
end
def start
stages.each(&:start)
logger.info "Started Schedule Pipeline"
end
def stop
stages.reverse_each(&:stop)
end
private
def stages
@stages
end
def queues
@queues
end
end
end

View File

@@ -0,0 +1,57 @@
# frozen_string_literal: true
require_relative '../../log/loggable'
require_relative '../errors/schedule_fetch_error'
require 'net/http'
module Vendors
module BroadSign
class BroadSignFetchSchedule
include Log::Loggable
AIR_DOMAIN = 'air.broadsign.com'
PATH = 'playlist/v1/generate'
def initialize(tokens)
@tokens = tokens
end
def call(params)
params = ActiveSupport::HashWithIndifferentAccess.new(
screen: '1',
duration: 3600
).merge(params_whitelist(params))
logger.info "BroadSign fetch schedule request for player #{params[:player]}"
url = URI.join("https://#{AIR_DOMAIN}", PATH)
res = Net::HTTP.post(
url,
{
player_identifier: params[:player].to_s,
screen_identifier: params[:screen]&.to_s,
duration: "#{params[:duration]}s"
}.to_json,
{
"Authorization": "Bearer #{@tokens.auth_token}",
"Content-Type": 'application/json'
}
)
case res
when Net::HTTPSuccess
JSON.parse(res.body).with_indifferent_access
else
logger.error "Error fetching BroadSign Air schedule: #{res.code}"
raise Vendors::Errors::ScheduleFetchError, { player: params[:player], response_code: res.code }.to_json
end
end
private
def params_whitelist(params)
params.slice(
:player,
:screen,
:duration
)
end
end
end
end

View File

@@ -0,0 +1,15 @@
# frozen_string_literal: true
require_relative '../../log/loggable'
module Vendors
module BroadSign
class BroadSignTokens
include Singleton
def auth_token
AppConfig.get_mandatory 'vendors.broad_sign.token'
end
end
end
end

View File

@@ -0,0 +1,47 @@
# frozen_string_literal: true
require_relative '../../log/loggable'
require_relative '../../schedule_pipeline/models/schedule'
require_relative '../../schedule_pipeline/models/schedule_item'
require_relative '../errors/schedule_transform_error'
module Vendors
module BroadSign
class BroadSignTransformSchedule
include Log::Loggable
def call(vendor, player, schedule, content_map)
logger.debug("BroadSign schedule transform for vendor #{vendor}, player #{player}")
start_time = nil
# for performance reasons, here we truncate the items list to the first N items where N is the number of contents
# in the schedule. This assumes the schedule round-robins content!
truncate_schedule(schedule, content_map.size)
items = schedule[:items].collect do |item|
start_time ||= item[:startTime]
SchedulePipeline::Models::ScheduleItem.new(
item[:duration],
content_map[item[:contentIndex] || 0][:id],
# TODO: build full POP reporting url using this token
item[:token]
)
end
raise Vendors::Errors::ScheduleTransformError, 'Schedule without a start time' if start_time.nil?
SchedulePipeline::Models::Schedule.new(
"BroadSign schedule for player #{player}",
vendor,
player,
start_time,
items
)
end
private
def truncate_schedule(schedule, size)
schedule[:items] = schedule[:items].first(size)
end
end
end
end

View File

@@ -0,0 +1,7 @@
# frozen_string_literal: true
module Vendors
module Errors
class ContentIngestError < StandardError; end
end
end

View File

@@ -0,0 +1,7 @@
# frozen_string_literal: true
module Vendors
module Errors
class ScheduleFetchError < StandardError; end
end
end

View File

@@ -0,0 +1,7 @@
# frozen_string_literal: true
module Vendors
module Errors
class SchedulePublishError < StandardError; end
end
end

View File

@@ -0,0 +1,7 @@
# frozen_string_literal: true
module Vendors
module Errors
class ScheduleTransformError < StandardError; end
end
end

View File

@@ -0,0 +1,93 @@
# frozen_string_literal: true
require_relative '../../log/loggable'
require_relative '../errors/schedule_fetch_error'
require 'net/http'
module Vendors
module Vistar
class VistarFetchAd
include Log::Loggable
PATH = 'api/v1/get_ad/json'
DEFAULT_SUPPORTED_MEDIA = %w[
application/x-shockwave-dynamic-flash
application/x-shockwave-flash
image/jpeg
image/png
video/mp4
video/mpeg
video/mpg
video/quicktime
video/webm
video/x-flv
video/x-ms-wmv
video/x-msvideo
]
def initialize(tokens)
@tokens = tokens
end
def call(params)
logger.info "Vistar fetch Ad request"
res = Net::HTTP.post(
Vendors::Vistar::VistarSettings.instance.vistar_url(PATH),
body(params),
{
"Content-Type": 'application/json'
}
)
case res
when Net::HTTPSuccess
JSON.parse(res.body).with_indifferent_access
else
logger.error "Error fetching Vistar Ad: #{res.code}"
raise Vendors::Errors::ScheduleFetchError, { response_code: res.code }.to_json
end
end
private
def body(params)
display_area = (params[:display_area] || [{}]).each_with_index.collect do |item, idx|
{
id: "display-#{idx}",
width: 1080,
height: 1920,
supported_media: DEFAULT_SUPPORTED_MEDIA,
static_duration: 8
}.merge(item)
end
params_whitelist(params).merge({
network_id: Vendors::Vistar::VistarSettings.instance.network_id,
api_key: "#{@tokens.api_key}",
direct_connection: false,
display_area: display_area
}).to_json
end
def params_whitelist(params)
params.slice(
:device_id,
:venue_id,
:display_time,
:device_attribute,
:name,
:display_area,
:id,
:width,
:height,
:allow_audio,
:supported_media,
:min_duration,
:max_duration,
:order_id,
:max_file_size_bytes,
:static_duration,
:latitude,
:longitude
)
end
end
end
end

View File

@@ -0,0 +1,37 @@
module Vendors
module Vistar
class VistarFetchSchedule
def initialize(tokens)
@tokens = tokens
end
def call(params)
fetch = fetch_ad(params)
return nil unless fetch && fetch[:advertisement]
ad = fetch[:advertisement][0]
{
contents: [
{
name: "vistar_asset_#{ad[:asset_id]}",
url: ad[:asset_url]
}
],
startTime: Time.at(ad[:display_time]).to_datetime,
items: [
{
contentIndex: 0,
duration: "#{ad[:length_in_seconds]}s",
pop_url: ad[:proof_of_play_url]
}
]
}
end
private
def fetch_ad(params)
Vendors::Vistar::VistarFetchAd.new(@tokens).call(params)
end
end
end
end

View File

@@ -0,0 +1,20 @@
# frozen_string_literal: true
module Vendors
module Vistar
class VistarSettings
include Singleton
def vistar_url(path)
URI::join(
AppConfig.get_mandatory('vendors.vistar.base_url'),
path
)
end
def network_id
AppConfig.get_mandatory('vendors.vistar.network_id')
end
end
end
end

View File

@@ -0,0 +1,15 @@
# frozen_string_literal: true
require_relative '../../log/loggable'
module Vendors
module Vistar
class VistarTokens
include Singleton
def api_key
AppConfig.get_mandatory 'vendors.vistar.api_key'
end
end
end
end

View File

@@ -0,0 +1,35 @@
# frozen_string_literal: true
require_relative '../../log/loggable'
require_relative '../../schedule_pipeline/models/schedule'
require_relative '../../schedule_pipeline/models/schedule_item'
require_relative '../errors/schedule_transform_error'
module Vendors
module Vistar
class VistarTransformSchedule
include Log::Loggable
def call(vendor, player, schedule, content_map)
logger.debug("Vistar schedule transform for vendor #{vendor}, player #{player}")
start_time = schedule[:startTime]
items = schedule[:items].collect do |item|
SchedulePipeline::Models::ScheduleItem.new(
item[:duration],
content_map[item[:contentIndex] || 0][:id],
item[:pop_url]
)
end
raise Vendors::Errors::ScheduleTransformError, 'Schedule without a start time' if start_time.nil?
SchedulePipeline::Models::Schedule.new(
"Vistar schedule for player #{player}",
vendor,
player,
start_time,
items
)
end
end
end
end

View File

@@ -0,0 +1,52 @@
# frozen_string_literal: true
require_relative '../log/loggable'
require_relative '../vendors/errors/content_ingest_error'
require_relative 'vle_settings'
module Vle
class VleCreateAsset
include Log::Loggable
PATH = 'v1/assets'
def initialize(asset)
@asset = asset
end
def url
Vle::VleSettings.instance.central_url(PATH)
end
def call(access_token)
logger.info("ONEX create asset: #{asset}")
res = Net::HTTP.post(
url,
{
file: asset[:file],
name: asset[:name],
project_id: asset[:project_id],
description_short: "vendor-scheduler-service content: #{asset[:name]}"
}.to_json,
{
"Authorization": "Bearer #{access_token}",
"Content-Type": 'application/json'
}
)
case res
when Net::HTTPSuccess
JSON.parse(res.body).with_indifferent_access
when Net::HTTPUnauthorized
raise Auth::Client::Errors::Unauthorized, 'Unauthorized'
else
logger.error "Error creating asset in VLE: #{res.code}"
raise Vendors::Errors::ContentIngestError
end
end
private
def asset
@asset
end
end
end

View File

@@ -0,0 +1,35 @@
# frozen_string_literal: true
require 'rest-client'
require_relative '../log/loggable'
require_relative '../vendors/errors/content_ingest_error'
module Vle
class VleIngestAsset
include Log::Loggable
def call(content, destination)
logger.info("ONEX asset ingest: #{content}")
tmp_file = Tempfile.new('vendor-scheduler-service-content-ingest')
begin
source = content[:url] || content[:uri]
IO.copy_stream(URI.parse(source).open, tmp_file.path)
logger.debug("Content retrieved. Now starting ONEX ingest...")
res = RestClient.put(destination, tmp_file, { 'Content-Type': "binary/octet-stream" })
case res.code
when 200 #Net::HTTPSuccess
logger.debug("ONEX Asset ingested.")
true
else
logger.error "Error ingesting content into ONEX: POST returned #{res.code}"
raise Vendors::Errors::ContentIngestError
end
rescue StandardError => e
logger.error "Error ingesting content into ONEX: #{e.message}"
raise Vendors::Errors::ContentIngestError
ensure
tmp_file.close!
end
end
end
end

View File

@@ -0,0 +1,25 @@
# frozen_string_literal: true
module Vle
class VleSettings
include Singleton
def target_project
AppConfig.safe_get('onex.target_project_id') || '1'
end
def central_url(path)
URI::join(
AppConfig.safe_get('onex.central_url') || 'http://localhost:5070',
path
)
end
def scheduler_url(path)
URI::join(
AppConfig.safe_get('onex.scheduler_url') || 'http://localhost:5040',
path
)
end
end
end

View File

@@ -0,0 +1,46 @@
# frozen_string_literal: true
require_relative '../log/loggable'
require_relative '../vendors/errors/schedule_publish_error'
module Vle
class VleVendorSchedule
include Log::Loggable
PATH = 'v1/vendor_schedule'
def initialize(vendor, player, schedule)
@vendor = vendor
@player = player
@schedule = schedule
end
def url
Vle::VleSettings.instance.scheduler_url(PATH)
end
def call(access_token)
res = Net::HTTP.post(
url,
{
vendor: @vendor,
player: @player,
schedule: @schedule
}.to_json,
{
"Authorization": "Bearer #{access_token}",
"Content-Type": 'application/json'
}
)
case res
when Net::HTTPSuccess
JSON.parse(res.body).with_indifferent_access
when Net::HTTPUnauthorized
raise Auth::Client::Errors::Unauthorized, 'Unauthorized'
else
logger.error "Error (#{res.code}) scheduling vendor content in ONEX: #{res.body}"
raise Vendors::Errors::SchedulePublishError
end
end
end
end

View File

@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title>VendorSchedulerService</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all' %>
</head>
<body>
<%= yield %>
</body>
</html>

105
bin/bundle Executable file
View File

@@ -0,0 +1,105 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
#
# This file was generated by Bundler.
#
# The application 'bundle' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require "rubygems"
m = Module.new do
module_function
def invoked_as_script?
File.expand_path($0) == File.expand_path(__FILE__)
end
def env_var_version
ENV["BUNDLER_VERSION"]
end
def cli_arg_version
return unless invoked_as_script? # don't want to hijack other binstubs
return unless "update".start_with?(ARGV.first || " ") # must be running `bundle update`
bundler_version = nil
update_index = nil
ARGV.each_with_index do |a, i|
if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN
bundler_version = a
end
next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/
bundler_version = $1 || ">= 0.a"
update_index = i
end
bundler_version
end
def gemfile
gemfile = ENV["BUNDLE_GEMFILE"]
return gemfile if gemfile && !gemfile.empty?
File.expand_path("../../Gemfile", __FILE__)
end
def lockfile
lockfile =
case File.basename(gemfile)
when "gems.rb" then gemfile.sub(/\.rb$/, gemfile)
else "#{gemfile}.lock"
end
File.expand_path(lockfile)
end
def lockfile_version
return unless File.file?(lockfile)
lockfile_contents = File.read(lockfile)
return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/
Regexp.last_match(1)
end
def bundler_version
@bundler_version ||= begin
env_var_version || cli_arg_version ||
lockfile_version || "#{Gem::Requirement.default}.a"
end
end
def load_bundler!
ENV["BUNDLE_GEMFILE"] ||= gemfile
# must dup string for RG < 1.8 compatibility
activate_bundler(bundler_version.dup)
end
def activate_bundler(bundler_version)
if Gem::Version.correct?(bundler_version) && Gem::Version.new(bundler_version).release < Gem::Version.new("2.0")
bundler_version = "< 2"
end
gem_error = activation_error_handling do
gem "bundler", bundler_version
end
return if gem_error.nil?
require_error = activation_error_handling do
require "bundler/version"
end
return if require_error.nil? && Gem::Requirement.new(bundler_version).satisfied_by?(Gem::Version.new(Bundler::VERSION))
warn "Activating bundler (#{bundler_version}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_version}'`"
exit 42
end
def activation_error_handling
yield
nil
rescue StandardError, LoadError => e
e
end
end
m.load_bundler!
if m.invoked_as_script?
load Gem.bin_path("bundler", "bundle")
end

4
bin/rails Executable file
View File

@@ -0,0 +1,4 @@
#!/usr/bin/env ruby
APP_PATH = File.expand_path('../config/application', __dir__)
require_relative "../config/boot"
require "rails/commands"

4
bin/rake Executable file
View File

@@ -0,0 +1,4 @@
#!/usr/bin/env ruby
require_relative "../config/boot"
require "rake"
Rake.application.run

33
bin/setup Executable file
View File

@@ -0,0 +1,33 @@
#!/usr/bin/env ruby
require "fileutils"
# path to your application root.
APP_ROOT = File.expand_path('..', __dir__)
def system!(*args)
system(*args) || abort("\n== Command #{args} failed ==")
end
FileUtils.chdir APP_ROOT do
# This script is a way to set up or update your development environment automatically.
# This script is idempotent, so that you can run it at any time and get an expectable outcome.
# Add necessary setup steps to this file.
puts '== Installing dependencies =='
system! 'gem install bundler --conservative'
system('bundle check') || system!('bundle install')
# puts "\n== Copying sample files =="
# unless File.exist?('config/database.yml')
# FileUtils.cp 'config/database.yml.sample', 'config/database.yml'
# end
puts "\n== Preparing database =="
system! 'bin/rails db:prepare'
puts "\n== Removing old logs and tempfiles =="
system! 'bin/rails log:clear tmp:clear'
puts "\n== Restarting application server =="
system! 'bin/rails restart'
end

6
config.ru Normal file
View File

@@ -0,0 +1,6 @@
# This file is used by Rack-based servers to start the application.
require_relative "config/environment"
run Rails.application
Rails.application.load_server

59
config/application.rb Normal file
View File

@@ -0,0 +1,59 @@
require_relative "boot"
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
# require "active_job/railtie"
require "active_record/railtie"
# require "active_storage/engine"
require "action_controller/railtie"
# require "action_mailer/railtie"
# require "action_mailbox/engine"
# require "action_text/engine"
require "action_view/railtie"
# require "action_cable/engine"
require "sprockets/railtie"
require "rails/test_unit/railtie"
require_relative 'initializers/app_config'
require_relative '../app/services/log/log'
require_relative '../app/services/log/logging'
require 'auth/log'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module VendorSchedulerService
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 6.1
# Configuration for the application, engines, and railties goes here.
#
# These settings can be overridden in specific environments using the files
# in config/environments, which are processed later.
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")
# Don't generate system test files.
config.generators.system_tests = nil
# Logging
config.log_level = AppConfig.log_level
config.logger = Log::Log.new.logger
config.logger.info "** vendor-scheduler-service started at #{Time.now.utc} within process #{ Process.pid } **"
Rails.logger = config.logger
Log::Logging.instance.logger = config.logger
Auth::Log.instance.logger = config.logger
# Configure generators
config.generators do |g|
g.test_framework :rspec
g.template_engine false
g.assets false
g.helper false
end
end
end

35
config/application.yml Normal file
View File

@@ -0,0 +1,35 @@
common: &common
tmpdir: <%= ENV['TMPDIR'] %>
stack_name: <%= ENV['STACK_NAME'] %>
log:
level: <%= ENV['LOG_LEVEL'] %>
amqp:
host: <%= ENV['RABBITMQ_HOST'] %>
port: <%= ENV['RABBITMQ_PORT'] %>
vhost: <%= ENV['RABBITMQ_VHOST'] %>
user: <%= ENV['RABBITMQ_USER'] %>
pass: <%= ENV['RABBITMQ_PASSWORD'] %>
auth:
url: <%= ENV['AUTH_URL'] %>
client_id: <%= ENV['AUTH_CLIENT_ID'] %>
client_secret: <%= ENV['AUTH_CLIENT_SECRET'] %>
onex:
target_project_id: <%= ENV['ONEX_TARGET_PROJECT_ID'] %>
central_url: <%= ENV['ONEX_CENTRAL_URL'] %>
scheduler_url: <%= ENV['ONEX_SCHEDULER_URL'] %>
vendors:
broad_sign:
token: <%= ENV['BROAD_SIGN_TOKEN'] %>
vistar:
base_url: <%= ENV['VISTAR_BASE_URL'] %>
network_id: <%= ENV['VISTAR_NETWORK_ID'] %>
api_key: <%= ENV['VISTAR_API_KEY'] %>
production:
<<: *common
development:
<<: *common
test:
<<: *common

3
config/boot.rb Normal file
View File

@@ -0,0 +1,3 @@
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
require "bundler/setup" # Set up gems listed in the Gemfile.

View File

@@ -0,0 +1 @@
55Rdn1ayK+N3mKSDjSzhLwzFh1EyhmzuXK3CKpg5Z7hFJbuWV0Jy3q9YLdPLkl2nbc3G82DxGfunXfkaYgrXBGlVKcvAMhJ6dcN3Lsr3/0pfQM3ICxa9gYaSYMGuv9HNq6a6OvNSNMR1WT2G8xovBiuReqCdmhxg26deggNqRg+sdH+vRxXjC9cpAyY0HgMAAmoDUf0x85iWaANIVaBGcGCglAYPzpktJcGRo/ZhzUI5ECQcN6lB30j9CLldWDMaYdFM5MRzlMwng3frZlw4SeWt0OAygrxH3pmq0mLs4OHKJZVp/HHvJbn27rpePN1u12d0iTzeDH2YL3UWdbbh2mzWgiBUdR30JcpuBW105fZ36Pj751Pr5kF1KU/aIYXVbLUYwEtJ/7DMu6+QL8bqAmR7oKu5xaTxjyHe--BdT+NwAvRSIcMKlG--BMm7AGk+ykT6UjdapIDuZg==

91
config/database.yml Normal file
View File

@@ -0,0 +1,91 @@
# PostgreSQL. Versions 9.3 and up are supported.
#
# Install the pg driver:
# gem install pg
# On macOS with Homebrew:
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On macOS with MacPorts:
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
adapter: postgresql
encoding: unicode
database: <%= ENV.fetch('DATABASE_NAME') %>
host: <%= ENV.fetch('DATABASE_HOST') %>
port: <%= ENV.fetch('DATABASE_PORT') %>
username: <%= ENV.fetch('DATABASE_USERNAME') %>
password: <%= ENV.fetch('DATABASE_PASSWORD') %>
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: vendor_scheduler_development
# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user running Rails.
#username: vendor_scheduler
# The password associated with the postgres role (username).
#password:
# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
#host: localhost
# The TCP port the server listens on. Defaults to 5432.
# If your server runs on a different port number, change accordingly.
#port: 5432
# Schema search path. The server defaults to $user,public
#schema_search_path: myapp,sharedapp,public
# Minimum log levels, in increasing order:
# debug5, debug4, debug3, debug2, debug1,
# log, notice, warning, error, fatal, and panic
# Defaults to warning.
#min_messages: notice
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: vendor_scheduler_test
# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password or a full connection URL as an environment
# variable when you boot the app. For example:
#
# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
# production:
# url: <%= ENV['MY_APP_DATABASE_URL'] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
production:
<<: *default
database: vendor_scheduler_production
username: vendor_scheduler
password: <%= ENV['VENDOR_SCHEDULER_DATABASE_PASSWORD'] %>

5
config/environment.rb Normal file
View File

@@ -0,0 +1,5 @@
# Load the Rails application.
require_relative "application"
# Initialize the Rails application.
Rails.application.initialize!

View File

@@ -0,0 +1,68 @@
require "active_support/core_ext/integer/time"
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded any time
# it changes. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports.
config.consider_all_requests_local = true
# Enable/disable caching. By default caching is disabled.
# Run rails dev:cache to toggle caching.
if Rails.root.join('tmp', 'caching-dev.txt').exist?
config.action_controller.perform_caching = true
config.action_controller.enable_fragment_cache_logging = true
config.cache_store = :memory_store
config.public_file_server.headers = {
'Cache-Control' => "public, max-age=#{2.days.to_i}"
}
else
config.action_controller.perform_caching = false
config.cache_store = :null_store
end
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise exceptions for disallowed deprecations.
config.active_support.disallowed_deprecation = :raise
# Tell Active Support which deprecation messages to disallow.
config.active_support.disallowed_deprecation_warnings = []
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
# Highlight code that triggered database queries in logs.
config.active_record.verbose_query_logs = true
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
# Suppress logger output for asset requests.
config.assets.quiet = true
# Raises error for missing translations.
# config.i18n.raise_on_missing_translations = true
# Annotate rendered view with file names.
# config.action_view.annotate_rendered_view_with_filenames = true
# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
# Uncomment if you wish to allow Action Cable access from any origin.
# config.action_cable.disable_request_forgery_protection = true
end

View File

@@ -0,0 +1,103 @@
require "active_support/core_ext/integer/time"
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# Code is not reloaded between requests.
config.cache_classes = true
# Eager load code on boot. This eager loads most of Rails and
# your application in memory, allowing both threaded web servers
# and those relying on copy on write to perform better.
# Rake tasks automatically ignore this option for performance.
config.eager_load = true
# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"]
# or in config/master.key. This key is used to decrypt credentials (and other encrypted files).
# config.require_master_key = true
# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
# Compress CSS using a preprocessor.
# config.assets.css_compressor = :sass
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = false
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
# config.asset_host = 'http://assets.example.com'
# Specifies the header that your server uses for sending files.
# config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
# Include generic and useful information about system operation, but avoid logging too much
# information to avoid inadvertent exposure of personally identifiable information (PII).
config.log_level = :info
# Prepend all log lines with the following tags.
config.log_tags = [ :request_id ]
# Use a different cache store in production.
# config.cache_store = :mem_cache_store
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation cannot be found).
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners.
config.active_support.deprecation = :notify
# Log disallowed deprecations.
config.active_support.disallowed_deprecation = :log
# Tell Active Support which deprecation messages to disallow.
config.active_support.disallowed_deprecation_warnings = []
# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new
# Use a different logger for distributed setups.
# require "syslog/logger"
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')
if ENV["RAILS_LOG_TO_STDOUT"].present?
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
end
# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false
# Inserts middleware to perform automatic connection switching.
# The `database_selector` hash is used to pass options to the DatabaseSelector
# middleware. The `delay` is used to determine how long to wait after a write
# to send a subsequent read to the primary.
#
# The `database_resolver` class is used by the middleware to determine which
# database is appropriate to use based on the time delay.
#
# The `database_resolver_context` class is used by the middleware to set
# timestamps for the last write to the primary. The resolver uses the context
# class timestamps to determine how long to wait before reading from the
# replica.
#
# By default Rails will store a last write timestamp in the session. The
# DatabaseSelector middleware is designed as such you can define your own
# strategy for connection switching and pass that into the middleware through
# these configuration options.
# config.active_record.database_selector = { delay: 2.seconds }
# config.active_record.database_resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver
# config.active_record.database_resolver_context = ActiveRecord::Middleware::DatabaseSelector::Resolver::Session
end

View File

@@ -0,0 +1,49 @@
require "active_support/core_ext/integer/time"
# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
config.cache_classes = true
# Do not eager load code on boot. This avoids loading your whole application
# just for the purpose of running a single test. If you are using a tool that
# preloads Rails for running tests, you may have to set it to true.
config.eager_load = false
# Configure public file server for tests with Cache-Control for performance.
config.public_file_server.enabled = true
config.public_file_server.headers = {
'Cache-Control' => "public, max-age=#{1.hour.to_i}"
}
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.cache_store = :null_store
# Raise exceptions instead of rendering exception templates.
config.action_dispatch.show_exceptions = false
# Disable request forgery protection in test environment.
config.action_controller.allow_forgery_protection = false
# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr
# Raise exceptions for disallowed deprecations.
config.active_support.disallowed_deprecation = :raise
# Tell Active Support which deprecation messages to disallow.
config.active_support.disallowed_deprecation_warnings = []
# Raises error for missing translations.
# config.i18n.raise_on_missing_translations = true
# Annotate rendered view with file names.
# config.action_view.annotate_rendered_view_with_filenames = true
end

View File

@@ -0,0 +1,45 @@
require 'settingslogic'
require 'uri'
# Set application config
class AppConfig < Settingslogic
source "#{Dir.pwd}/config/application.yml"
namespace Rails.env
suppress_errors true
#
# Gets the log level as a symbol (one of :debug, :info, :warn, :error, etc.)
#
def self.log_level(default = :info)
self.log.level.to_sym rescue default
end
#
# Gets the log level as an integer (debug: 0, info: 1, etc.)
#
def self.numeric_log_level(default = Logger::INFO)
Logger.const_get(log_level(nil).upcase) rescue default
end
# Safe retrieval of an option value with options:
# :default specifies default value
# :warn_if_missing true: emits warning to logger if setting missing
# :integer true: forces default value if setting is not an integer
def self.safe_get(attr, options = {})
val = attr.to_s.split('.').reduce(self) { |config, attr| config.send attr }
raise if options[:integer] && val.to_i.to_s != val
val
rescue
default = options[:default]
logger.warn("No config for #{attr}. #{ default ? "Using default: #{default}" : 'No default'}") if options[:warn_if_missing]
default
end
def self.get_mandatory(attr)
safe_get(attr) || raise(StandardError, "No such setting #{attr}")
end
def self.logger
Rails.logger
end
end

View File

@@ -0,0 +1,8 @@
# Be sure to restart your server when you modify this file.
# ActiveSupport::Reloader.to_prepare do
# ApplicationController.renderer.defaults.merge!(
# http_host: 'example.org',
# https: false
# )
# end

View File

@@ -0,0 +1,12 @@
# Be sure to restart your server when you modify this file.
# Version of your assets, change this if you want to expire all your assets.
Rails.application.config.assets.version = '1.0'
# Add additional assets to the asset load path.
# Rails.application.config.assets.paths << Emoji.images_path
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in the app/assets
# folder are already added.
# Rails.application.config.assets.precompile += %w( admin.js admin.css )

View File

@@ -0,0 +1,8 @@
# Be sure to restart your server when you modify this file.
# You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
# Rails.backtrace_cleaner.add_silencer { |line| /my_noisy_library/.match?(line) }
# You can also remove all the silencers if you're trying to debug a problem that might stem from framework code
# by setting BACKTRACE=1 before calling your invocation, like "BACKTRACE=1 ./bin/rails runner 'MyClass.perform'".
Rails.backtrace_cleaner.remove_silencers! if ENV["BACKTRACE"]

View File

@@ -0,0 +1,28 @@
# Be sure to restart your server when you modify this file.
# Define an application-wide content security policy
# For further information see the following documentation
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
# Rails.application.config.content_security_policy do |policy|
# policy.default_src :self, :https
# policy.font_src :self, :https, :data
# policy.img_src :self, :https, :data
# policy.object_src :none
# policy.script_src :self, :https
# policy.style_src :self, :https
# # Specify URI for violation reports
# # policy.report_uri "/csp-violation-report-endpoint"
# end
# If you are using UJS then enable automatic nonce generation
# Rails.application.config.content_security_policy_nonce_generator = -> request { SecureRandom.base64(16) }
# Set the nonce only to specific directives
# Rails.application.config.content_security_policy_nonce_directives = %w(script-src)
# Report CSP violations to a specified URI
# For further information see the following documentation:
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy-Report-Only
# Rails.application.config.content_security_policy_report_only = true

View File

@@ -0,0 +1,5 @@
# Be sure to restart your server when you modify this file.
# Specify a serializer for the signed and encrypted cookie jars.
# Valid options are :json, :marshal, and :hybrid.
Rails.application.config.action_dispatch.cookies_serializer = :json

View File

@@ -0,0 +1,6 @@
# Be sure to restart your server when you modify this file.
# Configure sensitive parameters which will be filtered from the log file.
Rails.application.config.filter_parameters += [
:passw, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn
]

View File

@@ -0,0 +1,16 @@
# Be sure to restart your server when you modify this file.
# Add new inflection rules using the following format. Inflections
# are locale specific, and you may define rules for as many different
# locales as you wish. All of these examples are active by default:
# ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.plural /^(ox)$/i, '\1en'
# inflect.singular /^(ox)en/i, '\1'
# inflect.irregular 'person', 'people'
# inflect.uncountable %w( fish sheep )
# end
# These inflection rules are supported but not enabled by default:
# ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.acronym 'RESTful'
# end

View File

@@ -0,0 +1,4 @@
# Be sure to restart your server when you modify this file.
# Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf

View File

@@ -0,0 +1,11 @@
# Define an application-wide HTTP permissions policy. For further
# information see https://developers.google.com/web/updates/2018/06/feature-policy
#
# Rails.application.config.permissions_policy do |f|
# f.camera :none
# f.gyroscope :none
# f.microphone :none
# f.usb :none
# f.fullscreen :self
# f.payment :self, "https://secure.example.com"
# end

View File

@@ -0,0 +1,8 @@
# starts the schedule processing pipeline
return if Rails.env.test?
require_relative '../../app/services/schedule_pipeline/schedule_pipeline'
require_relative '../../app/services/schedule_pipeline/queue/bunny_queue_factory'
SchedulePipeline::SchedulePipeline.new(SchedulePipeline::Queue::BunnyQueueFactory.instance).start

View File

@@ -0,0 +1,14 @@
# Be sure to restart your server when you modify this file.
# This file contains settings for ActionController::ParamsWrapper which
# is enabled by default.
# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
ActiveSupport.on_load(:action_controller) do
wrap_parameters format: [:json]
end
# To enable root element in JSON for ActiveRecord objects.
# ActiveSupport.on_load(:active_record) do
# self.include_root_in_json = true
# end

33
config/locales/en.yml Normal file
View File

@@ -0,0 +1,33 @@
# Files in the config/locales directory are used for internationalization
# and are automatically loaded by Rails. If you want to use locales other
# than English, add the necessary files in this directory.
#
# To use the locales, use `I18n.t`:
#
# I18n.t 'hello'
#
# In views, this is aliased to just `t`:
#
# <%= t('hello') %>
#
# To use a different locale, set it with `I18n.locale`:
#
# I18n.locale = :es
#
# This would use the information in config/locales/es.yml.
#
# The following keys must be escaped otherwise they will not be retrieved by
# the default I18n backend:
#
# true, false, on, off, yes, no
#
# Instead, surround them with single quotes.
#
# en:
# 'true': 'foo'
#
# To learn more, please read the Rails Internationalization guide
# available at https://guides.rubyonrails.org/i18n.html.
en:
hello: "Hello world"

43
config/puma.rb Normal file
View File

@@ -0,0 +1,43 @@
# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers: a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum; this matches the default thread size of Active Record.
#
max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
threads min_threads_count, max_threads_count
# Specifies the `worker_timeout` threshold that Puma will use to wait before
# terminating a worker in development environments.
#
worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development"
# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
port ENV.fetch("PORT") { 5090 }
# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "development" }
# Specifies the `pidfile` that Puma will use.
pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }
# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked web server processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
# workers ENV.fetch("WEB_CONCURRENCY") { 2 }
# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory.
#
# preload_app!
# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart

3
config/routes.rb Normal file
View File

@@ -0,0 +1,3 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

18
db/schema.rb generated Normal file
View File

@@ -0,0 +1,18 @@
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 0) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
end

7
db/seeds.rb Normal file
View File

@@ -0,0 +1,7 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)

0
lib/assets/.keep Normal file
View File

0
lib/tasks/.keep Normal file
View File

67
public/404.html Normal file
View File

@@ -0,0 +1,67 @@
<!DOCTYPE html>
<html>
<head>
<title>The page you were looking for doesn't exist (404)</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>
.rails-default-error-page {
background-color: #EFEFEF;
color: #2E2F30;
text-align: center;
font-family: arial, sans-serif;
margin: 0;
}
.rails-default-error-page div.dialog {
width: 95%;
max-width: 33em;
margin: 4em auto 0;
}
.rails-default-error-page div.dialog > div {
border: 1px solid #CCC;
border-right-color: #999;
border-left-color: #999;
border-bottom-color: #BBB;
border-top: #B00100 solid 4px;
border-top-left-radius: 9px;
border-top-right-radius: 9px;
background-color: white;
padding: 7px 12% 0;
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
}
.rails-default-error-page h1 {
font-size: 100%;
color: #730E15;
line-height: 1.5em;
}
.rails-default-error-page div.dialog > p {
margin: 0 0 1em;
padding: 1em;
background-color: #F7F7F7;
border: 1px solid #CCC;
border-right-color: #999;
border-left-color: #999;
border-bottom-color: #999;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
border-top-color: #DADADA;
color: #666;
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
}
</style>
</head>
<body class="rails-default-error-page">
<!-- This file lives in public/404.html -->
<div class="dialog">
<div>
<h1>The page you were looking for doesn't exist.</h1>
<p>You may have mistyped the address or the page may have moved.</p>
</div>
<p>If you are the application owner check the logs for more information.</p>
</div>
</body>
</html>

67
public/422.html Normal file
View File

@@ -0,0 +1,67 @@
<!DOCTYPE html>
<html>
<head>
<title>The change you wanted was rejected (422)</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>
.rails-default-error-page {
background-color: #EFEFEF;
color: #2E2F30;
text-align: center;
font-family: arial, sans-serif;
margin: 0;
}
.rails-default-error-page div.dialog {
width: 95%;
max-width: 33em;
margin: 4em auto 0;
}
.rails-default-error-page div.dialog > div {
border: 1px solid #CCC;
border-right-color: #999;
border-left-color: #999;
border-bottom-color: #BBB;
border-top: #B00100 solid 4px;
border-top-left-radius: 9px;
border-top-right-radius: 9px;
background-color: white;
padding: 7px 12% 0;
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
}
.rails-default-error-page h1 {
font-size: 100%;
color: #730E15;
line-height: 1.5em;
}
.rails-default-error-page div.dialog > p {
margin: 0 0 1em;
padding: 1em;
background-color: #F7F7F7;
border: 1px solid #CCC;
border-right-color: #999;
border-left-color: #999;
border-bottom-color: #999;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
border-top-color: #DADADA;
color: #666;
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
}
</style>
</head>
<body class="rails-default-error-page">
<!-- This file lives in public/422.html -->
<div class="dialog">
<div>
<h1>The change you wanted was rejected.</h1>
<p>Maybe you tried to change something you didn't have access to.</p>
</div>
<p>If you are the application owner check the logs for more information.</p>
</div>
</body>
</html>

66
public/500.html Normal file
View File

@@ -0,0 +1,66 @@
<!DOCTYPE html>
<html>
<head>
<title>We're sorry, but something went wrong (500)</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>
.rails-default-error-page {
background-color: #EFEFEF;
color: #2E2F30;
text-align: center;
font-family: arial, sans-serif;
margin: 0;
}
.rails-default-error-page div.dialog {
width: 95%;
max-width: 33em;
margin: 4em auto 0;
}
.rails-default-error-page div.dialog > div {
border: 1px solid #CCC;
border-right-color: #999;
border-left-color: #999;
border-bottom-color: #BBB;
border-top: #B00100 solid 4px;
border-top-left-radius: 9px;
border-top-right-radius: 9px;
background-color: white;
padding: 7px 12% 0;
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
}
.rails-default-error-page h1 {
font-size: 100%;
color: #730E15;
line-height: 1.5em;
}
.rails-default-error-page div.dialog > p {
margin: 0 0 1em;
padding: 1em;
background-color: #F7F7F7;
border: 1px solid #CCC;
border-right-color: #999;
border-left-color: #999;
border-bottom-color: #999;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
border-top-color: #DADADA;
color: #666;
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
}
</style>
</head>
<body class="rails-default-error-page">
<!-- This file lives in public/500.html -->
<div class="dialog">
<div>
<h1>We're sorry, but something went wrong.</h1>
</div>
<p>If you are the application owner check the logs for more information.</p>
</div>
</body>
</html>

View File

View File

0
public/favicon.ico Normal file
View File

1
public/robots.txt Normal file
View File

@@ -0,0 +1 @@
# See https://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file

75
spec/rails_helper.rb Normal file
View File

@@ -0,0 +1,75 @@
# frozen_string_literal: true
require 'simplecov'
require 'simplecov-cobertura'
SimpleCov.formatter = SimpleCov::Formatter::CoberturaFormatter
SimpleCov.start 'rails' do
add_filter '/vendor/'
add_filter '/.bundle/'
end
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
begin
ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
puts e.to_s.strip
exit 1
end
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# You can uncomment this line to turn off ActiveRecord support entirely.
# config.use_active_record = false
# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, type: :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
end

View File

@@ -0,0 +1,51 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe SchedulePipeline::FetchSchedule, type: :service do
let(:in_queue) { double(subscribe: nil) }
let(:out_queue) { double(push: nil) }
let(:errors) { double(push: nil) }
let(:service) { described_class.new(in_queue, out_queue, errors) }
describe '#start' do
it 'subscribes to the in_queue' do
service.start
expect(in_queue).to have_received(:subscribe)
end
end
describe '#process_msg' do
let(:player) { 'fake-player-identifier' }
let(:vendor) { :broad_sign }
let(:vendor_schedule) { JSON.parse(File.read('spec/services/vendors/broad_sign/broad_sign_player_schedule.json')).with_indifferent_access }
let(:vendor_fetch) { double(call: vendor_schedule) }
let(:params) { { player: player } }
let(:msg) { { params: params, vendor: vendor } }
before { allow(service).to receive(:for_vendor).and_return(vendor_fetch) }
context 'when successful' do
before { service.process_msg(msg) }
it 'fetches the player schedule from the vendor' do
expect(vendor_fetch).to have_received(:call).with(params).once
end
it 'pushes schedule to out_queue' do
expect(out_queue).to have_received(:push).with({ vendor: vendor, player: player, vendor_schedule: vendor_schedule }).once
end
it('raises no errors') { expect(errors).not_to have_received(:push) }
end
context 'when the vendor is unknown' do
before do
allow(service).to receive(:for_vendor).and_return(nil)
service.process_msg(msg)
end
it('raises an error') { expect(errors).to have_received(:push).once }
end
end
end

View File

@@ -0,0 +1,65 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe SchedulePipeline::ProcessSchedule, type: :service do
let(:in_queue) { double(subscribe: nil) }
let(:out_queue) { double(push: nil) }
let(:errors) { double(push: nil) }
let(:service) { described_class.new(in_queue, out_queue, errors) }
let(:player) { 'fake-player-id' }
let(:vendor) { 'fake-vendor' }
let(:presigned_url) { SecureRandom.uuid }
let(:transformed_schedule) { {} }
let(:vendor_transform) { double(call: transformed_schedule) }
before do
allow(service).to receive(:create_asset).and_return({ asset: { meta: { presigned_url: presigned_url } } })
allow(service).to receive(:ingest_content) {}
allow(service).to receive(:for_vendor).and_return(vendor_transform)
end
describe '#start' do
it 'subscribes to the in_queue' do
service.start
expect(in_queue).to have_received(:subscribe)
end
end
describe '#process_msg' do
let(:vendor_schedule) { JSON.parse(File.read('spec/services/vendors/broad_sign/broad_sign_player_schedule.json')).with_indifferent_access }
let(:msg) { SchedulePipeline::Models::ScheduleProcessMsg.new(vendor, player, vendor_schedule) }
let(:content_count) { vendor_schedule[:contents].count }
context 'when successful' do
before { service.process_msg(msg) }
it 'processes the player schedule from the vendor' do
expect(vendor_transform).to have_received(:call).with(vendor, player, vendor_schedule, anything).once
end
it 'pushes schedule to out_queue' do
expect(out_queue).to have_received(:push).with(transformed_schedule).once
end
it 'creates each content asset' do
expect(service).to have_received(:create_asset).exactly(content_count).times
end
it 'ingests each content' do
expect(service).to have_received(:ingest_content).exactly(content_count).times
end
it('raises no errors') { expect(errors).not_to have_received(:push) }
end
context 'when the vendor is unknown' do
before do
allow(service).to receive(:for_vendor).and_return(nil)
service.process_msg(msg)
end
it('raises an error') { expect(errors).to have_received(:push).once }
end
end
end

View File

@@ -0,0 +1,16 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe SchedulePipeline::PublishSchedule, type: :service do
let(:queue) { double(subscribe: nil) }
let(:errors) { double(push: nil) }
let(:service) { described_class.new(queue, errors) }
describe '#start' do
it 'subscribes to the queue' do
service.start
expect(queue).to have_received(:subscribe)
end
end
end

View File

@@ -0,0 +1,37 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe SchedulePipeline::SchedulePipeline, type: :service do
let(:queue_factory) { double(for_name: nil) }
describe '#initialize' do
it 'uses 3 queues' do
described_class.new(queue_factory)
expect(queue_factory).to have_received(:for_name).with(:fetch_vendor_schedules)
expect(queue_factory).to have_received(:for_name).with(:unprocessed_schedules)
expect(queue_factory).to have_received(:for_name).with(:processed_schedules)
end
end
context 'after construction' do
let(:service) { described_class.new(queue_factory) }
let(:stages) { (1..3).collect{ double(start: nil, stop: nil) } }
before { allow(service).to receive(:stages).and_return(stages) }
describe '#start' do
it 'starts each stage' do
service.start
stages.each { |stg| expect(stg).to have_received(:start).once }
end
end
describe '#stop' do
it 'stops each stage' do
service.stop
stages.each { |stg| expect(stg).to have_received(:stop).once }
end
end
end
end

Some files were not shown because too many files have changed in this diff Show More