diff --git a/.env b/.env new file mode 100644 index 0000000..b658843 --- /dev/null +++ b/.env @@ -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 \ No newline at end of file diff --git a/.env.development b/.env.development new file mode 100644 index 0000000..ba58f98 --- /dev/null +++ b/.env.development @@ -0,0 +1,8 @@ +# Rails +RAILS_ENV=development + +# Persistence +DATABASE_NAME=vendor-scheduler-service-dev + +# Vistar +VISTAR_BASE_URL=https://sandbox-api.vistarmedia.com \ No newline at end of file diff --git a/.env.test b/.env.test new file mode 100644 index 0000000..784cfd4 --- /dev/null +++ b/.env.test @@ -0,0 +1,8 @@ +# Rails +RAILS_ENV=test + +# Persistence +DATABASE_NAME=vendor-scheduler-service-test + +# Vistar +VISTAR_BASE_URL=https://sandbox-api.vistarmedia.com \ No newline at end of file diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dff662b --- /dev/null +++ b/.gitattributes @@ -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 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0fce05a --- /dev/null +++ b/.gitignore @@ -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 diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..0752cc5 --- /dev/null +++ b/.rspec @@ -0,0 +1,3 @@ +--require spec_helper +--color + diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..4457df2 --- /dev/null +++ b/.rubocop.yml @@ -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 diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..0cadbc1 --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.5.5 diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..6b6298b --- /dev/null +++ b/Gemfile @@ -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] diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..fa400da --- /dev/null +++ b/Gemfile.lock @@ -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 diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..7842862 --- /dev/null +++ b/Jenkinsfile @@ -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') + } +} diff --git a/README.md b/README.md index fb4feaf..7db80e4 100644 --- a/README.md +++ b/README.md @@ -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. +* ... diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..9a5ea73 --- /dev/null +++ b/Rakefile @@ -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 diff --git a/app/assets/config/manifest.js b/app/assets/config/manifest.js new file mode 100644 index 0000000..5918193 --- /dev/null +++ b/app/assets/config/manifest.js @@ -0,0 +1,2 @@ +//= link_tree ../images +//= link_directory ../stylesheets .css diff --git a/app/assets/images/.keep b/app/assets/images/.keep new file mode 100644 index 0000000..e69de29 diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css new file mode 100644 index 0000000..d05ea0f --- /dev/null +++ b/app/assets/stylesheets/application.css @@ -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 + */ diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb new file mode 100644 index 0000000..09705d1 --- /dev/null +++ b/app/controllers/application_controller.rb @@ -0,0 +1,2 @@ +class ApplicationController < ActionController::Base +end diff --git a/app/controllers/concerns/.keep b/app/controllers/concerns/.keep new file mode 100644 index 0000000..e69de29 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb new file mode 100644 index 0000000..de6be79 --- /dev/null +++ b/app/helpers/application_helper.rb @@ -0,0 +1,2 @@ +module ApplicationHelper +end diff --git a/app/javascript/.keep b/app/javascript/.keep new file mode 100644 index 0000000..e69de29 diff --git a/app/models/application_record.rb b/app/models/application_record.rb new file mode 100644 index 0000000..10a4cba --- /dev/null +++ b/app/models/application_record.rb @@ -0,0 +1,3 @@ +class ApplicationRecord < ActiveRecord::Base + self.abstract_class = true +end diff --git a/app/models/concerns/.keep b/app/models/concerns/.keep new file mode 100644 index 0000000..e69de29 diff --git a/app/services/amqp/amqp_service.rb b/app/services/amqp/amqp_service.rb new file mode 100644 index 0000000..44b8233 --- /dev/null +++ b/app/services/amqp/amqp_service.rb @@ -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 diff --git a/app/services/auth/auth_token_service.rb b/app/services/auth/auth_token_service.rb new file mode 100644 index 0000000..7cacaff --- /dev/null +++ b/app/services/auth/auth_token_service.rb @@ -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 diff --git a/app/services/log/log.rb b/app/services/log/log.rb new file mode 100644 index 0000000..f2b57fc --- /dev/null +++ b/app/services/log/log.rb @@ -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 diff --git a/app/services/log/loggable.rb b/app/services/log/loggable.rb new file mode 100644 index 0000000..b55de66 --- /dev/null +++ b/app/services/log/loggable.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +require_relative 'logging' + +module Log + module Loggable + def logger + Logging.instance.logger + end + end +end diff --git a/app/services/log/logging.rb b/app/services/log/logging.rb new file mode 100644 index 0000000..9c138da --- /dev/null +++ b/app/services/log/logging.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +require 'singleton' + +module Log + class Logging + include Singleton + + attr_accessor :logger + end +end diff --git a/app/services/schedule_pipeline/errors.rb b/app/services/schedule_pipeline/errors.rb new file mode 100644 index 0000000..022b473 --- /dev/null +++ b/app/services/schedule_pipeline/errors.rb @@ -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 diff --git a/app/services/schedule_pipeline/fetch_schedule.rb b/app/services/schedule_pipeline/fetch_schedule.rb new file mode 100644 index 0000000..fa32874 --- /dev/null +++ b/app/services/schedule_pipeline/fetch_schedule.rb @@ -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 diff --git a/app/services/schedule_pipeline/models/schedule.rb b/app/services/schedule_pipeline/models/schedule.rb new file mode 100644 index 0000000..c41abfc --- /dev/null +++ b/app/services/schedule_pipeline/models/schedule.rb @@ -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 diff --git a/app/services/schedule_pipeline/models/schedule_fetch_msg.rb b/app/services/schedule_pipeline/models/schedule_fetch_msg.rb new file mode 100644 index 0000000..45b999e --- /dev/null +++ b/app/services/schedule_pipeline/models/schedule_fetch_msg.rb @@ -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 diff --git a/app/services/schedule_pipeline/models/schedule_item.rb b/app/services/schedule_pipeline/models/schedule_item.rb new file mode 100644 index 0000000..e33a456 --- /dev/null +++ b/app/services/schedule_pipeline/models/schedule_item.rb @@ -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 \ No newline at end of file diff --git a/app/services/schedule_pipeline/models/schedule_process_msg.rb b/app/services/schedule_pipeline/models/schedule_process_msg.rb new file mode 100644 index 0000000..2cecf4e --- /dev/null +++ b/app/services/schedule_pipeline/models/schedule_process_msg.rb @@ -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 diff --git a/app/services/schedule_pipeline/process_schedule.rb b/app/services/schedule_pipeline/process_schedule.rb new file mode 100644 index 0000000..8071e97 --- /dev/null +++ b/app/services/schedule_pipeline/process_schedule.rb @@ -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 diff --git a/app/services/schedule_pipeline/publish_schedule.rb b/app/services/schedule_pipeline/publish_schedule.rb new file mode 100644 index 0000000..78f82de --- /dev/null +++ b/app/services/schedule_pipeline/publish_schedule.rb @@ -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 diff --git a/app/services/schedule_pipeline/queue/bunny_queue.rb b/app/services/schedule_pipeline/queue/bunny_queue.rb new file mode 100644 index 0000000..8bd5ec8 --- /dev/null +++ b/app/services/schedule_pipeline/queue/bunny_queue.rb @@ -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 diff --git a/app/services/schedule_pipeline/queue/bunny_queue_factory.rb b/app/services/schedule_pipeline/queue/bunny_queue_factory.rb new file mode 100644 index 0000000..7988ed5 --- /dev/null +++ b/app/services/schedule_pipeline/queue/bunny_queue_factory.rb @@ -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 \ No newline at end of file diff --git a/app/services/schedule_pipeline/schedule_pipeline.rb b/app/services/schedule_pipeline/schedule_pipeline.rb new file mode 100644 index 0000000..75da8d0 --- /dev/null +++ b/app/services/schedule_pipeline/schedule_pipeline.rb @@ -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 diff --git a/app/services/vendors/broad_sign/broad_sign_fetch_schedule.rb b/app/services/vendors/broad_sign/broad_sign_fetch_schedule.rb new file mode 100644 index 0000000..752b72c --- /dev/null +++ b/app/services/vendors/broad_sign/broad_sign_fetch_schedule.rb @@ -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 diff --git a/app/services/vendors/broad_sign/broad_sign_tokens.rb b/app/services/vendors/broad_sign/broad_sign_tokens.rb new file mode 100644 index 0000000..a371ece --- /dev/null +++ b/app/services/vendors/broad_sign/broad_sign_tokens.rb @@ -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 diff --git a/app/services/vendors/broad_sign/broad_sign_transform_schedule.rb b/app/services/vendors/broad_sign/broad_sign_transform_schedule.rb new file mode 100644 index 0000000..fbe49cb --- /dev/null +++ b/app/services/vendors/broad_sign/broad_sign_transform_schedule.rb @@ -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 diff --git a/app/services/vendors/errors/content_ingest_error.rb b/app/services/vendors/errors/content_ingest_error.rb new file mode 100644 index 0000000..c41f9e8 --- /dev/null +++ b/app/services/vendors/errors/content_ingest_error.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module Vendors + module Errors + class ContentIngestError < StandardError; end + end +end diff --git a/app/services/vendors/errors/schedule_fetch_error.rb b/app/services/vendors/errors/schedule_fetch_error.rb new file mode 100644 index 0000000..ff226ca --- /dev/null +++ b/app/services/vendors/errors/schedule_fetch_error.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module Vendors + module Errors + class ScheduleFetchError < StandardError; end + end +end diff --git a/app/services/vendors/errors/schedule_publish_error.rb b/app/services/vendors/errors/schedule_publish_error.rb new file mode 100644 index 0000000..695434d --- /dev/null +++ b/app/services/vendors/errors/schedule_publish_error.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module Vendors + module Errors + class SchedulePublishError < StandardError; end + end +end diff --git a/app/services/vendors/errors/schedule_transform_error.rb b/app/services/vendors/errors/schedule_transform_error.rb new file mode 100644 index 0000000..cdfc7f9 --- /dev/null +++ b/app/services/vendors/errors/schedule_transform_error.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module Vendors + module Errors + class ScheduleTransformError < StandardError; end + end +end diff --git a/app/services/vendors/vistar/vistar_fetch_ad.rb b/app/services/vendors/vistar/vistar_fetch_ad.rb new file mode 100644 index 0000000..80947ef --- /dev/null +++ b/app/services/vendors/vistar/vistar_fetch_ad.rb @@ -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 diff --git a/app/services/vendors/vistar/vistar_fetch_schedule.rb b/app/services/vendors/vistar/vistar_fetch_schedule.rb new file mode 100644 index 0000000..b8c2f89 --- /dev/null +++ b/app/services/vendors/vistar/vistar_fetch_schedule.rb @@ -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 diff --git a/app/services/vendors/vistar/vistar_settings.rb b/app/services/vendors/vistar/vistar_settings.rb new file mode 100644 index 0000000..7a56d97 --- /dev/null +++ b/app/services/vendors/vistar/vistar_settings.rb @@ -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 \ No newline at end of file diff --git a/app/services/vendors/vistar/vistar_tokens.rb b/app/services/vendors/vistar/vistar_tokens.rb new file mode 100644 index 0000000..e5ceceb --- /dev/null +++ b/app/services/vendors/vistar/vistar_tokens.rb @@ -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 diff --git a/app/services/vendors/vistar/vistar_transform_schedule.rb b/app/services/vendors/vistar/vistar_transform_schedule.rb new file mode 100644 index 0000000..0759f34 --- /dev/null +++ b/app/services/vendors/vistar/vistar_transform_schedule.rb @@ -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 diff --git a/app/services/vle/vle_create_asset.rb b/app/services/vle/vle_create_asset.rb new file mode 100644 index 0000000..ac71547 --- /dev/null +++ b/app/services/vle/vle_create_asset.rb @@ -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 diff --git a/app/services/vle/vle_ingest_asset.rb b/app/services/vle/vle_ingest_asset.rb new file mode 100644 index 0000000..8d86312 --- /dev/null +++ b/app/services/vle/vle_ingest_asset.rb @@ -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 diff --git a/app/services/vle/vle_settings.rb b/app/services/vle/vle_settings.rb new file mode 100644 index 0000000..9b27329 --- /dev/null +++ b/app/services/vle/vle_settings.rb @@ -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 \ No newline at end of file diff --git a/app/services/vle/vle_vendor_schedule.rb b/app/services/vle/vle_vendor_schedule.rb new file mode 100644 index 0000000..70e0ffd --- /dev/null +++ b/app/services/vle/vle_vendor_schedule.rb @@ -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 diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb new file mode 100644 index 0000000..b398c4c --- /dev/null +++ b/app/views/layouts/application.html.erb @@ -0,0 +1,15 @@ + + + + VendorSchedulerService + + <%= csrf_meta_tags %> + <%= csp_meta_tag %> + + <%= stylesheet_link_tag 'application', media: 'all' %> + + + + <%= yield %> + + diff --git a/bin/bundle b/bin/bundle new file mode 100755 index 0000000..524dfd3 --- /dev/null +++ b/bin/bundle @@ -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 diff --git a/bin/rails b/bin/rails new file mode 100755 index 0000000..6fb4e40 --- /dev/null +++ b/bin/rails @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby +APP_PATH = File.expand_path('../config/application', __dir__) +require_relative "../config/boot" +require "rails/commands" diff --git a/bin/rake b/bin/rake new file mode 100755 index 0000000..4fbf10b --- /dev/null +++ b/bin/rake @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby +require_relative "../config/boot" +require "rake" +Rake.application.run diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..5792302 --- /dev/null +++ b/bin/setup @@ -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 diff --git a/config.ru b/config.ru new file mode 100644 index 0000000..4a3c09a --- /dev/null +++ b/config.ru @@ -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 diff --git a/config/application.rb b/config/application.rb new file mode 100644 index 0000000..ce8fe99 --- /dev/null +++ b/config/application.rb @@ -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 diff --git a/config/application.yml b/config/application.yml new file mode 100644 index 0000000..0e47d4a --- /dev/null +++ b/config/application.yml @@ -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 diff --git a/config/boot.rb b/config/boot.rb new file mode 100644 index 0000000..d69bd27 --- /dev/null +++ b/config/boot.rb @@ -0,0 +1,3 @@ +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) + +require "bundler/setup" # Set up gems listed in the Gemfile. diff --git a/config/credentials.yml.enc b/config/credentials.yml.enc new file mode 100644 index 0000000..1b32eb3 --- /dev/null +++ b/config/credentials.yml.enc @@ -0,0 +1 @@ +55Rdn1ayK+N3mKSDjSzhLwzFh1EyhmzuXK3CKpg5Z7hFJbuWV0Jy3q9YLdPLkl2nbc3G82DxGfunXfkaYgrXBGlVKcvAMhJ6dcN3Lsr3/0pfQM3ICxa9gYaSYMGuv9HNq6a6OvNSNMR1WT2G8xovBiuReqCdmhxg26deggNqRg+sdH+vRxXjC9cpAyY0HgMAAmoDUf0x85iWaANIVaBGcGCglAYPzpktJcGRo/ZhzUI5ECQcN6lB30j9CLldWDMaYdFM5MRzlMwng3frZlw4SeWt0OAygrxH3pmq0mLs4OHKJZVp/HHvJbn27rpePN1u12d0iTzeDH2YL3UWdbbh2mzWgiBUdR30JcpuBW105fZ36Pj751Pr5kF1KU/aIYXVbLUYwEtJ/7DMu6+QL8bqAmR7oKu5xaTxjyHe--BdT+NwAvRSIcMKlG--BMm7AGk+ykT6UjdapIDuZg== \ No newline at end of file diff --git a/config/database.yml b/config/database.yml new file mode 100644 index 0000000..de1406b --- /dev/null +++ b/config/database.yml @@ -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'] %> diff --git a/config/environment.rb b/config/environment.rb new file mode 100644 index 0000000..cac5315 --- /dev/null +++ b/config/environment.rb @@ -0,0 +1,5 @@ +# Load the Rails application. +require_relative "application" + +# Initialize the Rails application. +Rails.application.initialize! diff --git a/config/environments/development.rb b/config/environments/development.rb new file mode 100644 index 0000000..c3b88cd --- /dev/null +++ b/config/environments/development.rb @@ -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 diff --git a/config/environments/production.rb b/config/environments/production.rb new file mode 100644 index 0000000..b876cdf --- /dev/null +++ b/config/environments/production.rb @@ -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 diff --git a/config/environments/test.rb b/config/environments/test.rb new file mode 100644 index 0000000..9fa79dd --- /dev/null +++ b/config/environments/test.rb @@ -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 diff --git a/config/initializers/app_config.rb b/config/initializers/app_config.rb new file mode 100644 index 0000000..3d04d34 --- /dev/null +++ b/config/initializers/app_config.rb @@ -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 diff --git a/config/initializers/application_controller_renderer.rb b/config/initializers/application_controller_renderer.rb new file mode 100644 index 0000000..89d2efa --- /dev/null +++ b/config/initializers/application_controller_renderer.rb @@ -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 diff --git a/config/initializers/assets.rb b/config/initializers/assets.rb new file mode 100644 index 0000000..fe48fc3 --- /dev/null +++ b/config/initializers/assets.rb @@ -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 ) diff --git a/config/initializers/backtrace_silencers.rb b/config/initializers/backtrace_silencers.rb new file mode 100644 index 0000000..33699c3 --- /dev/null +++ b/config/initializers/backtrace_silencers.rb @@ -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"] diff --git a/config/initializers/content_security_policy.rb b/config/initializers/content_security_policy.rb new file mode 100644 index 0000000..41c4301 --- /dev/null +++ b/config/initializers/content_security_policy.rb @@ -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 diff --git a/config/initializers/cookies_serializer.rb b/config/initializers/cookies_serializer.rb new file mode 100644 index 0000000..5a6a32d --- /dev/null +++ b/config/initializers/cookies_serializer.rb @@ -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 diff --git a/config/initializers/filter_parameter_logging.rb b/config/initializers/filter_parameter_logging.rb new file mode 100644 index 0000000..4b34a03 --- /dev/null +++ b/config/initializers/filter_parameter_logging.rb @@ -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 +] diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb new file mode 100644 index 0000000..ac033bf --- /dev/null +++ b/config/initializers/inflections.rb @@ -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 diff --git a/config/initializers/mime_types.rb b/config/initializers/mime_types.rb new file mode 100644 index 0000000..dc18996 --- /dev/null +++ b/config/initializers/mime_types.rb @@ -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 diff --git a/config/initializers/permissions_policy.rb b/config/initializers/permissions_policy.rb new file mode 100644 index 0000000..00f64d7 --- /dev/null +++ b/config/initializers/permissions_policy.rb @@ -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 diff --git a/config/initializers/schedule_pipeline_init.rb b/config/initializers/schedule_pipeline_init.rb new file mode 100644 index 0000000..4ebfc88 --- /dev/null +++ b/config/initializers/schedule_pipeline_init.rb @@ -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 diff --git a/config/initializers/wrap_parameters.rb b/config/initializers/wrap_parameters.rb new file mode 100644 index 0000000..bbfc396 --- /dev/null +++ b/config/initializers/wrap_parameters.rb @@ -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 diff --git a/config/locales/en.yml b/config/locales/en.yml new file mode 100644 index 0000000..cf9b342 --- /dev/null +++ b/config/locales/en.yml @@ -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" diff --git a/config/puma.rb b/config/puma.rb new file mode 100644 index 0000000..a6adfe7 --- /dev/null +++ b/config/puma.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb new file mode 100644 index 0000000..c06383a --- /dev/null +++ b/config/routes.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..4603022 --- /dev/null +++ b/db/schema.rb @@ -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 diff --git a/db/seeds.rb b/db/seeds.rb new file mode 100644 index 0000000..f3a0480 --- /dev/null +++ b/db/seeds.rb @@ -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) diff --git a/lib/assets/.keep b/lib/assets/.keep new file mode 100644 index 0000000..e69de29 diff --git a/lib/tasks/.keep b/lib/tasks/.keep new file mode 100644 index 0000000..e69de29 diff --git a/public/404.html b/public/404.html new file mode 100644 index 0000000..2be3af2 --- /dev/null +++ b/public/404.html @@ -0,0 +1,67 @@ + + + + The page you were looking for doesn't exist (404) + + + + + + +
+
+

