Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

How to compress URL parameters (and any other string) in Ruby

Introduction

Working with web applications and microservices we often have the problem of passing long lists of settings through HTTP requests. While the canonical way of doing this would be to use a POST request and pass the values in the body of the request, this is not always an option for us. (Comment: How come? Maybe the reason we can't do it is interesting/helpful)

In fact there can be cases where we need to generate URLs that can be passed to other users as a link, and reused by them to later trigger an action in the target Rails server.

The only way to achieve this is for us to be able to pass these values as URL parameters in a GET request, but unfortunately these URLs exceed often the maximum length limit allowed by the browsers or web servers. (Comment: Maybe include the byte limit here)

@rgould
rgould / pre-commit.rb
Created August 10, 2017 01:10 — forked from cben/pre-commit.rb
git pre-commit hook that runs rubocop on staged files
#!/usr/bin/env ruby
# make sure you have gem 'rubocop-git' in your Gemfile
require 'readline'
def input(prompt="", newline=false)
prompt += "\n" if newline
Readline.readline(prompt, true).squeeze(" ").strip
end
@rgould
rgould / pre-commit.rb
Last active August 17, 2016 17:18
Don't allow rspec's `:focus` to be committed
#!/usr/bin/env ruby
# Copy this to your project's `.git/pre-commit` file.
if `git diff --cached spec` =~ /,\s?(:focus|focus:\s?true|:focus\s?=>\s?true)/
puts "\e[31mPlease focus and remove your :focus tags before committing :)"
exit 1
end
ERROR: Traceback (most recent call last): File "/home/ubuntu/upscaler.py", line 425, in reportCloudWatchMetrics dimensions = metric['dimensions']) File "/usr/local/lib/python2.7/dist-packages/boto/ec2/cloudwatch/__init__.py", line 334, in put_metric_data return self.get_status('PutMetricData', params, verb="POST") File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 1213, in get_status response = self.make_request(action, params, path, verb) File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 1116, in make_request return self._mexe(http_request) File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 1028, in _mexe raise BotoServerError(response.status, response.reason, body) BotoServerError: BotoServerError: 500 Internal Server Error <ErrorResponse xmlns="http://monitoring.amazonaws.com/doc/2010-08-01/"> <Error> <Type>Receiver</Type> <Code>InternalFailure</Code> </Error> <RequestId>f9f369c7-5f8b-11e5-aa80-67222b579acd</RequestId> </ErrorResponse>
@rgould
rgould / raven_client.js
Created March 13, 2014 05:31
Using Sentry-Raven with Meteor on both client and server, catching uncaught exceptions. Uses this PR: https://github.com/deepwell/meteor-raven/pull/4
function initializeRaven() {
/*
* SENTRY_DSN is provided as an environment variable on Heroku, and is
* inaccessible on the client. We added this method to make it accessible.
*/
Meteor.call('getSentryDSN', function(error, sentryDSN) {
if (error) throw error;
RavenLogger.initialize({
client: sentryDSN
});
def welcome
@show_splash = false
if @current_user
redirect_to home_path(@current_brand)
else
@ct_deal = nil
@biz_deal = nil
@kidz_deal = nil
Deal.live_by_brand("gaggleup", @current_region).each do |chkdeal|
if check_for_exclusive_deal_access(chkdeal, @current_user)
@rgould
rgould / gist:3006199
Created June 27, 2012 19:22
oh crap format
# @current_region
def show
if @current_brand == Brand.gaggleup
@deals = Deal.select('distinct deals.*').active.approved.in_region_incl_parents(@current_region).started.order("starts_at desc").limit(15)
@header_title = t('_gup', :default => 'GaggleUp')
@deals = @deals.delete_if do |deal|
true unless check_for_exclusive_deal_access(deal, current_user)
end
@rgould
rgould / gist:2997230
Created June 26, 2012 17:25
Print out sql queries when they happen.
ANSI = {}
ANSI[:RESET] = "\e[0m"
ANSI[:BOLD] = "\e[1m"
ANSI[:UNDERLINE] = "\e[4m"
ANSI[:LGRAY] = "\e[0;37m"
ANSI[:GRAY] = "\e[1;30m"
ANSI[:RED] = "\e[31m"
ANSI[:GREEN] = "\e[32m"
ANSI[:YELLOW] = "\e[33m"
ANSI[:BLUE] = "\e[34m"
var CAS = require('cas')
, express = require('express');
var cas = new CAS({
base_url: 'https://test.littlevikinggames.com',
service: 'http://test.littlevikinggames.com:4000'
});
@rgould
rgould / gist:2580751
Created May 2, 2012 21:44
Refactor the UsersController

Standing at the edge of the Cliffs of Refactoring, he surveyed the landscape. He saw the English settlers overwhelming the French colonists in the Valley of Config; He saw the magnificent View Mountains, though some of the peaks looked almost identical; He saw the neat little town of Model, nestled by the river Rails, with smoke drifting out of the chimneys. From his vantage point, he could see deep in the Chasm of Controllers, where his eyes saw a figure emerging from a dark cave. It was the UsersController! Once a renowned and beautiful entity, it now resembles a shambling, mutating monster. It came writhing out of the cave, dragging by a leash the AccountsController, savagely beating it with a wielded club. He averted his gaze, sunk his head into his hands, and said "The horror, the horror."