我正在尝试使用 Basecamp Classic API ( http://developer.37signals.com/basecamp/comments.shtml )。当前的 basecamp-wrapper 版本很适合我,其中一件事是因为 json 响应包含分页输出,而 xml 响应不包含。这是一个简单的解决方法,但问题是 url 结构没有标准化。
API 指定了一些类似的东西,这让我相信它只是将元素路径和集合路径分开。
Get recent comments (for a commentable resource) GET /#{resource}/#{resource_id}/comments.xml Update comment PUT /comments/#{id}.xml
我在这方面做了几次尝试,但都没有真正成功。尝试处理这样的评论充其量是 hacky,并且实际上不起作用,因为 element_path 与 collection_path 不同。
class Resource < ActiveResource::Base
self.site = "https://XXXX.basecamphq.com"
self.user = "XXXX"
self.password = "X" # This is just X according to the API, I have also read nil works
self.format = :xml # json responses include pagination crap
# Override element path so it isn't nested
class << self
def element_path(id, prefix_options={}, query_options={})
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
"#{collection_name}/#{URI.parser.escape id.to_s}.#{format.extension}#{query_string(query_options)}"
end
end
end
class Project < Resource
end
class Message < Resource
self.element_name = "post"
self.prefix = "/projects/:project_id/"
self.collection_name = "posts"
def comments
@comments ||= Comment.all(:params => {:resource => "posts" , :resource_id => id})
end
end
class Comment < Resource
self.prefix = "/:resource/:resource_id/"
end
puts m = Message.first(:params => {:project_id => PROJECT_ID})
puts m = Message.find(m.id)
puts m.update_attribute(:title, "name")
这一直有效,直到 update_attribute,它实际上正在获取正确的非嵌套 url,并且它正在发出一个失败的 PUT 请求。
为什么这不适用于更新?如何更好地处理不同的父资源?
任何提示都会很棒。:)