The page you were looking for doesn't exist.

+

You may have mistyped the address or the page may have moved.

+
+

If you are the application owner check the logs for more information.

+
+ + diff --git a/public/422.html b/public/422.html new file mode 100644 index 0000000..c08eac0 --- /dev/null +++ b/public/422.html @@ -0,0 +1,67 @@ + + + + The change you wanted was rejected (422) + + + + + + +
+
+

The change you wanted was rejected.

+

Maybe you tried to change something you didn't have access to.

+
+

If you are the application owner check the logs for more information.

+
+ + diff --git a/public/500.html b/public/500.html new file mode 100644 index 0000000..78a030a --- /dev/null +++ b/public/500.html @@ -0,0 +1,66 @@ + + + + We're sorry, but something went wrong (500) + + + + + + +
+
+

We're sorry, but something went wrong.

+
+

If you are the application owner check the logs for more information.

+
+ + diff --git a/public/apple-touch-icon-precomposed.png b/public/apple-touch-icon-precomposed.png new file mode 100644 index 0000000..e69de29 diff --git a/public/apple-touch-icon.png b/public/apple-touch-icon.png new file mode 100644 index 0000000..e69de29 diff --git a/public/favicon.ico b/public/favicon.ico new file mode 100644 index 0000000..e69de29 diff --git a/public/robots.txt b/public/robots.txt new file mode 100644 index 0000000..c19f78a --- /dev/null +++ b/public/robots.txt @@ -0,0 +1 @@ +# See https://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 0000000..fa64ef1 --- /dev/null +++ b/spec/rails_helper.rb @@ -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 diff --git a/spec/services/schedule_pipeline/fetch_schedule_spec.rb b/spec/services/schedule_pipeline/fetch_schedule_spec.rb new file mode 100644 index 0000000..8cce1b3 --- /dev/null +++ b/spec/services/schedule_pipeline/fetch_schedule_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/services/schedule_pipeline/process_schedule_spec.rb b/spec/services/schedule_pipeline/process_schedule_spec.rb new file mode 100644 index 0000000..21b9376 --- /dev/null +++ b/spec/services/schedule_pipeline/process_schedule_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/services/schedule_pipeline/publish_schedule_spec.rb b/spec/services/schedule_pipeline/publish_schedule_spec.rb new file mode 100644 index 0000000..7e8b178 --- /dev/null +++ b/spec/services/schedule_pipeline/publish_schedule_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/services/schedule_pipeline/schedule_pipeline_spec.rb b/spec/services/schedule_pipeline/schedule_pipeline_spec.rb new file mode 100644 index 0000000..8d80992 --- /dev/null +++ b/spec/services/schedule_pipeline/schedule_pipeline_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/services/vendors/broad_sign/broad_sign_fetch_schedule_spec.rb b/spec/services/vendors/broad_sign/broad_sign_fetch_schedule_spec.rb new file mode 100644 index 0000000..4ccf626 --- /dev/null +++ b/spec/services/vendors/broad_sign/broad_sign_fetch_schedule_spec.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'webmock/rspec' + +RSpec.describe Vendors::BroadSign::BroadSignFetchSchedule do + let(:player) { 'dpc-100ca2-154410202' } + let(:tokens) { double(auth_token: SecureRandom.uuid) } + let(:action) { described_class.new(tokens) } + let(:auth_token) { SecureRandom.hex(10) } + + context 'when successful' do + let(:vendor_schedule) { JSON.parse(File.read('spec/services/vendors/broad_sign/broad_sign_player_schedule.json')).with_indifferent_access } + + before do + WebMock.stub_request(:post, URI.join("https://#{described_class::AIR_DOMAIN}", described_class::PATH)) + .to_return( + status: 200, + body: vendor_schedule.to_json + ) + end + + describe '#call' do + it 'makes the http call' do + schedule_data = action.call({ player: player }) + expect(schedule_data[:items]).not_to be_nil + end + + it 'requests an access token' do + action.call({ player: player }) + expect(tokens).to have_received(:auth_token).once + end + end + end + + context 'when unsuccessful' do + before do + WebMock.stub_request(:post, URI.join("https://#{described_class::AIR_DOMAIN}", described_class::PATH)) + .to_return( + status: 429, + body: "Too Many Requests" + ) + end + + it 'raises an error' do + expect { action.call({ player: player }) }.to raise_error(Vendors::Errors::ScheduleFetchError) + end + end +end diff --git a/spec/services/vendors/broad_sign/broad_sign_player_schedule.json b/spec/services/vendors/broad_sign/broad_sign_player_schedule.json new file mode 100644 index 0000000..3a06c13 --- /dev/null +++ b/spec/services/vendors/broad_sign/broad_sign_player_schedule.json @@ -0,0 +1,96 @@ +{ + "items": [ + { + "startTime": "2019-06-14T14:17:26.456Z", + "duration": "5s", + "token": "CgphbGV4YW5kcmVjEgNVVEMaDAj22I7oBRCAhLjZASICCAUoAToDCNMBQgMIlRtKAwiSG2IDVVUwcgMI4BE=", + "contentIndex": 0, + "campaign_index": 0, + "frame_index": 0, + "geometry_index": 0 + }, + { + "duration": "5s", + "token": "CgphbGV4YW5kcmVjEgNVVEMaDAj72I7oBRCAhLjZASICCAUoAToDCNQBQgMInhtKAwiSG2IDVVUwcgMI4BE=", + "contentIndex": 1, + "campaign_index": 1, + "frame_index": 0, + "geometry_index": 0 + }, + { + "duration": "5s", + "token": "CgphbGV4YW5kcmVjEgNVVEMaDAio2Y7oBRCAhLjZASICCAUoAToDCNMBQgMIlRtKAwiSG2IDVVUwcgMI4BE=", + "contentIndex": 2, + "campaign_index": 2, + "frame_index": 0, + "geometry_index": 0 + } + ], + "contents": [ + { + "name": "Content-0", + "mimeType": "jpg", + "uri": "https://my-storage-url.com/11045825-19889464/8e649eb2-4e0a-4727-8d8c-9e6b1fb31f50.jpg", + "size": "187435", + "hash": { + "type": "CRC32", + "payload": "ODc1ZTlmYg==" + }, + "adCopyId": "211" + }, + { + "name": "Content-1", + "mimeType": "jpg", + "uri": "https://my-storage-url.com/11045825-19889464/8e649eb2-4e0a-4727-8d8c-9e6b1fb31f50.jpg", + "size": "97227", + "hash": { + "type": "CRC32", + "payload": "ODI0MmZjMjg=" + }, + "adCopyId": "212" + }, + { + "name": "Content-2", + "mimeType": "mp4", + "uri": "https://my-storage-url.com/11045825-19889464/8e649eb2-4e0a-4727-8d8c-9e6b1fb31f50.mp4", + "size": "97434227", + "hash": { + "type": "CRC32", + "payload": "ODI0MmZjMjg=" + }, + "adCopyId": "2122" + } + ], + "identification": { + "playerId": "2272", + "displayUnitId": "3472", + "displayUnitLatlong": { + "latitude": 45.5164, + "longitude": -73.5548 + }, + "displayUnitAddress": "1100 Robert-Bourassa Montreal" + }, + "campaigns": [ + { + "campaignId": "19889482" + }, + { + "campaignId": "14889444" + }, + { + "campaignId": "99932" + } + ], + "frames": [ + { + "frameId": "19889456" + } + ], + "geometries": [ + { + "width": 1920, + "height": 1080, + "fullscreen": true + } + ] +} \ No newline at end of file diff --git a/spec/services/vendors/broad_sign/broad_sign_transform_schedule_spec.rb b/spec/services/vendors/broad_sign/broad_sign_transform_schedule_spec.rb new file mode 100644 index 0000000..f152e49 --- /dev/null +++ b/spec/services/vendors/broad_sign/broad_sign_transform_schedule_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Vendors::BroadSign::BroadSignTransformSchedule do + let(:action) { described_class.new } + let(:vendor) { 'fake-vendor' } + let(:player) { 'dpc-100ca2-154410202' } + let(:vendor_schedule) { JSON.parse(File.read('spec/services/vendors/broad_sign/broad_sign_player_schedule.json')).with_indifferent_access } + let(:content_map) { vendor_schedule[:contents].collect { |item| { id: SecureRandom.uuid, name: item[:name] } } } + let(:content_keys) { content_map.collect { |item| item[:id] } } + + context 'when successful' do + describe '#call' do + it 'converts each item in the vendor schedule' do + schedule_data = action.call(vendor, player, vendor_schedule, content_map) + expect(schedule_data.items.count).to eq(vendor_schedule[:items].count) + end + + it 'maps content indices' do + schedule_data = action.call(vendor, player, vendor_schedule, content_map) + expect(schedule_data.items.collect(&:content_key)).to eq(content_keys) + end + + it 'parses item duration' do + schedule_data = action.call(vendor, player, vendor_schedule, content_map) + expect(schedule_data.items.collect(&:duration)).to eq(vendor_schedule[:items].collect { |i| i[:duration] }) + end + end + end +end diff --git a/spec/services/vendors/vistar/vistar_ad_fetch_response.json b/spec/services/vendors/vistar/vistar_ad_fetch_response.json new file mode 100644 index 0000000..40917a8 --- /dev/null +++ b/spec/services/vendors/vistar/vistar_ad_fetch_response.json @@ -0,0 +1,23 @@ +{ + "advertisement": [ + { + "id": "-227826498", + "proof_of_play_url": "https://sandbox-api.vistarmedia.com/api/v2/proof_of_play/json?s=27jkXH6c5WIPAaVHTauJUJ0NFfbMQlequO8MUArXW8Hhdot43e0S6fo5kK2j7r3kCVybgRnrRsxu2-zibsVFueeUMfwytTVPMYhG4u--3YEvFH6rQe2LQKiWnkQmyopVC09URKCS3Vtz8H0G3svHQMeWi8tcvnkZyVHzEIzDLuBXugusV76kbUZ-OvIhOihT0cPBH3Oj5mAKQZpQiP2sCVMVS0XocBajc20CeCE7rYXwds-Rj1WnvRPvEzz_dBpfwo-cpD8tNLUJCOzr5loY_kxOgbYQttEPwD1SWEZgCmf7qo9AaaTYDjiEHKHgU94t5AaldJqIaz3f5ax5j5oXr9YxvOGSblN25XPNoNZDFWbb0bS5oClXhBN3kWIB_U_C_lLqFYx0nZAQuiso9oSzaTiEbqTCbNCkpZ8UPxCxvsUq6_Q5usQkKNwzG6TCv1_fK9xRTWoVHznMoiHZ0y-lDF33K6QCEmAqDExpCWi7BIsT66KFznH0DbgElJBYsTpX7Dp5Na7MNaE3_Rus4fJz8Kjx4gwL0vYTl_pHmOaZZPQGOQp8GKv4qq7uvSEUw49-tY2id5snQ1xBQ0iwVsX5EET_wKJh9-EJHzcjqhBr_ZcIk70qgxK1zTMjfqDe6_aQHfY3rXiAXRPCUOM9ayUy8db1cbyuOdkOA63HQJXLIGVNwTXOlaDU-jBLB3A-_ap0ap_tcz_085-rH7A-ORG_aoiYPjrOQEEHl-AQcnanuVjEx30n-UyKcjP6BHoXbNKMj-GyWyclUsSHo1t0Tw2ARN6b7FIaK272DToMQCLbxGxJJymD5Iwy2p03XeUH1vbZmq2EOewq1Q1W2YPmJ_M2YAUYL2I4MyF2icS4xdySZ1y3mdmbuA", + "expiration_url": "https://sandbox-api.vistarmedia.com/api/v2/cancel/json?s=27jkXH6c5WIPAaVHTauJUJ0NFfbMQlequO8MUArXW8Hhdot43e0S6fo5kK2j7r3kCVybgRnrRsxu2-zibsVFueeUMfwytTVPMYhG4u--3YEvFH6rQe2LQKiWnkQmyopVC09URKCS3Vtz8H0G3svHQMeWi8tcvnkZyVHzEIzDLuBXugusV76kbUZ-OvIhOihT0cPBH3Oj5mAKQZpQiP2sCVMVS0XocBajc20CeCE7rYXwds-Rj1WnvRPvEzz_dBpfwo-cpD8tNLUJCOzr5loY_kxOgbYQttEPwD1SWEZgCmf7qo9AaaTYDjiEHKHgU94t5AaldJqIaz3f5ax5j5oXr9YxvOGSblN25XPNoNZDFWbb0bS5oClXhBN3kWIB_U_C_lLqFYx0nZAQuiso9oSzaTiEbqTCbNCkpZ8UPxCxvsUq6_Q5usQkKNwzG6TCv1_fK9xRTWoVHznMoiHZ0y-lDF33K6QCEmAqDExpCWi7BIsT66KFznH0DbgElJBYsTpX7Dp5Na7MNaE3_Rus4fJz8Kjx4gwL0vYTl_pHmOaZZPQGOQp8GKv4qq7uvSEUw49-tY2id5snQ1xBQ0iwVsX5EET_wKJh9-EJHzcjqhBr_ZcIk70qgxK1zTMjfqDe6_aQHfY3rXiAXRPCUOM9ayUy8db1cbyuOdkOA63HQJXLIGVNwTXOlaDU-jBLB3A-_ap0ap_tcz_085-rH7A-ORG_aoiYPjrOQEEHl-AQcnanuVjEx30n-UyKcjP6BHoXbNKMj-GyWyclUsSHo1t0Tw2ARN6b7FIaK272DToMQCLbxGxJJymD5Iwy2p03XeUH1vbZmq2EOewq1Q1W2YPmJ_M2YAUYL2I4MyF2icS4xdySZ1y3mdmbuA", + "display_time": 1638576000, + "lease_expiry": 1638662400, + "display_area_id": "display-0", + "creative_id": "582e43720b476f4e6d3e5e69495e370c585b27436352", + "asset_id": "6e286b5d0b796835683b78154b4030162f643b1b7e74", + "asset_url": "https://s3.amazonaws.com/dev.assets.vistarmedia.com/creative/SLNbnvhuQo2t71yKptSsvA/2f/f0a/c1137069-0966-4be2-b109-1d742d759191.jpeg", + "width": 600, + "height": 900, + "mime_type": "image/jpeg", + "length_in_seconds": 8, + "length_in_milliseconds": 8000, + "campaign_id": 1044648314, + "creative_category": "10144", + "advertiser": "01" + } + ] +} \ No newline at end of file diff --git a/spec/services/vendors/vistar/vistar_fetch_ad_spec.rb b/spec/services/vendors/vistar/vistar_fetch_ad_spec.rb new file mode 100644 index 0000000..590aad6 --- /dev/null +++ b/spec/services/vendors/vistar/vistar_fetch_ad_spec.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'webmock/rspec' + +RSpec.describe Vendors::Vistar::VistarFetchAd do + let(:player) { 'dpc-100ca2-154410202' } + let(:tokens) { double(api_key: SecureRandom.uuid) } + let(:action) { described_class.new(tokens) } + let(:auth_token) { SecureRandom.hex(10) } + let(:params) { { device_id: player } } + + before do + allow(Vendors::Vistar::VistarSettings.instance).to receive(:network_id).and_return('fake-network-id') + end + + context 'when successful' do + let(:response_body) { JSON.parse(File.read('spec/services/vendors/vistar/vistar_ad_fetch_response.json')).with_indifferent_access } + + before do + WebMock.stub_request(:post, Vendors::Vistar::VistarSettings.instance.vistar_url(described_class::PATH)) + .to_return( + status: 200, + body: response_body.to_json + ) + end + + describe '#call' do + it 'makes the http call' do + response = action.call(params) + expect(response).to eq(response_body) + end + + it 'requests an api key' do + action.call(params) + expect(tokens).to have_received(:api_key).once + end + end + end + + context 'when unsuccessful' do + before do + WebMock.stub_request(:post, Vendors::Vistar::VistarSettings.instance.vistar_url(described_class::PATH)) + .to_return( + status: 500, + ) + end + + it 'raises an error' do + expect { action.call(params) }.to raise_error(Vendors::Errors::ScheduleFetchError) + end + end +end diff --git a/spec/services/vendors/vistar/vistar_fetch_schedule_spec.rb b/spec/services/vendors/vistar/vistar_fetch_schedule_spec.rb new file mode 100644 index 0000000..966967b --- /dev/null +++ b/spec/services/vendors/vistar/vistar_fetch_schedule_spec.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Vendors::Vistar::VistarFetchSchedule do + let(:tokens) { double(api_key: SecureRandom.uuid) } + let(:action) { described_class.new(tokens) } + let(:params) { {} } + + + let(:fetch_ad_response) { JSON.parse(File.read('spec/services/vendors/vistar/vistar_ad_fetch_response.json')).with_indifferent_access } + let(:ad) { fetch_ad_response[:advertisement][0] } + let(:schedule) do + { + 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 + + describe '#call' do + context 'when ad is fetched from vistar' do + before { allow(action).to receive(:fetch_ad).and_return(fetch_ad_response) } + + it 'returns the expected schedule with the first ad' do + expect(action.call(params)).to eq(schedule) + end + end + end +end diff --git a/spec/services/vendors/vistar/vistar_schedule.json b/spec/services/vendors/vistar/vistar_schedule.json new file mode 100644 index 0000000..24046d7 --- /dev/null +++ b/spec/services/vendors/vistar/vistar_schedule.json @@ -0,0 +1,16 @@ +{ + "contents": [ + { + "name": "vistar_asset_-227826498", + "url": "https://s3.amazonaws.com/dev.assets.vistarmedia.com/creative/SLNbnvhuQo2t71yKptSsvA/2f/f0a/c1137069-0966-4be2-b109-1d742d759191.jpeg" + } + ], + "startTime": 1638576000, + "items": [ + { + "contentIndex": 0, + "duration": "8s", + "pop_url": "https://sandbox-api.vistarmedia.com/api/v2/proof_of_play/json?s=27jkXH6c5WIPAaVHTauJUJ0NFfbMQlequO8MUArXW8Hhdot43e0S6fo5kK2j7r3kCVybgRnrRsxu2-zibsVFueeUMfwytTVPMYhG4u--3YEvFH6rQe2LQKiWnkQmyopVC09URKCS3Vtz8H0G3svHQMeWi8tcvnkZyVHzEIzDLuBXugusV76kbUZ-OvIhOihT0cPBH3Oj5mAKQZpQiP2sCVMVS0XocBajc20CeCE7rYXwds-Rj1WnvRPvEzz_dBpfwo-cpD8tNLUJCOzr5loY_kxOgbYQttEPwD1SWEZgCmf7qo9AaaTYDjiEHKHgU94t5AaldJqIaz3f5ax5j5oXr9YxvOGSblN25XPNoNZDFWbb0bS5oClXhBN3kWIB_U_C_lLqFYx0nZAQuiso9oSzaTiEbqTCbNCkpZ8UPxCxvsUq6_Q5usQkKNwzG6TCv1_fK9xRTWoVHznMoiHZ0y-lDF33K6QCEmAqDExpCWi7BIsT66KFznH0DbgElJBYsTpX7Dp5Na7MNaE3_Rus4fJz8Kjx4gwL0vYTl_pHmOaZZPQGOQp8GKv4qq7uvSEUw49-tY2id5snQ1xBQ0iwVsX5EET_wKJh9-EJHzcjqhBr_ZcIk70qgxK1zTMjfqDe6_aQHfY3rXiAXRPCUOM9ayUy8db1cbyuOdkOA63HQJXLIGVNwTXOlaDU-jBLB3A-_ap0ap_tcz_085-rH7A-ORG_aoiYPjrOQEEHl-AQcnanuVjEx30n-UyKcjP6BHoXbNKMj-GyWyclUsSHo1t0Tw2ARN6b7FIaK272DToMQCLbxGxJJymD5Iwy2p03XeUH1vbZmq2EOewq1Q1W2YPmJ_M2YAUYL2I4MyF2icS4xdySZ1y3mdmbuA" + } + ] +} diff --git a/spec/services/vendors/vistar/vistar_transform_schedule_spec.rb b/spec/services/vendors/vistar/vistar_transform_schedule_spec.rb new file mode 100644 index 0000000..db8f9ce --- /dev/null +++ b/spec/services/vendors/vistar/vistar_transform_schedule_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Vendors::Vistar::VistarTransformSchedule do + let(:action) { described_class.new } + let(:vendor) { 'fake-vendor' } + let(:player) { 'dpc-100ca2-154410202' } + let(:vendor_schedule) { JSON.parse(File.read('spec/services/vendors/vistar/vistar_schedule.json')).with_indifferent_access } + let(:content_map) { vendor_schedule[:contents].collect { |item| { id: SecureRandom.uuid, name: item[:name] } } } + let(:content_keys) { content_map.collect { |item| item[:id] } } + + context 'when successful' do + describe '#call' do + it 'converts each item in the vendor schedule' do + schedule_data = action.call(vendor, player, vendor_schedule, content_map) + expect(schedule_data.items.count).to eq(vendor_schedule[:items].count) + end + + it 'maps content indices' do + schedule_data = action.call(vendor, player, vendor_schedule, content_map) + expect(schedule_data.items.collect(&:content_key)).to eq(content_keys) + end + + it 'parses item duration' do + schedule_data = action.call(vendor, player, vendor_schedule, content_map) + expect(schedule_data.items.collect(&:duration)).to eq(vendor_schedule[:items].collect { |i| i[:duration] }) + end + end + end +end diff --git a/spec/services/vle/vle_create_asset_spec.rb b/spec/services/vle/vle_create_asset_spec.rb new file mode 100644 index 0000000..a9beb15 --- /dev/null +++ b/spec/services/vle/vle_create_asset_spec.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'webmock/rspec' + +RSpec.describe Vle::VleCreateAsset do + let(:token) { SecureRandom.hex(4) } + let(:asset) do + { + asset: { + id: 101, + type: "asset", + processed: 0, + error: "", + project_id: 155162, + meta: { + key: "sandbox-vle/library/original/101_original.jpeg", + presigned_url: "https://fake-url.com", + original_filename: "sunrise.jpeg" + }, + name:"sunrise", + orientation:"any", + loop:false, + contract_id:"", + description_long:"", + description_short:"", + tag_match_expression:"", + tags:[], + created_at:"2021-10-22T18:09:13.689Z", + updated_at:"2021-10-22T18:09:13.710Z", + blobs:[] + } + }.with_indifferent_access + end + let(:content) { asset.slice(:name, :organization_id, :project_id, :contract_id).merge(file: 'fake-filename.ext') } + let(:action) { described_class.new(content) } + let(:url) { Vle::VleSettings.instance.central_url.join(described_class::PATH) } + + context 'when successful' do + before do + WebMock.stub_request(:post, action.url) + .to_return( + status: 200, + body: { asset: asset }.to_json + ) + end + + describe '#call' do + it 'makes the http call' do + response = action.call(token) + expect(response[:asset]).to eq(asset) + end + end + end + + context 'when unsuccessful with 401' do + before { WebMock.stub_request(:post, action.url).to_return(status: 401, body: "") } + + it 'raises an auth error' do + expect { action.call(token) }.to raise_error(Auth::Client::Errors::Unauthorized) + end + end + + context 'when unsuccessful with 500' do + before { WebMock.stub_request(:post, action.url).to_return(status: 500, body: "") } + + it 'raises an error' do + expect { action.call(token) }.to raise_error(Vendors::Errors::ContentIngestError) + end + end +end diff --git a/spec/services/vle/vle_ingest_asset_spec.rb b/spec/services/vle/vle_ingest_asset_spec.rb new file mode 100644 index 0000000..998926f --- /dev/null +++ b/spec/services/vle/vle_ingest_asset_spec.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'webmock/rspec' + +RSpec.describe Vle::VleIngestAsset do + let(:action) { described_class.new } + let(:source) { 'https://fake-source.com' } + let(:destination) { 'https://fake-destination.com' } + let(:content) { { uri: source } } + let(:asset_contents) { SecureRandom.hex(16) } + + before do + WebMock.stub_request(:get, source).to_return(status: 200, body: asset_contents) + WebMock.stub_request(:put, destination).to_return(status: 200) + end + + context 'when the requests work' do + describe '#call' do + it('succeeds') { expect(action.call(content, destination)).to eq(true) } + end + end + + context 'when content fetch fails' do + before do + WebMock.stub_request(:get, source).to_return(status: 400) + end + + it 'raises an error' do + expect { action.call(content, destination) }.to raise_error(Vendors::Errors::ContentIngestError) + end + end + + context 'when content post fails' do + before do + WebMock.stub_request(:put, destination).to_return(status: 400) + end + + it 'raises an error' do + expect { action.call(content, destination) }.to raise_error(Vendors::Errors::ContentIngestError) + end + end +end diff --git a/spec/services/vle/vle_vendor_schedule_spec.rb b/spec/services/vle/vle_vendor_schedule_spec.rb new file mode 100644 index 0000000..ea5aa60 --- /dev/null +++ b/spec/services/vle/vle_vendor_schedule_spec.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'webmock/rspec' + +RSpec.describe Vle::VleVendorSchedule do + let(:token) { SecureRandom.hex(10) } + let(:vendor) { 'fake-vendor' } + let(:player) { 'dpc-100ca2-154410202' } + let(:schedule) do + { + "items": [ + { + "startTime": "2019-06-14T14:17:26.456Z", + "duration": "5s", + "pop_data": "CgphbGV4YW5kcmVjEgNVVEMaDAj22I7oBRCAhLjZASICCAUoAToDCNMBQgMIlRtKAwiSG2IDVVUwcgMI4BE=", + "content_key": 0, + }, + { + "startTime": "2019-06-14T14:17:26.456Z", + "duration": "5s", + "pop_data": "CgphbGV4YW5kcmVjEgNVVEMaDAj72I7oBRCAhLjZASICCAUoAToDCNQBQgMInhtKAwiSG2IDVVUwcgMI4BE=", + "content_key": 1, + } + ] + }.with_indifferent_access + end + let(:action) { described_class.new(vendor, player, schedule) } + + let(:url) { Vle::VleSettings.instance.scheduler_url.join(described_class::PATH) } + + context 'when successful' do + before { WebMock.stub_request(:post, action.url).to_return(status: 200, body: {}.to_json ) } + + describe '#call' do + it 'makes the http call' do + response = action.call(token) + expect(response).not_to be_nil + end + end + end + + context 'when unsuccessful with a 401' do + before { WebMock.stub_request(:post, action.url).to_return(status: 401) } + + it 'raises an error' do + expect { action.call(token) }.to raise_error(Auth::Client::Errors::Unauthorized) + end + end + + context 'when unsuccessful with a 500' do + before { WebMock.stub_request(:post, action.url).to_return(status: 500) } + + it 'raises an error' do + expect { action.call(token) }.to raise_error(Vendors::Errors::SchedulePublishError) + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..ce33d66 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,96 @@ +# This file was generated by the `rails generate rspec:install` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # This allows you to limit a spec run to individual examples or groups + # you care about by tagging them with `:focus` metadata. When nothing + # is tagged with `:focus`, all examples get run. RSpec also provides + # aliases for `it`, `describe`, and `context` that include `:focus` + # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + config.filter_run_when_matching :focus + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = "spec/examples.txt" + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + config.disable_monkey_patching! + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = "doc" + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end +end diff --git a/vendor/.keep b/vendor/.keep new file mode 100644 index 0000000..e69de29