Skip to content

Instantly share code, notes, and snippets.

@sunny0425
sunny0425 / dev.conf
Last active December 26, 2016 08:07 — forked from fnando/dev.conf
Nginx configuration for SSH tunnel
# 在服务器的nginx上添加配置
upstream tunnel {
server 127.0.0.1:3000;
}
server {
listen 80;
server_name dev.example.com;
location / {
@sunny0425
sunny0425 / quick_sort.rb
Last active November 1, 2016 05:26
快速排序
def quick_sort(arr)
len = arr.length
return arr if len == 1
old_index = 0
x = arr[old_index]
if len == 2
if arr[1] < x
@sunny0425
sunny0425 / bubble_sort.rb
Created November 1, 2016 02:09
冒泡排序
def bubble_sort(arr)
len = arr.length
(0..len).each do |i|
j = 0
while j < len - i - 1
if arr[j] > arr[j+1].to_i
x = arr[j]
arr[j] = arr[j+1]
arr[j+1] = x
@sunny0425
sunny0425 / sprite_icons.scss
Last active October 8, 2015 09:09
Sprite mixin, works perfectly with standard defines, refer to: http://www.sitepoint.com/css-techniques-for-retina-displays/
//Sprite mixin, works perfectly with standard defines
@mixin icons-sprite($sprite) {
background-image: sprite-url($icons);
background-position: sprite-position($icons, $sprite);
background-repeat: no-repeat;
overflow: hidden;
display: inline-block;
vertical-align: middle;
margin-right: 3px;
height: round(image-height(sprite-file($icons, $sprite)));
@sunny0425
sunny0425 / uploader.rb
Created November 19, 2013 03:44
use carrirewave and mini-magick to change the png image's color
class Uploader < CarrierWave::Uploader::Base
version :full do
process resize_to_fit: [960, 640]
process :set_color
end
def set_color
color = 'gold'
# convert old.png -alpha off -fill gold -colorize 100% -alpha on new.png
#!/bin/bash
db=$1
uploads=$(dirname $0)/../public/uploads
if [ ! -d $uploads ]; then
echo "Error: directory 'public/uploads' does not exist"
exit 1
fi
@sunny0425
sunny0425 / google_contact.rb
Last active December 10, 2015 04:08
use oauth 2.0 to get google contact list (use httparty gem)
# access_token = "abcdefg123456hijklmn"
# curl -H "Authorization: OAuth abcdefg123456hijklmn" "https://www.google.com/m8/feeds/contacts/default/full?alt=json&v=3.0"
# https://developers.google.com/google-apps/contacts/v3/
# https://developers.google.com/google-apps/contacts/v3/reference
# OAuth 2.0 Playground: https://developers.google.com/oauthplayground/
# Get a contact photo: http://stackoverflow.com/questions/5983484/google-api-getting-a-contacts-photo
def contact_list
url = "https://www.google.com/m8/feeds/contacts/default/full"
response = HTTParty.get(url,
:query => {:alt => "json", :"max-results" => "100000000"},
@sunny0425
sunny0425 / city_to_postal_code.rb
Created December 12, 2012 04:01
use geocoder(gem) to find postal code from a city
Geocoder.coordinates("Shanghai, China")
=> [31.230393, 121.473704]
results = Geocoder.search('31.230393, 121.473704')
r = results.first
r.postal_code
@sunny0425
sunny0425 / mongo_aggregate.rb
Created December 5, 2012 08:43
mongo aggregate script
# Here comments are embedded_in letters
$ mongo
> db.letters.aggregate(
{ $unwind : "$comments"},
{ $match: { "comments.created_at": { $gt: new Date('01/01/2012') } } },
{ $group: {
_id : "$_id",
comment_count: { $sum : 1 }
} },
{ $sort : { comment_count : -1 }},
@sunny0425
sunny0425 / mongo_aggregate.rb
Created December 4, 2012 10:30
run mongo script in rails console
$ mongo
> db.view_logs.aggregate(
{$match : {created_at : {$gt : new Date( "12/01/2012" )}}},
{ $group : {
_id : "$loggable_id",
cc : { $sum : 1 },
}},
{ $sort : { cc : -1 }},
{ $limit : 5 }
);