Skip to content

Instantly share code, notes, and snippets.

@joost
joost / pixabay_client.rb
Created July 22, 2014 10:37
Supersimple http://pixabay.com/api/docs/ Ruby API client.
require 'hashie'
require 'faraday'
require 'faraday_middleware'
# Supersimple http://pixabay.com/api/docs/ client.
# Usage:
# result = PixabayClient.new(username: 'someone', key: 'your_key').get(search_term: 'beach')
# result.body # Mashified results
# See: https://gist.github.com/joost/e32daf904ed8bd171974
class PixabayClient
@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 / icons8_client.rb
Created June 25, 2014 14:36
Super simple Ruby Icons8 API (http://api.icons8.com/) client
require 'hashie'
require 'faraday'
require 'faraday_middleware'
# Supersimple http://api.icons8.com/ client.
# Usage:
# Icons8Client.new.search('icon')
# See: https://gist.github.com/joost/e149404f645f33ba1939
class Icons8Client
def initialize(options = {})
@joost
joost / google_places_client.rb
Last active August 29, 2015 14:02
Super Simple Google Places API Ruby Client
# This gist is turned into a gem: https://github.com/joost/google_places_api
require 'hashie'
require 'faraday'
require 'faraday_middleware'
# Created our own very simple Google Places API client since the google_places gem
# is returning incorrect results.
# Usage:
# client = GooglePlacesClient.new(key: API_KEY)
# response = client.get('textsearch', query: 'something')
# ref = response.body.results.first.reference
@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 / 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 / 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 / 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 / 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