Skip to content

Instantly share code, notes, and snippets.

require 'net/http'
require 'uri'
def get_html_content(requested_url)
url = URI.parse(requested_url)
full_path = (url.query.blank?) ? url.path : "#{url.path}?#{url.query}"
the_request = Net::HTTP::Get.new(full_path)
the_response = Net::HTTP.start(url.host, url.port) { |http|
http.request(the_request)
@btelles
btelles / split_into.rb
Created January 23, 2010 20:01
Split a hash into multiple equal-sized hashes
# Extend hashes by adding one method that allows you to split a hash into an array of equal-sized hashes.
# Filter items to be included in the result by adding a selecting block that returns true
# if the key/value is to be kept.
# Per the usage example, return values are not ordered unless you're using Ruby 1.9
# usage:
# a = {1 => 'a', 2 => 'b', 3 => 'c', 4 => 'd', 5 => 'e'}
# a.split_into(3) {|k,v| k.instance_of?(Integer)}
@lxneng
lxneng / gist:741932
Created December 15, 2010 13:21
install PostgreSQL 9 in Mac OSX via Homebrew
install PostgreSQL 9 in Mac OSX via Homebrew
Mac OS X Snow Leopard
System Version: Mac OS X 10.6.5
Kernel Version: Darwin 10.5.0
Install notes for PostgreSQL 9.0.1 install using Homebrew:
sh-3.2# brew install postgresql
@fajrif
fajrif / rvm_cheatsheet
Created June 14, 2011 14:11
RVM cheatsheet
RVM home page: http://rvm.beginrescueend.com
Install RVM
------------
See http://rvm.beginrescueend.com/rvm/install/
bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
Install rvm for all users
@lbj96347
lbj96347 / JS-Regular.html
Created February 1, 2012 06:06
JS正则提取字符串/中文/英文/数字
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>javaScript正则表达式提取字符串中字母、数字、中文</title>
</head>
<body>
<input type="text" id="oText" value="李秉骏的常用代号是96347,英文名字是CashLee噢" size=100><br/>
<input type="button" value="number" onclick="get_character_you_want(this);">
@webmat
webmat / dashboards.rb
Created February 22, 2012 20:46
First draft of an active_admin-based view for Delayed::Job
ActiveAdmin::Dashboards.build do
# Add this section in your dashboard...
section "Background Jobs" do
now = Time.now.getgm
ul do
li do
jobs = Delayed::Job.where('failed_at is not null').count(:id)
link_to "#{jobs} failing jobs", admin_jobs_path(q: {failed_at_is_not_null: true}), style: 'color: red'
end

Sublime Text 2 - Useful Shortcuts

Tested in Mac OS X: super == command

Open/Goto


  • super+t: go to file
  • super+ctrl+p: go to project
  • super+r: go to methods
@traviskroberts
traviskroberts / gist:2830535
Created May 29, 2012 20:33
FactoryGirl Polymorphic Association
class Alert < ActiveRecord::Base
belongs_to :alertable, :polymorphic => true
end
class Region < ActiveRecord::Base
has_many :alerts, :as => :alertable
end
@justinweiss
justinweiss / filterable.rb
Last active January 11, 2024 07:28
Filterable
# Call scopes directly from your URL params:
#
# @products = Product.filter(params.slice(:status, :location, :starts_with))
module Filterable
extend ActiveSupport::Concern
module ClassMethods
# Call the class methods with names based on the keys in <tt>filtering_params</tt>
# with their associated values. For example, "{ status: 'delayed' }" would call
@jonmagic
jonmagic / email.rb
Last active December 20, 2022 17:17
A handy Rails model for storing emails with a little logic to let a User have multiple email addresses but a single primary address.
class Email < ActiveRecord::Base
# Nope, it's not RFC compliant. F*** that regex.
# http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html
EmailRegex = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i.freeze
before_validation :strip_spaces
# Public: The email address.
# column :address