This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /app/models/mongoid/base_model.rb | |
module Mongoid | |
module BaseModel | |
extend ActiveSupport::Concern | |
included do | |
scope :recent, -> { desc(:create_at) } | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MagicString < String | |
def +@ | |
upcase | |
end | |
def -@ | |
downcase | |
end | |
def ! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
def self.added_fans(day_num) | |
self.collection.aggregate( | |
{ | |
"$match" => { | |
"followable_type" => "User", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { ... } | |
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---- | |
// libsass (v3.2.4) | |
// ---- | |
//定義方法 animation-delay-n | |
@mixin animation-delay-seconds($n){ | |
&:nth-child(#{$n}){ | |
animation-delay: #{$n*2}s; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proxy_set_header X-Real-IP $remote_addr; | |
# 然后在Grape 里面用 headers['X-Real-Ip'] 就可以取到 remote ip 了 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ExampleCrawler < PoltergeistCrawler | |
def crawl | |
visit "https://news.ycombinator.com/" | |
click_on "More" | |
page.evaluate_script("window.location = '/'") | |
end | |
end | |
ExampleCrawler.new.crawl |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |