Skip to content

Instantly share code, notes, and snippets.

@Martin91
Martin91 / port_server.rb
Last active August 29, 2015 14:06
ruby network address and por detecter
#!/usr/bin/ruby
require 'socket'
class PortServer
attr_reader(:port,:server)
def initialize(port)
@port = port
end
@Martin91
Martin91 / pin_payments_subscriptions_research.md
Created September 3, 2014 04:19
Pin Payments Subscriptions Integration Research
  1. Sync plans List all active plans
[8] pry(main)> RSpreedly::SubscriptionPlan.active
=> [#<RSpreedly::SubscriptionPlan:0x000001051e4be0
  @description="<description>",
  @duration_quantity=12,
  @duration_units="months",
 @id=25113,
@Martin91
Martin91 / environment_config.rb
Created August 30, 2014 05:46
use custom fonts in rails
# Using fonts in Rails
config.assets.paths << Rails.root.join('app', 'assets', 'fonts')
config.assets.precompile << /\.(?:svg|eot|woff|ttf)$/
/*
_ooOoo_
o8888888o
88" . "88
(| -_- |)
O\ = /O
____/`---'\____
.' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
@Martin91
Martin91 / octopress_pdf_tag.rb
Created August 19, 2014 15:40
Support octopress project to generate pdf previews from pdf files to png images, and render <img> tags
# Title: PDF previewer for Octopress
# Author: Martin (http://martin91.github.io/)
# Description: Convert a pdf file into a series of images, and then output a html
# <ul> block with each <li></li> correspond to each image orderly.
#
# Syntax {% pdf_tag relative/path/to/pdf/file/under/source/directory %}
#
# Example:
# {% pdf_tag WebRTC introduction %}
#
@Martin91
Martin91 / final_version.rb
Last active August 29, 2015 14:04
simple socket server to loop accept connections
require 'socket'
Socket.tcp_server_loop(4481) do |connection| # 处理连接。
# 必要的处理
connection.close
end
@Martin91
Martin91 / auto_set_tab_chrome_background_color.sh
Last active August 29, 2015 14:04
set iTerm2 tab chrome background color according to the current working directory
function auto_set_tab_chrome_background_color {
# Red component value is calculated from the full path of current directory
RR=`pwd | wc -m`
let RR=(RR%25)*10
# Green component value is calculated from the basename of current directory
GG=`basename \`pwd\` | wc -m`
let GG=(GG%25)*10*2
# Blue component value is calculated from the full total files and directories count under this directory
@Martin91
Martin91 / state_machine.rb
Last active August 29, 2015 14:04
implement orders' common state machine through state_machine or workflow
# state_machine: https://github.com/pluginaweek/state_machine
class Order < ActiveRecord::Base
state_machine :state, initial: :cart do
before_transition on: :checkout, do: :reduce_buyer_balance
after_transition on: :dispatch_goods, do: :create_shipping_log
after_transition on: :confirm, do: :charge_to_seller
event :checkout do
transition :cart => :paid
@Martin91
Martin91 / weibo_oauth2.rb
Last active August 29, 2015 14:04
methods pair to finish OAuth2 flow on weibo.com
# Attention: the below methods are actions extracted from my controllers in a Rails App, if you need to use these codes outside
# a Rails project, rememeber to replace some Rails-specified methods, such as, Hash#to_param.
# STEP 1: start to request an authorization
def request_authorize
session[:security_state] = 'I am from Martin site'
authorize_url = "https://api.weibo.com/oauth2/authorize"
params = {
client_id: "663911642", # Required: Your assigned App Key
@Martin91
Martin91 / conditions.rb
Created July 19, 2014 01:57
support classed to have abilities to determine if its instances match all or any conditions
module Conditions
def self.included(base)
class << base
attr_reader :conditions_map
def condition(name, &block)
@conditions_map ||= Hash.new
@conditions_map.store(name, block)
end