Skip to content

Instantly share code, notes, and snippets.

@joost
joost / solr.rb
Created November 27, 2012 12:38
Solr 4.0 brew recipe
require 'formula'
class Solr < Formula
url 'http://www.apache.org/dyn/closer.cgi?path=lucene/solr/4.0.0/apache-solr-4.0.0.tgz'
homepage 'http://lucene.apache.org/solr/'
sha1 '0cb61d9572516fc627785201b79b3a85e95f877d'
def script; <<-EOS.undent
#!/bin/sh
if [ -z "$1" ]; then
@joost
joost / search_hash.rb
Created November 28, 2012 12:27
Convert a params Hash to a Sunspot search.
# SearchHash converts a Hash with search options to a Sunspot search.
# Example:
# s = SearchHash.new(Model, :q => 'some string to match in all text fields')
# s.search # The Sunspot search
# s.search.results # The actual results
#
# s = SearchHash.new(Model, :some_field__gt => 12, :text_field_name => 'some query')
#
# Pagination:
# s = SearchHash.new(Model, {:some_field__gt => 12, :text_field_name => 'some query', :page => 2, :per_page => 100}, {:per_page_max => 200, :per_page_default => 25})
@joost
joost / google_timezone.rb
Created January 30, 2013 19:50
Get time zone id of a location by latitude and longitude (lat, lng).
# See: https://developers.google.com/maps/documentation/timezone/
# Uses HTTParty gem (https://github.com/jnunemaker/httparty).
# Usage:
# GoogleTimezone.search(:lat => 51.38494009999999, :lng => -0.3514683)['timeZoneId']
class GoogleTimezone
include HTTParty
base_uri 'https://maps.googleapis.com'
@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 / gist:5354717
Created April 10, 2013 13:40
Languages per TLD
Source: http://www.distilled.net/blog/uncategorized/google-cctlds-and-associated-languages-codes-reference-sheet/
Top level domain Extension Country Name Language Language b
http://www.google.com.af .af Afghanistan fa – Persian ps – Pashto, Pushto
http://www.google.dz .dz Algeria fr – French ar – Arabic
http://www.google.as .as American Samoa en – English
http://www.google.ad .ad Andorra ca – Catalan; Valencian
http://www.google.co.ao .ao Angola pt-PT – Portuguese kg – Kongo
http://www.google.com.ai .ai Anguilla en – English
http://www.google.com.ag .ag Antigua and Barbuda en – English
@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.
@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 / force_non_ssl.rb
Last active January 26, 2017 02:01
Force non SSL Rails Concern. Inverse of force_ssl method. Accepts same options. Add to your app/controllers/concerns/ directory.
# Usage:
# In your (Application)Controller:
# include Concerns::ForceNonSSL
# force_non_ssl
#
# You can use the same options as with force_ssl.
# See: http://api.rubyonrails.org/classes/ActionController/ForceSSL/ClassMethods.html#method-i-force_ssl
#
# Code based on: https://github.com/rails/rails/blob/ab08519b1aed46dbd4b3e13932bbaddfe42d8315/actionpack/lib/action_controller/metal/force_ssl.rb
#
@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 / deploy.rb
Last active July 10, 2024 10:19 — 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}"