Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / change_s3_metadata.rb
Last active August 26, 2016 01:40
Change metadata on existing S3 files
# Using v1 of Ruby aws-sdk as currently v2 seems not able to do this (broken?).
require 'aws-sdk-v1'
key = YOUR_AWS_KEY
secret = YOUR_AWS_SECRET
region = YOUR_AWS_REGION
AWS.config(access_key_id: key, secret_access_key: secret, region: region)
s3 = AWS::S3.new
bucket = s3.buckets[bucket_name]
@joost
joost / json_validator.rb
Created March 26, 2015 14:13
Rails 3 / Rails 4 JSON validator
# Put this code in lib/validators/json_validator.rb
# Usage in your model:
# validates :json_attribute, presence: true, json: true
#
# To have a detailed error use something like:
# validates :json_attribute, presence: true, json: {message: :some_i18n_key}
# In your yaml use:
# some_i18n_key: "detailed exception message: %{exception_message}"
class JsonValidator < ActiveModel::EachValidator
@joost
joost / your_model.rb
Created March 27, 2015 11:01
Sunspot reindex without removing the index
class YourModel
# Overwrite sunspot version to not remove from index.
# See: https://github.com/sunspot/sunspot/blob/16e5f620814fd4bdf43e3df158e7fdcbbd92713b/sunspot_rails/lib/sunspot/rails/searchable.rb#L206
def self.solr_reindex(options = {})
solr_index(options)
solr_clean_index_orphans
end
end
@joost
joost / api_controller.rb
Last active July 31, 2018 06:38
Validate parameters in Rails 4 JSON API
class ApiController < ApplicationController
# FIXME: Since we cannot set ActionController::Parameters.action_on_unpermitted_parameters = :raise
# on a controller level we do this hotfix.
class ApiParameters < ActionController::Parameters
def action_on_unpermitted_parameters
:raise
end
end
def params

Keybase proof

I hereby claim:

  • I am joost on github.
  • I am joosth (https://keybase.io/joosth) on keybase.
  • I have a public key whose fingerprint is 3546 2624 A98A C4A1 2E7C 414D ABA4 5856 9272 EE8A

To claim this, I am signing this object:

@joost
joost / resize_boot2docker.sh
Last active December 14, 2022 07:34
Resize boot2docker VirtualBox image
# Steps we will take:
# 1. Change boot2docker image type (this will take long)
# 2. Resize image
# 3. Resize partion (using GParted)
#
# Also see: https://docs.docker.com/articles/b2d_volume_resize/
# Stop boot2docker
boot2docker stop