一个问题,在 ruby-china API 源码中有这样一段,对应 json 是http://ruby-china.org/api/v2/users/dave.json, 这里 json 中的 topics 都没有 id,但代码中是有 id 的。为什么呢?
expose(:topics, :unless => { :collection => true }) do |model, opts|
model.topics.recent.limit(opts[:topics_limit] ||= 1).as_json(:only => [:id, :title, :created_at, :node_name, :replies_count])
end
class DetailUser < Grape::Entity
expose :id, :name, :login, :email, :location, :company, :twitter, :website, :bio, :tagline, :github_url
# deprecated: gravatar_hash, use avatar_url for user avatar
expose(:gravatar_hash) { |model, opts| Digest::MD5.hexdigest(model.email || "") }
expose(:avatar_url) do |model, opts|
if model.avatar?
model.avatar.url(:large)
else
hash = Digest::MD5.hexdigest(model.email || "")
"#{Setting.gravatar_proxy}/avatar/#{hash}.png?s=120"
end
end
expose(:topics, :unless => { :collection => true }) do |model, opts|
model.topics.recent.limit(opts[:topics_limit] ||= 1).as_json(:only => [:id, :title, :created_at, :node_name, :replies_count])
end
end