5

Basecamp Classic API( http://developer.37signals.com/basecamp/comments.shtml )を使用しようとしています。現在のbasecamp-wrapperバージョンは私にフィット感を与えていました。1つは、jsonの応答にページネーションの出力が含まれているのに対し、xmlの応答には含まれていないためです。これは簡単な修正でしたが、問題はURL構造が標準化されていないことです。

APIはそのようないくつかのことを指定しているので、要素パスとコレクションパスを単純に分離していると私は信じています。

Get recent comments (for a commentable resource)
GET /#{resource}/#{resource_id}/comments.xml

Update comment
PUT /comments/#{id}.xml

私はこれを何度か試みましたが、実際には成功していません。このようなコメントを処理しようとすると、せいぜいハッキーであり、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")

これは、実際にネストされていない適切なURLを取得し、失敗するPUT要求を行うupdate_attributeまで機能​​します。

なぜこれがアップデートで機能しないのですか?さまざまな親リソースをより適切に処理するにはどうすればよいですか?

どんなヒントも素晴らしいでしょう。:)

4

1 に答える 1

0

ActiveResourceをハックしようとすると、楽しい時間を過ごすことができなくなります。

を使用せずprefix、代わりに、を使用して子リソースを取得するためのメソッドを親リソースに定義しますfind(:all, :from => '')http://api.rubyonrails.org/classes/ActiveResource/Base.html#method-c-find

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
于 2013-02-24T04:11:52.673 に答える