Skip to content

Instantly share code, notes, and snippets.

View lulalala's full-sized avatar

lulalala lulalala

View GitHub Profile
@lulalala
lulalala / gist:3652906
Created September 6, 2012 08:15
台灣住址行政區域分類
class TaiwaneseAddress
def initialize(address)
@original_address = address.clone()
@levels = prepare_level()
@levels.each do |level|
level.keys.each do |region|
if match = address[/^(.{1,4}#{region})/,1]
level[region] = match
@lulalala
lulalala / rspecgrep.sh
Created November 2, 2012 09:36
rspec with grep to run selected specs
function rspecgrep()
{
SPK_FILES=`find spec -type f -name "*_spec.rb" | grep $1`
echo $SPK_FILES
time rspec $SPK_FILES --format documentation
}
@lulalala
lulalala / wretch_to_mt.xslt
Last active December 12, 2015 01:38
能把無名小站的 xml 備份轉換成 Moveable Type 格式的 xslt 檔案
<xsl:stylesheet version="1.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
>
<!-- lulalala 2008/10/16 created -->
<xsl:output method="text" encoding="UTF-8"/>
@lulalala
lulalala / redirect_if_mobile
Created May 6, 2013 09:25
Javascript to redirect to mobile subdomain, when using Ernie Miller's methods: http://erniemiller.org/2011/01/05/mobile-devices-and-rails-maintaining-your-sanity/
if (document.cookie.indexOf("full_site=1") === -1 && navigator.userAgent.match(/iPhone|iPod|iPad|Android/i)) {
window.location = document.URL.replace(/:\/\//, '://m.');
}
@lulalala
lulalala / ConditionalValidations.rb
Last active December 17, 2015 13:39 — forked from ibanez270dx/ConditionalValidations.rb
Add association conditional validation
#
# At CoverHound, we use conditional validations all over the form. However, there is no proper way to do
# this in Rails. Instead, we can provide an array of attributes (validated_fields attribute)
# and ensure they are the only ones to get validated.
#
module ConditionalValidations
attr_accessor :validated_fields
def field_is_required?(field)
@lulalala
lulalala / nokogiri_ext.rb
Created June 28, 2013 03:54
Every web crawler using Nokogiri should have this. This simplifies the page fetching call, and adds redirected url automatically so you can reference that url later (like when you have to compute absolute url out of relative href values).
require 'open-uri'
class Nokogiri::HTML::Document
# Use open-uri to get document and set url accordingly.
def self.get(url)
raw = open(url)
parse(raw, raw.base_uri.try(:to_s)) # handles redirected url
end
end
module Nokogiri::HTML
def self.get(url)
@lulalala
lulalala / banner.rb
Last active January 14, 2019 12:58 — forked from matheusvetor/banner.rb
Carrierwave image model validation on image dimensions/height/width.
class Banner < ActiveRecord::Base
attr_accessor :image_width, :image_height
mount_uploader :image, ImageUploader
validate :check_dimensions, :on => :create
def check_dimensions
if !image_cache.nil? && (image.width < 300 || image.height < 300)
errors.add :image, "Dimension too small."
end
end
@lulalala
lulalala / yahoo_api_example.json
Created August 15, 2013 07:41
Tiger der Yahoo BUY API
{"categories"=>{"count"=>"0"}}
{"categories"=>{"category"=>{"no"=>"76044", "level_no"=>"4", "name"=>"NANING9", "type"=>"區"}, "count"=>"1"}}
{"categories"=>{"category"=>[{"no"=>"17746", "level_no"=>"4", "name"=>"Fashion Focus", "type"=>"區"}, {"no"=>"14482", "level_no"=>"4", "name"=>"MM25下身系列", "type"=>"區"}], "count"=>"2"}}
@lulalala
lulalala / logdown
Last active December 21, 2015 22:09
grammar correction
Hey, fellow hackers!
This week we are bringing "Custom Theme" to you !
1. Starting today, we now support custom themes. You can customize your own template by going to Theme's setting :
http://logdown.com/account/blogs/lulalala/themes
* See announcement here: New Feature: Custom Theme
@lulalala
lulalala / 中華民國地理極點經緯值
Created September 11, 2013 06:48
中華民國地理極點經緯值,可用來辨別一個經緯度是否在範圍內。
最北 西引島北固礁
26.38306, 120.47611
最南 七星岩
21.758883, 120.824706
最東 赤尾嶼
25.921667, 124.558333
最西 金門縣烈嶼鄉