直接定义一个 format 格式的函数 helper 也可以。这样也统一了返回格式。
def format(data, msg="ok", code=200)
{data: data, msg: msg, code: code}
end
#2 楼 @jzlikewei 把图片名字 hash 或许可以
#2 楼 @jzlikewei 把图片名字 hash 或许可以
直接 grape 吧,如果要在上面写 api 文档就加上 sinatra(web 页面)和 SwaggerUI(api 的 ui 界面)
如果是你带着做,我觉得就合格了。
哈哈,以前是 wordpress,很久的事了。你再去看他的论坛。
#6 楼 @chairy11 你的 blog 挂掉啦!!! http://topit.me/
ruby 的相关 gem 有 curb(curl) 适合抓取 json 通信类数据,mechanize 适合抓取少 js 和 iframe 类页面,watir 貌似只能用来做测试,直接模拟浏览器,但是速度可想而知。phantomjs 据说很不错,没用过。
rails s 应该就是 webrick。楼主真要这么干?还是换 thin 或者 puma 吧,或者省事点 apache+passenger
浏览器自带的就够啦。
nginx+passenger+mysql 的:https://ruby-china.org/topics/20216 apache+php+mysql+passenger 的:http://flowerwrong.iteye.com/blog/2105093
补充 watir 版本
# -*- coding: utf-8 -*-
require 'watir-webdriver'
b = Watir::Browser.new
USER_NAME = "[email protected]"
PASS = "xxx"
login_url = "https://ruby-china.org/account/sign_in"
b.goto login_url
b.text_field(:name => 'user[login]').set USER_NAME
b.text_field(:name => 'user[password]').set PASS
b.button(:name => 'commit').click
puts b.text
#1 楼 @saiga 我改了下,先 get 取得 token,然后 post,可是还是不行
require 'curb'
require 'nokogiri'
USER_NAME = "[email protected]"
PASS = "xxx"
login_url = "https://ruby-china.org/account/sign_in"
c = Curl::Easy.new
c.url = login_url
c.http_get
doc = Nokogiri::HTML(c.body_str)
csrf_token = doc.xpath("//meta[@name='csrf-token']/@content")[0].value.strip
c.url = login_url
c.enable_cookies = true
c.cookiejar = "cookies.txt"
user = {
"user\[login\]" => USER_NAME,
"user\[password\]" => PASS,
"authenticity_token" => csrf_token,
"user\[remember_me\]" => 0,
"utf8" => "✓",
"commit" => "登陆"
}
c.http_post(c.url,user)
puts csrf_token
puts "---" * 20
puts c.header_str
打印结果
flowerwrong@flowerwrong:~/dev/ruby/mechanize$ ruby ruby_china.rb
2O85a3ZBPvdTyo3XQNvvvVlg57VLQ/+vhm3ktyIXXbE=
------------------------------------------------------------
HTTP/1.1 200 OK
Server: nginx/1.6.0
Date: Tue, 19 Aug 2014 10:09:45 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Strict-Transport-Security: max-age=31536000
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
ETag: "c889b4d41d8ddd454f2d022cdc8ef457"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: ffce7524-a903-4844-943e-0b54b872ccfc
X-Runtime: 0.042324
RAILS_ENV=production rake db:migrate 默认是 test+development
js 或者 ruby 的可以操作 html 的 gem,遍历一次,替换吧。
#5 楼 @blacktulip 哈哈
@billy 印象最深的就是 activerecord,不用声明对象属性。还没写过,楼主主要用在那些地方?
我怎么觉得 ruby 东西太少呢?