对照看了 RubyChina 的 Github 代码库的 API,和 Twitter 的 user_timeline 是一致的,代码改个字段名称直接就能套用。可是看了国内的微博,多套了一层 statuses,然后我就弄不懂了,如:
# Github 项目
def github_repositories
return [] if self.github.blank?
count = 14
cache_key = "github_repositories:#{self.github}+#{count}+v2"
items = Rails.cache.read(cache_key)
if items == nil
begin
json = open("https://api.github.com/users/#{self.github}/repos?type=owner&sort=pushed").read
rescue => e
Rails.logger.error("Github Repositiory fetch Error: #{e}")
items = []
Rails.cache.write(cache_key, items, :expires_in => 15.days)
return items
end
items = JSON.parse(json)
items = items.collect do |a1|
{
:name => a1["name"],
:url => a1["html_url"],
:watchers => a1["watchers"],
:description => a1["description"]
}
end
items = items.sort { |a1,a2| a2[:watchers] <=> a1[:watchers] }.take(count)
Rails.cache.write(cache_key, items, :expires_in => 7.days)
end
items
end
关键的代码段:
items = items.collect do |a1|
{
:name => a1["name"],
:url => a1["html_url"],
:watchers => a1["watchers"],
:description => a1["description"]
}
要怎么改改呢?