Skip to content

Instantly share code, notes, and snippets.

View aileron's full-sized avatar

AILERON aileron

View GitHub Profile
@aileron
aileron / gist:2987427
Created June 25, 2012 08:45
RailsでURLヘルパーを、直接取得するには
Rails.application.routes.url_helpers
@aileron
aileron / gist:2946985
Created June 18, 2012 05:27
Gem (active_paypal_adaptive_payment) を使用した事前承認支払いのサンプルモデル
class Approval
include Mongoid::Document
include Mongoid::Timestamps
belongs_to :user
field :item_id
field :key
field :approved, :type=> Boolean, :default => false
def pay
@aileron
aileron / gist:2946980
Created June 18, 2012 05:25
Gem (active_paypal_adaptive_payment) を使用した事前承認支払いのコントローラ サンプル
class PaypalController < ApplicationController
def approval
raise "not loggined" unless login_user
@approval = Approval.find_or_create_by :user_id => login_user.id, :item_id => params[:item_id]
raise "subscribed!" if @approval.key
start_date = Time.now.gmtime
end_date = 3.months.from_now.gmttime
@aileron
aileron / gist:2946977
Created June 18, 2012 05:24
Gem (active_paypal_adaptive_payment) 初期化コード config/initializers/paypal.rb
unless Rails.env.production?
ActiveMerchant::Billing::Base.mode = :test
end
config = YAML.load(ERB.new(File.read("#{Rails.root}/config/paypal.yml")).result)[Rails.env]
PaypalGateway = ActiveMerchant::Billing::PaypalAdaptivePayment.new(
:login => config["username"],
:password => config["password"],
:signature => config["signature"],
:appid => config["application_id"]
)
@aileron
aileron / gist:2894649
Created June 8, 2012 09:14
backgroun-image にグラデを付ける方法
background-color: white;
background-image: url(bg.png);
background-image: url(bg.png), -webkit-gradient(linear, left top, right bottom, from(#000000), to(#ffffff));
background-image: url(bg.png), -moz-linear-gradient(-45deg, #000000, #ffffff);
background-attachment: fixed;
@aileron
aileron / gist:2631920
Created May 8, 2012 01:45
javascriptでなんとなく単語を抽出する
(function() {
var tags = [];
var _tags = {};
var words = document.documentElement.innerText.match(/[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]+|[\u12449-\u12531\u12540;]+|[a-zA-Z0-9!]+|[a-zA-Z>0-9]+/g);
words.forEach(function(word) {
if (word.length <= 3 || _tags[word]) { return ; }
_tags[ word ] = 0;
tags.push( word ); });
delete _tags;
return tags;
@aileron
aileron / gist:2464233
Created April 22, 2012 13:58
accesslog rack モジュール
class AccessLog
class Handler
def initialize(app)
@app = app
end
def call(env)
session = env["rack.session"] && user_id = session["user_id"]
ipaddress = env["REMOTE_ADDR"]
user_agent = env["HTTP_USER_AGENT"]
referer = env["HTTP_REFERER"]
@aileron
aileron / gist:2347987
Created April 10, 2012 02:31
てすと

Hello

@aileron
aileron / gist:2305094
Created April 4, 2012 19:50
sexy enumerator
class Enumerator
def filter(c=nil,&b)
s = self
e = ::Enumerator.new do |y|
s.each do |x|
y << (c||b).call(x)
end
end
return e unless c
e.each &b
@aileron
aileron / gist:2300796
Created April 4, 2012 12:30
短縮URLとか展開する
def extract_tiny_url(url)
url = URI.parse(url) if url.is_a?String
TCPSocket.open(url.host, url.port) do|s|
s.write("GET #{url.path} HTTP/1.1")
s.write("\nHost: #{url.host}")
s.write("\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:11.0) Gecko/20100101 Firefox/11.0")
s.write("\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
s.write("\nAccept-Language: ja-JP-mac,ja;q=0.8,en-US;q=0.5,en;q=0.3")
s.write("\n\n")
text = (s.readlines.join('').scan /URL=([a-z:\/\.\_0-9]*)/).flatten.first