Skip to content

Instantly share code, notes, and snippets.

@3dd13
3dd13 / ruby_ftp_example.rb
Created November 5, 2011 17:08
Sample code of using Ruby Net::FTP library. Login to FTP server, list out files, check directory existence, upload files
require 'net/ftp'
CONTENT_SERVER_DOMAIN_NAME = "one-of-the-ftp-server.thought-sauce.com.hk"
CONTENT_SERVER_FTP_LOGIN = "saucy-ftp-server-login"
CONTENT_SERVER_FTP_PASSWORD = "saucy-ftp-server-password"
# LOGIN and LIST available files at default home directory
Net::FTP.open(CONTENT_SERVER_DOMAIN_NAME, CONTENT_SERVER_FTP_LOGIN, CONTENT_SERVER_FTP_PASSWORD) do |ftp|
files = ftp.list
@3dd13
3dd13 / guest_user.rb
Last active June 22, 2022 09:16
some helper methods for handling guest user in Rails (with devise) ecommerce website
#
# User Stories:
# To start shopping easily,
# guest user wants to add items to shopping cart without signing up
#
#
# Code Explanation:
# - a guest_user is created when new user arrives to your website, so that he could have his own shopping cart, and adding items to shopping cart.
# - when the guest_user sign up, all items in the shopping cart should be transferred to the new signed up user account
#
@3dd13
3dd13 / generate_ssh_keys.rb
Created January 20, 2011 16:59
chef recipe to generate ssh key for a user
define :generate_ssh_keys, :user_account => nil do
username = params[:user_account]
raise ":user_account should be provided." if username.nil?
Chef::Log.debug("generate ssh skys for #{username}.")
execute "generate ssh skys for #{username}." do
user username
creates "/home/#{username}/.ssh/id_rsa.pub"
@3dd13
3dd13 / row_is_cut_in_the_middle_solution.rb
Created February 18, 2011 07:49
overflow / page break examples of Prawn Pdf generation.
# gem "prawn", "0.8.4"
# Sometime there is no enough space for the last table row when it reaches the end of page
Prawn::Document.generate("text_group_overflow_question.pdf") do |pdf|
add_page_break_if_overflow(pdf) do |pdf|
# generating table here
# ...
end
end
@3dd13
3dd13 / download_open_rice_contact.rb
Created October 19, 2010 08:36
OpenRice.com is a famous restaurant online directory in Hong Kong. Here is a ruby script to grep all the contact information of all shops. at this moment of time, there are around 26K records on the website. this program took ~ 15mins to finish. and
require 'rubygems'
require 'mechanize'
require 'fastercsv'
def get_all_option_values(page, attr_name)
page.search("[name=#{attr_name}]").search("option").map do |opt|
option_value = opt.attribute("value").value
[option_value, opt.text.strip.gsub(/\302\240\302\240/, '')] if option_value && option_value.length > 0
end.compact
end
@3dd13
3dd13 / controllers.js
Created June 15, 2015 12:33
Testing ngCordova geolocation watchPosition and getCurrentPosition function
.controller('DashCtrl', function($scope, $ionicPlatform, $cordovaGeolocation) {
var watch;
var watchOptions = {
timeout : 5000,
maximumAge: 3000,
enableHighAccuracy: true // may cause errors if true
};
var pollCurrentLocation = function() {
$cordovaGeolocation.getCurrentPosition(watchOptions)
@3dd13
3dd13 / Gulpfile.js
Last active March 12, 2016 20:33 — forked from webdesserts/Gulpfile.js
Adapt the gulp hot reload file and apply to partial.js directory structure. (ignored public folder at the moment)
// NOTE: I previously suggested doing this through Grunt, but had plenty of problems with
// my set up. Grunt did some weird things with scope, and I ended up using nodemon. This
// setup is now using Gulp. It works exactly how I expect it to and is WAY more concise.
var gulp = require('gulp'),
spawn = require('child_process').spawn,
node;
/**
* $ gulp server
* description: launch the server. If there's a server already running, kill it.
@3dd13
3dd13 / apple_green_address.rb
Created September 18, 2011 10:03
Scraping the address of "Apple Green" from openrice.com
#
# loading the mechanize library for scraping
# install it if you haven't done it:
# sudo gem install mechanize
#
require 'mechanize'
agent = Mechanize.new
page = agent.get("http://www.openrice.com/english/restaurant/sr2.htm?shopid=32108")
@3dd13
3dd13 / ormat_apn_token.rb
Created December 19, 2012 10:15
Convert the apn token collected from iOS to the format apn_on_rails is expecting
# the apn push token you collected from mobile is in this format:
# 00ea74e76a873e8e9c14c2dd2afe3b42abb35148e94042811e2b6985072641f2
#
# but actually, apn_on_rails is expecting this:
# 00ea74e7 6a873e8e 9c14c2dd 2afe3b42 abb35148 e9404281 1e2b6985 072641f2
def format_apn_token(text)
text && text.gsub(/(.{8})(?=.)/, '\1 \2')
end
@3dd13
3dd13 / Gemfile
Created November 27, 2012 08:38
Runing single ruby file on heroku
source 'https://rubygems.org'