0

我一直想知道如何将类和实例方法都放入一个模块中,然后将该模块包含到一个模型中。

我已经将它与其他示例一起使用,但我很难理解正确放置包含 HTTPparty 的位置。

以下是我所在位置的详细信息:

module Vimeo

  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods

   class Base      
     include HTTParty
     base_uri 'vimeo.com/api/v2'
     headers 'Content-Type' => 'application/json' 
   end

   class VimeoUser < Base

    def vimeo_account(account_name)
      @id = account_name
    end

   end

 end

   def info
     Vimeo::Base.get("http://vimeo.com/api/v2/#{@id}/info.json")
   end

end

目标是使用以下方法将其附加到用户模型:包括 Vimeo 并能够调用:

User.vimeo_account("name")

user = User.new
user.info

任何建议将不胜感激!

4

1 回答 1

1

我认为通过以下方式调用 get 应该可以解决问题。

def info
  Vimeo::Base.class.get("http://vimeo.com/api/v2/#{@id}/info.json")
end

但对我来说,您似乎可以进行如下更简单的设置:(除非还有更多我不明白的)

   class VimeoUser
     include HTTParty
     base_uri 'vimeo.com/api/v2'
     headers 'Content-Type' => 'application/json' 

   // All vimeo user related methods here, such that self.class.get etc is possible.
   end
于 2011-09-14T11:19:50.300 回答