Skip to content

Instantly share code, notes, and snippets.

View kennx's full-sized avatar
:octocat:
iGlow

张远山 kennx

:octocat:
iGlow
View GitHub Profile
@kennx
kennx / output
Last active December 14, 2015 23:38
txt.size
=> 99607
puts txt
<html>
<head>
<title>ocr</title>
<link rel="stylesheet" type="text/css" href="../style.css">
</head>
connection = Faraday::Connection.new('http://example.com') do |builder|
builder.request :url_encoded # for POST/PUT params
builder.adapter :net_http
end
# same as above, short form:
connection = Faraday.new 'http://example.com'
# GET
connection.get '/posts'
@kennx
kennx / Gemfile
Last active December 14, 2015 15:49
为什么
source 'http://ruby.taobao.org'
gem 'sinatra', :require => 'sinatra/base'
gem 'sinatra-reloader', :require => 'sinatra/reloader'
gem 'oauth2'
gem 'thin'
gem 'rake'
@kennx
kennx / cache.rb
Created January 31, 2013 07:38 — forked from alfanick/cache.rb
# Amadeusz Juskowiak <amadeusz@amanointeractive.com> 2012 - MIT License
# 1. Put inside helpers do .. end
# 2. Set CACHE_DIR to somewhere writable and private (like CACHE_DIR=File.dirname(__FILE__) + '/tmp'
# 3. Use it! You can use fragment_expire to remove cache with given name.
#
# Example:
# %h1 foo
# = cache_fragment(:report, 300) do
# - data = get_data_slowly
require 'sinatra/base'
class CacheHelper
module Sinatra
module Helpers
def cache(name, options = {}, &block)
if cache = read_fragment(name, options)
@_out_buf << cache
else
pos = @_out_buf.length
require 'sinatra/base'
require 'memcached'
module Sinatra
module Memcacher
module Helpers
def cache(key, &block)
return block.call unless options.memcacher_enabled
begin
def resize_and_crop(image, size)
if image.width < image.height
remove = ((image.height - image.width)/2).round
image.shave("0x#{remove}")
elsif image.width > image.height
remove = ((image.width - image.height)/2).round
image.shave("#{remove}x0")
end
image.resize("#{size}x#{size}")
return image
@kennx
kennx / resize_and_crop.rb
Created September 20, 2012 08:17
裁切指定尺寸大小图片
def crop_image_fixed_size(file, file_width, file_height)
begin
image = MiniMagick::Image.new(file)
original_width,original_height = image[:width],image[:height]
if original_width*file_height != original_height*file_width
if original_width*file_height >= original_height*file_width
original_height_clone = original_height
original_width_clone = original_height_clone * (file_width.to_f / file_height)
elsif original_width*file_height <= original_height * file_width
original_width_clone = original_width
@kennx
kennx / mini_magick_crop_image.rb
Created September 20, 2012 08:03
mini_magick 裁切正方形图片
# mini_magick 裁切正方形图片 sinatra
def crop_thumb_image(file)
begin
image = MiniMagick::Image.new(file)
image_width,image_height = image[:width],image[:height]
if image_width > image_height
shaved = (image_width-image_height).abs / 2
image.shave("#{shaved}x0")
else
# categorization.rb
class Categorization < ActiveRecord::Base
attr_accessible :category_id, :post_id
belongs_to :category
belongs_to :post
end
# category.rb