Skip to content

Instantly share code, notes, and snippets.

@joost
joost / README.md
Last active January 8, 2024 20:03
How to fix invalid byte sequence in UTF-8 rack in Rails

Add the utf8_sanitizer.rb to your Rails 3.2 project in app/middleware. Instead of removing the invalid request characters and continuing the request (as some gems do) it returns a 400 error.

Add the following line to your config/application.rb:

config.middleware.use 'Utf8Sanitizer'

If you only need it in production add to config/environments/production.rb. This can be without quotes:

config.middleware.use Utf8Sanitizer
@joost
joost / ruby_google_analytics_server_to_server.md
Last active November 27, 2023 15:43
Google Analytics API (server-to-server) using Ruby
@joost
joost / .env.example
Last active December 15, 2022 09:50
Clone or pull all your Azure DevOps repo's (for all projects)
export AZURE_PERSONAL_ACCESS_TOKEN=YOUR_AZURE_ACCESS_TOKEN
export AZURE_ORGANIZATION_URL=https://dev.azure.com/YOURORG
@joost
joost / resize_boot2docker.sh
Last active December 14, 2022 07:34
Resize boot2docker VirtualBox image
# Steps we will take:
# 1. Change boot2docker image type (this will take long)
# 2. Resize image
# 3. Resize partion (using GParted)
#
# Also see: https://docs.docker.com/articles/b2d_volume_resize/
# Stop boot2docker
boot2docker stop
@joost
joost / webhooks_controller.rb
Last active February 4, 2021 13:39
Mandrill API Webhook signature verification. This shows how you could verify a Mandrill signature in a Rails Controller.
class WebhooksController < ActionController::Base
WEBHOOK_KEY = "some_key" # You could also use an API request to lookup the key
before_filter :verify_request_signature
# See: http://help.mandrill.com/entries/23704122-Authenticating-webhook-requests
def verify_request_signature
signed_data = request.url
post_params = request.request_parameters.dup # POST parameters
@joost
joost / deploy.rb
Last active October 10, 2018 09:12 — forked from toobulkeh/deploy.rb
Capistrano 3 rails console tasks
# encoding: UTF-8
# Place in config/deploy.rb
# See: https://gist.github.com/joost/9343156
# Adapted to work with rbenv
namespace :rails do
desc "Open the rails console on primary app server"
task :console do
on roles(:app), primary: true do
rails_env = fetch(:stage)
execute_interactively "#{bundle_cmd} #{current_path}/script/rails console #{rails_env}"
@joost
joost / update_cache_counters.rake
Last active September 25, 2018 15:05 — forked from svyatov/update_cache_counters.rake
Rails Rake Task: Update all cache counters / counter caches.
# More robust version to update new or existing counter cache columns in your Rails app.
# See: https://gist.github.com/svyatov/4225663
desc 'Update all cache counters'
task :update_cache_counters => :environment do
models_to_update = {}
# or: Rails.application.eager_load!
# Dir loads less, so it's faster
Dir.glob(Rails.root.join('app/models/**/*')).each { |model| require model if File.file?(model) }
@joost
joost / pow_tunnel.md
Last active September 19, 2018 02:22
How to share a local site over the web using Pow.cx and Nginx. Similar to forwardhq.com, showoff.io, .. but your own and free.
  • Have pow.cx (http://pow.cx) installed

  • Create a DNS record *.dev.yourserver.com pointing to your Nginx server

  • Configure Nginx with the configuration below

  • Run: ssh -p 22 -nNT -g -R *:8888:0.0.0.0:3000 account@youserver.com

  • Go to http://somepowdomain.dev.yourserver.com and enjoy!

      server {
          listen 80;
          server_name *.dev.yourserver.com;
    
@joost
joost / api_controller.rb
Last active July 31, 2018 06:38
Validate parameters in Rails 4 JSON API
class ApiController < ApplicationController
# FIXME: Since we cannot set ActionController::Parameters.action_on_unpermitted_parameters = :raise
# on a controller level we do this hotfix.
class ApiParameters < ActionController::Parameters
def action_on_unpermitted_parameters
:raise
end
end
def params
@joost
joost / json_validator.rb
Created March 26, 2015 14:13
Rails 3 / Rails 4 JSON validator
# Put this code in lib/validators/json_validator.rb
# Usage in your model:
# validates :json_attribute, presence: true, json: true
#
# To have a detailed error use something like:
# validates :json_attribute, presence: true, json: {message: :some_i18n_key}
# In your yaml use:
# some_i18n_key: "detailed exception message: %{exception_message}"
class JsonValidator < ActiveModel::EachValidator