Ruby on rails 如何在ActiveResource中使用当前的Basecamp API?
我正在尝试使用Basecamp经典API()。当前的basecamp包装器版本让我很满意,其中一个原因是json响应包含分页输出,而xml响应不包含分页输出。这是一个很容易解决的问题,但问题是url结构没有标准化 API指定了一些类似的东西,这让我相信它只是简单地将元素路径和集合路径分开 我已经尝试了好几次,但都没有成功。试图处理这样的评论充其量只是一种黑客行为,实际上是行不通的,因为元素路径与集合路径不同Ruby on rails 如何在ActiveResource中使用当前的Basecamp API?,ruby-on-rails,ruby-on-rails-3,api,activeresource,basecamp,Ruby On Rails,Ruby On Rails 3,Api,Activeresource,Basecamp,我正在尝试使用Basecamp经典API()。当前的basecamp包装器版本让我很满意,其中一个原因是json响应包含分页输出,而xml响应不包含分页输出。这是一个很容易解决的问题,但问题是url结构没有标准化 API指定了一些类似的东西,这让我相信它只是简单地将元素路径和集合路径分开 我已经尝试了好几次,但都没有成功。试图处理这样的评论充其量只是一种黑客行为,实际上是行不通的,因为元素路径与集合路径不同 class Resource < ActiveResource::Base s
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")
类资源posts',:resource_id=>id})
结束
结束
类注释<资源
self.prefix=“/:resource/:resource\u id/”
结束
将m=Message.first(:params=>{:project\u id=>project\u id})
puts m=Message.find(m.id)
放置m.update_属性(:title,“name”)
这一直持续到update_属性,该属性实际上获得了正确的非嵌套url,并且发出了一个失败的PUT请求
为什么这不适用于更新?如何更好地处理不同的父资源
任何提示都会很棒。:) 如果你试图破解ActiveResource,你不会玩得很开心 我不会使用
前缀
,而是使用find(:all,:from=>“”)在父资源中定义获取子资源的方法
在回答之前,我很好奇这样的东西是否适合你?可能会。这对我来说已经不重要了,加上这现在被称为Basecamp Classic。
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")
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
end
class Project < Resource
def messages
@messages ||= Message.find(:all, :from => "/projects/#{self.id}/posts.xml")
end
end
class Message < Resource
self.element_name = "post"
def comments
@comments ||= Comment.find(:all, :from => "/posts/#{self.id}/comments.xml")
end
end
class Comment < Resource
end
project = Project.find(1) # GET /projects/1.xml
messages = project.messages # GET /projects/1/posts.xml
message = message.first
comments = message.comments # GET /posts/1/comments.xml
comment.update_attribute(:title,'name') # PUT /comments/1.xml