Skip to content

Instantly share code, notes, and snippets.

View acaporrini's full-sized avatar

Alessandro Caporrini acaporrini

  • Berlin, Germany
View GitHub Profile

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.

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 later to 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 url length limit allowed by the browsers or web servers.

@acaporrini
acaporrini / products_controller_spec.rb
Last active August 29, 2015 14:23
Product controller test
require 'rails_helper'
describe ProductsController, type: :controller do
describe "POST # products" do
before do
@user = User.create(email: "demo@demo.com", password: "12345678")
@user.confirm
sign_in @user
end
it "create a new product" do
@acaporrini
acaporrini / heroku
Created May 25, 2015 13:11
heroku warnings
Counting objects: 15, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (8/8), 633 bytes | 0 bytes/s, done.
Total 8 (delta 5), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Ruby app detected
remote: -----> Compiling Ruby/Rails
class Pet
attr_reader :color, :breed
attr_accessor :name
def initialize(color, breed)
@color = color
@breed = breed
@hungry = true
end
def feed(food)
puts "Mmm, " + food + "!"
class Cat
attr_reader :color, :breed
attr_accessor :name
def initialize(color, breed)
@color = color
@breed = breed
@hungry = true
end
def feed(food)
puts "Mmm, " + food + "!"
def fav_foods
food_array = []
my_fav = ["pizza","pasta","cheese"];
3.times do
puts "Name a favorite food."
food_array << gets.chomp
end
#p food_array
food_array.each do |food|
puts "I like #{food} too!" if my_fav.include?(food.downcase)
@acaporrini
acaporrini / program2.rb
Last active August 29, 2015 14:21
CF 22
if (5+5==10)
puts "this is true"
else
puts "this is false"
end
@acaporrini
acaporrini / hello.rb
Last active August 29, 2015 14:21
CF 21
def greeting
puts "Please enter your name"
name = gets.chomp
puts "hello "+name
end
greeting
@acaporrini
acaporrini / index.html
Last active August 29, 2015 14:20
singlepage
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8">
<title>Home</title>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link href="css/styles.css" rel="stylesheet">
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link href="css/styles.css" rel="stylesheet">
<link rel="stylesheet" href="css/about_styles.css">
<link href='http://fonts.googleapis.com/css?family=Quicksand' rel='stylesheet' type='text/css'>
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>