Skip to content

Instantly share code, notes, and snippets.

Avatar

Owen Peredo Diaz owen2345

View GitHub Profile
@owen2345
owen2345 / gist:26ab63cbe6af0f2194886c21417c49c1
Last active Nov 1, 2020
Configure e2e tests using cypress for dockerized projects
View gist:26ab63cbe6af0f2194886c21417c49c1
#// .github/actions/ruby.yml => Google actions
# Note: Includes error silence to avoid invalid "exit 0" when opening cypress service (cypress bug)
- name: E2e tests
run: docker-compose run test_content_service bash -c "CI=true foreman start -f Procfile-test-e2e 2> /dev/null"
if: ${{env.FRONTEND_CHANGED}}
#// Procfile-e2e ==> Start all required services (rails, react app and then init cypress awaiting for react port)
test_api_server: rm -f tmp/pids/server-test.pid; bundle exec rails s -e test -p 3031 -b '0.0.0.0' --pid tmp/pids/server-test.pid
frontend: cd frontend/ && PORT=3030 BROWSER=none RAILS_PORT=3031 yarn start test --silent
@owen2345
owen2345 / ruby.yaml
Created Aug 12, 2020
micro services + micro frnotends CI
View ruby.yaml
name: Content System
on:
push:
branches:
- master
- develop
- staging
pull_request:
@owen2345
owen2345 / repair_nested_params.rb
Last active Jun 19, 2020
Repair numbers converted into string (Rails 4)
View repair_nested_params.rb
repair_nested_params({id: '11', age: '25'}) # Sample
def repair_nested_params(obj)
obj.each { |key, value| obj[key] = parse_value(value) }
end
def parse_value(value)
return repair_nested_params(value) if value.is_a?(Hash)
return value.map(&method(:parse_value)) if value.is_a?(Array)
return value unless value.is_a?(String)
@owen2345
owen2345 / delayed_job_ext.rb
Last active Mar 20, 2020
Delayed job: uniqueness support, sequential execution support, keep successful jobs
View delayed_job_ext.rb
# frozen_string_literal: true
# config/initializers/delayed_job_ext.rb
# unique jobs across workers
# Sample: my_model.delay(across_uniq_key: 'article-19').create
# Sample: my_model.delay(across_uniq_key: 'article-19').update
# Sample: my_model.delay(across_uniq_key: 'article-19').destroy
# ==> the jobs will be processed in serial, not in parallel:
# can not call update before create or run both at the same time
Delayed::Job.class_eval do
@owen2345
owen2345 / uniqueness_job.rb
Created Sep 20, 2019
Sidekiq uniqueness jobs. Permits to avoid duplicated jobs already enqueued or scheduled.
View uniqueness_job.rb
# frozen_string_literal: true
require 'sidekiq/api'
module UniquenessJob
extend ActiveSupport::Concern
included do
class << self
alias_method :client_push_old, :client_push
def client_push(item)
@owen2345
owen2345 / Gemfile
Created Aug 31, 2019
Multiple man subscribe testing
View Gemfile
gem 'multiple_man'
gem 'bunny-mock'
@owen2345
owen2345 / result.yaml
Last active May 8, 2019
Yaml does not permit to share values between documents in the same file. Then this script helps with that problem.
View result.yaml
---
apiVersion: extensions/v1beta1
kind: Deployment
env: &env_vars
val1: "val1"
val2: "val2"
---
apiVersion: extensions/v1beta1
kind: Service
env:
@owen2345
owen2345 / secrets_generator.py
Created May 8, 2019
Kubernetes secrets generator from env vars (secrets.yml content and env vars for deployment.yml). Original code here: https://github.com/TelluIoT/kubernetes-env-to-secrets/blob/master/README.md
View secrets_generator.py
# required python 3
# command: python <location_of_the_file>/secrets_generator.py --env <path_to_env_var_file> --name <secrets_name>
#sample: python ./secrets_generator.py --env .env --name my_secrets
import copy
import argparse
import sys
import configparser
import itertools
import base64
from string import Template
View comments_controller.rb
class CommentsController < ApplicationController
def users_comments
# SOLUTION 1: a bit complicated to add pagination and its sql performance is bad when there is a lot of comments,
# because the sql query is returning all matched comments
@user_comments = Post.all.includes(:comments).eager_load(comments: :author)
.where(author: {username: params[:username]}).map(&:comments)
.flatten
# SOLUTION 2: easy to add pagination and its sql performance is much better
@user_comments = Comment.joins(:post, :author).where(author: {username: params[:username]})
View array_flatten.rb
#
# Converts multidimensional array into one-dimensional array
# @param array_data [Array]: Multidimensional array to be converted into one-dimensional array
# @param res [Array]: Internal param control (ignore it)
# @sample 1: flatten([1,2,[3,4]]) => [1,2,3,4]
# @sample 1: flatten([1,2,[3,4], [[5], [6, [7]]]]) => [1,2,3,4]
# @return [Array] Returns a new array that is a one-dimensional
def flatten(array_data, res = [])
array_data.each{|v| v.is_a?(Array) ? flatten(v, res) : res.push(v) }
res