Skip to content

Instantly share code, notes, and snippets.

module Feedzirra
class AtomEntry
include SAXMachine
element :title
# the :as argument makes this available through atom_entry.author instead of .name
element :name, :as => :author
element "feedburner:origLink", :as => :url
element :summary
element :content
element :published
@awesome
awesome / opposite_of_array_intersection.rb
Created January 21, 2010 17:44
Ruby opposite of array intersection
# Ruby opposite of array intersection... or maybe the method is missing from my brain bc not enough coffee
# http://twitter.com/soawesomeman/status/8035087261
def awesome(ar_1, ar_2)
(ar_1 + ar_2) - (ar_1 & ar_2)
end
awesome([1,2,3,4], [3,4,5,6]) # => [1, 2, 5, 6]
require 'rubygems'
require 'nokogiri'
class PushDocument < Nokogiri::XML::SAX::Document
attr_reader :document, :current, :parents, :errors, :warnings
def cdata_block(string)
current << Nokogiri::XML::CDATA.new(document, string)
end
@bowsersenior
bowsersenior / stooge_loader.rb
Created May 18, 2011 23:18
A demo of YAML anchors, references and nested values
require 'rubygems'
require 'yaml'
# A demonstration of YAML anchors, references and handling of nested values
# For more info, see:
# http://atechie.net/2009/07/merging-hashes-in-yaml-conf-files/
stooges = YAML::load( File.read('stooges.yml') )
# => {
# "default" => {
@aliang
aliang / mymodel.rb
Created June 13, 2011 06:25
render from model in Rails 3
# yes, sometimes you need to do this. you get pilloried in a forum if you
# ask about it, though!
# this code taken from
# http://wholemeal.co.nz/blog/2011/04/05/rendering-from-a-model-in-rails-3/
class MyModel < ActiveRecord::Base
# Use the my_models/_my_model.txt.erb view to render this object as a string
def to_s
ActionView::Base.new(Rails.configuration.paths.app.views.first).render(
@seanaedmiston
seanaedmiston / devise.rb
Created August 21, 2011 10:53
Devise Omniauthable
...
# ==> Configuration for any authentication mechanism
# Configure which keys are used when authenticating a user. The default is
# just :email. You can configure it to use [:username, :subdomain], so for
# authenticating a user, both parameters are required. Remember that those
# parameters are used only when authenticating and not when retrieving from
# session. If you need permissions, you should implement that in a before filter.
# You can also supply a hash where the value is a boolean determining whether
# or not authentication should be aborted when the value is not present.
config.authentication_keys = [ :login ]
@specialunderwear
specialunderwear / babel.sh
Created September 15, 2011 08:58
Install all utf8 locales on ubuntu
#! /bin/sh
cd /usr/share/locales
./install-language-pack eo
./install-language-pack ia
./install-language-pack ie
./install-language-pack io
./install-language-pack vo
./install-language-pack ca
@lucatironi
lucatironi / api.rb
Created December 1, 2011 16:12
Snippets for an API with grape
# Server App
# This file must be in lib/myapp/api.rb
module MyApp
module Entities
class Products < Grape::Entity
expose :id, :code, :name, :short_description
expose :description, :unless => { :collection => true }
expose (:category) { |model, options| model.category.name }
expose (:brand) { |model, options| model.brand.name }
end
@nicferrier
nicferrier / gist:2277987
Created April 1, 2012 19:34
Clone a git repo if it does not exist, or pull into it if it does exist
#!/bin/sh
REPOSRC=$1
LOCALREPO=$2
# We do it this way so that we can abstract if from just git later on
LOCALREPO_VC_DIR=$LOCALREPO/.git
if [ ! -d $LOCALREPO_VC_DIR ]
then
@hakanensari
hakanensari / Results
Created July 9, 2012 21:07
Benchmarking Ruby SAX parsing with Ox and Nokogiri
☁ code ruby sax_parsers.rb
Rehearsal --------------------------------------------
ox 0.800000 0.020000 0.820000 ( 0.820952)
nokogiri 2.110000 0.020000 2.130000 ( 2.144550)
----------------------------------- total: 2.950000sec
user system total real
ox 0.790000 0.010000 0.800000 ( 0.808858)
nokogiri 2.080000 0.030000 2.110000 ( 2.239248)