Skip to content

Instantly share code, notes, and snippets.

View liushooter's full-sized avatar

Shooter liushooter

View GitHub Profile
@liushooter
liushooter / BaseModel.rb
Last active August 29, 2015 14:18
💋
# /app/models/mongoid/base_model.rb
module Mongoid
module BaseModel
extend ActiveSupport::Concern
included do
scope :recent, -> { desc(:create_at) }
end
@liushooter
liushooter / MagicString
Created April 3, 2015 01:00
💋 邹倩雯
class MagicString < String
def +@
upcase
end
def -@
downcase
end
def !
@liushooter
liushooter / mongoid_aggregate.rb
Last active August 29, 2015 14:18
mongoid 如何 使用 mongoDB aggregate功能 💋
class User
include Mongoid::Document
include Mongoid::Timestamps
def self.added_fans(day_num)
self.collection.aggregate(
{
"$match" => {
"followable_type" => "User",
require "active_support"
require "rest_client"
url ="http://media.vimcasts.org/videos/index.json"
result = RestClient.get url
json_obj = ActiveSupport::JSON.decode result
json_obj.each do |json |
media_url = json.last["quicktime"]["url"]
module Retriable
# This will catch any exception and retry twice (three tries total):
# with_retries { ... }
#
# This will catch any exception and retry four times (five tries total):
# with_retries(:limit => 5) { ... }
#
# This will catch a specific exception and retry once (two tries total):
# with_retries(Some::Error, :limit => 2) { ... }
#
@liushooter
liushooter / SassMeister-input.scss
Created June 7, 2015 02:13
Generated by SassMeister.com.
// ----
// libsass (v3.2.4)
// ----
//定義方法 animation-delay-n
@mixin animation-delay-seconds($n){
&:nth-child(#{$n}){
animation-delay: #{$n*2}s;
}
}
@liushooter
liushooter / GrapeRender.rb
Created June 13, 2015 02:59
💋 扩展grape的formatter, 支持返回 html
module Grape
module ContentTypes
def self.content_types_for(from_settings)
if from_settings.present?
from_settings
else
ActiveSupport::OrderedHash[
:xml, 'application/xml',
:serializable_hash, 'application/json',
:json, 'application/json',
proxy_set_header X-Real-IP $remote_addr;
# 然后在Grape 里面用 headers['X-Real-Ip'] 就可以取到 remote ip 了
class ExampleCrawler < PoltergeistCrawler
def crawl
visit "https://news.ycombinator.com/"
click_on "More"
page.evaluate_script("window.location = '/'")
end
end
ExampleCrawler.new.crawl
@liushooter
liushooter / spider.rb
Last active August 29, 2015 14:24
爬虫 💋
require 'open-uri'
require 'nokogiri'
require 'capybara/poltergeist'
url = "http://mp.weixin.qq.com/s?__biz=MzA3NDMyOTcxMQ==&mid=209031164&idx=1&sn=606c865bfbc982029b2b71731304d7bb&3rd=MzA3MDU4NTYzMw==&scene=6#rd"
Capybara.default_driver = :poltergeist
Capybara.run_server = false
doc = Nokogiri::HTML(open(url))