我一直想知道如何将类和实例方法都放入一个模块中,然后将该模块包含到一个模型中。
我已经将它与其他示例一起使用,但我很难理解正确放置包含 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
任何建议将不胜感激!