Skip to content
Browse files

Update Api for iOS call.

  • Loading branch information...
1 parent b690230 commit aeb49cc9cd9a68a9d2cb4d0bb50888c105c4fe0f @huacnlee huacnlee committed Dec 11, 2012
Showing with 19 additions and 18 deletions.
  1. +1 −1 lib/api.rb
  2. +12 −11 lib/api/entities.rb
  3. +5 −5 spec/api/nodes_spec.rb
  4. +1 −1 spec/api/topics_spec.rb
View
2 lib/api.rb
@@ -58,7 +58,7 @@ class API < Grape::API
@topic = Topic.includes(:replies).find_by_id(params[:id])
# TODO: this code can not run in Mongoid 3.0.1 issue https://github.com/mongoid/mongoid/issues/2204
# @topic = Topic.includes(:replies => [:user]).find_by_id(params[:id])
- present @topic, :with => APIEntities::Topic
+ present @topic, :with => APIEntities::DetailTopic
end
# Post a new reply
View
23 lib/api/entities.rb
@@ -3,9 +3,7 @@
module RubyChina
module APIEntities
class User < Grape::Entity
- expose :_id, :name, :login, :location, :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 :id, :login
expose(:avatar_url) do |model, opts|
if model.avatar?
model.avatar.url(:normal)
@@ -17,7 +15,7 @@ class User < Grape::Entity
end
class DetailUser < Grape::Entity
- expose :_id, :name, :login, :email, :location, :company, :twitter, :website, :bio, :tagline, :github_url
+ 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|
@@ -34,25 +32,28 @@ class DetailUser < Grape::Entity
end
class UserTopic < Grape::Entity
- expose :_id, :title, :body, :body_html, :created_at, :updated_at, :replied_at, :replies_count, :node_name, :node_id, :last_reply_user_login
+ expose :id, :title, :body, :body_html, :created_at, :updated_at, :replied_at, :replies_count, :node_name, :node_id, :last_reply_user_login
end
-
-
class Reply < Grape::Entity
- expose :_id, :body, :body_html, :message_id, :created_at, :updated_at
+ expose :id, :body, :body_html, :message_id, :created_at, :updated_at
expose :user, :using => APIEntities::User
end
-
+
class Topic < Grape::Entity
- expose :_id, :title, :body, :body_html, :created_at, :updated_at, :replied_at, :replies_count, :node_name, :node_id, :last_reply_user_login
+ expose :id, :title, :created_at, :updated_at, :replied_at, :replies_count, :node_name, :node_id, :last_reply_user_id, :last_reply_user_login
+ expose :user, :using => APIEntities::User
+ end
+
+ class DetailTopic < Topic
+ expose :id, :title, :created_at, :updated_at, :replied_at, :replies_count, :node_name, :node_id, :last_reply_user_id, :last_reply_user_login, :body, :body_html
expose :user, :using => APIEntities::User
# replies only exposed when a single topic is fetched
expose :replies, :using => APIEntities::Reply, :unless => { :collection => true }
end
class Node < Grape::Entity
- expose :_id, :name, :topics_count, :summary, :section_id, :sort
+ expose :id, :name, :topics_count, :summary, :section_id, :sort
expose(:section_name) {|model, opts| model.section.name.to_s rescue "" }
end
end
View
10 spec/api/nodes_spec.rb
@@ -4,21 +4,21 @@
describe RubyChina::API, "nodes" do
describe "GET /api/nodes.json" do
before do
- %w(fun ruby nodes).each_with_index { |n, i| Factory(:node, :name => n, :_id => i + 1) }
+ %w(fun ruby nodes).each_with_index { |n, i| Factory(:node, :name => n, :id => i + 1) }
end
it "should return the list of nodes" do
get "/api/nodes.json"
response.status.should == 200
- keyset = ["_id","name"]
+ keyset = ["id","name"]
json = JSON.parse(response.body).each {
|h| h.slice!(*keyset)
}
json.should == [
- {"_id" => 1, "name" => "fun"},
- {"_id" => 2, "name" => "ruby"},
- {"_id" => 3, "name" => "nodes"}
+ {"id" => 1, "name" => "fun"},
+ {"id" => 2, "name" => "ruby"},
+ {"id" => 3, "name" => "nodes"}
]
end
end
View
2 spec/api/topics_spec.rb
@@ -16,7 +16,7 @@
get "/api/topics/node/#{node.id}.json"
json = JSON.parse(response.body)
- json_titles = json.map { |t| t["_id"] }
+ json_titles = json.map { |t| t["id"] }
topics.each { |t| json_titles.should include(t._id) }
other_topics.each { |t| json_titles.should_not include(t._id) }
end

3 comments on commit aeb49cc

@FinnChen

http://ruby-china.org/api/users/:user/topics.json 这个接口返回的json数据中 topics的_id 没替换成 id。

{
id: 1518,
name: "陈锋",
login: "chenfengi",
email: "chenfengole@gmail.com",
location: "",
company: "",
twitter: "",
website: "",
bio: "",
tagline: "",
github_url: "",
gravatar_hash: "56878f9d6fd51d9dd4dc23fb48914c5e",
avatar_url: "http://l.ruby-china.org/user/normal_avatar/1518.gif",
topics: [
{
_id: 5657,
created_at: "2012-09-20T15:39:08+08:00",
node_name: "Rails",
replies_count: 8,
title: "分享一个网站从 JAVA + Mysql 迁移到 Ruby + PostgreSQL的故事"
},
{
_id: 4391,
created_at: "2012-07-18T15:11:17+08:00",
node_name: "瞎扯淡",
replies_count: 3,
title: "新浪上看到的一条微博。看来,国内搞ruby的大牛还是很活跃的哈"
}
]
}
@huacnlee
Ruby China member

@chenfeng163 92105f2 好了,谢谢提醒

@FinnChen

@huacnlee 咋没id了

{
"id": 1518,
"name": "陈锋",
"login": "chenfengi",
"email": "chenfengole@gmail.com",
"location": "",
"company": "",
"twitter": "",
"website": "",
"bio": "",
"tagline": "",
"github_url": "",
"gravatar_hash": "56878f9d6fd51d9dd4dc23fb48914c5e",
"avatar_url": "http://l.ruby-china.org/user/large_avatar/1518.gif",
"topics": [
{
"created_at": "2012-09-20T15:39:08+08:00",
"node_name": "Rails",
"replies_count": 8,
"title": "分享一个网站从 JAVA + Mysql 迁移到 Ruby + PostgreSQL的故事"
},
{
"created_at": "2012-07-18T15:11:17+08:00",
"node_name": "瞎扯淡",
"replies_count": 3,
"title": "新浪上看到的一条微博。看来,国内搞ruby的大牛还是很活跃的哈"
}
]
}
Please sign in to comment.
Something went wrong with that request. Please try again.