Skip to content

Instantly share code, notes, and snippets.

@ccyrille
ccyrille / anonymous_crawler.rb
Created December 29, 2012 15:23
Use the Tor network to parse web content anonymously...
require 'nokogiri'
require 'socksify'
# Configure SOCKSify to use local Tor proxy
TCPSocket::socks_server = "127.0.0.1"
TCPSocket::socks_port = 9050
# Make sure that we use the Tor network
begin
doc = Nokogiri::HTML(open("https://check.torproject.org"))
@ccyrille
ccyrille / carrier_wave.rb
Last active December 10, 2015 08:28
CarrierWave custom processor to apply a white background to a picture. Useful to avoid black background when dealing with transparent images.
require 'mini_magick'
module CarrierWave
module ClassMethods
# Custom processor to apply a white background to a picture
def apply_white_background
process :apply_white_background
end
end
@ccyrille
ccyrille / rest_api_model.rb
Created December 29, 2012 14:31
Class to be inherited by models on top of REST API endpoints
require 'httparty'
# ApiModel could be inherited by all models on the top of a RESTfull API
#
# Note: Inheriting models should at least define the "api_collection_name"
#
# == Example
#
# class Product < RestApiModel
# api_collection_name "products"
@ccyrille
ccyrille / http_cacheable.rb
Created May 10, 2012 14:18
Module to generate HTTP 'cache-control' header
module HTTPCacheable
def self.included(base)
base.extend ClassMethods
default_options = {
:http_cache_directive => 'public',
:http_cache_expiration => 300
}
base.instance_variable_set("@default_options", default_options)
end
@ccyrille
ccyrille / my_helper.rb
Created May 5, 2012 08:22
Helper to ignore asset pipeline exceptions when rendering images (for example, 'Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError')
module MyHelper
def image_tag_without_exception image_name, opts = {}
begin
return image_tag(image_name, opts)
rescue
# DO NOTHING
end
return ""
end
end