我正在尝试在Ruby中编写一个API包装器,并且难以理解如何从子类调用HTTParty方法。
我希望用户创建与API的连接,然后能够查询子类的结果。
module ApiWrapper
class Connection
include HTTParty
base_uri '...'
def initialize( u, p )
...
end
def contacts
ApiWrapper::Contact
end
end
end
module ApiWrapper
class Contact
def all
# issue httparty get request here that is created from the Connection class
end
end
end
## The user would do this
conn = ApiWrapper::Connection.new( 'username', 'password' )
contacts = conn.contacts.all
答案 0 :(得分:3)
all()
是一个实例方法,而不是类方法,但您将其称为类方法。试试这样:
module ApiWrapper
class Contact
def self.all
# issue httparty get request here that is created from the Connection class
end
end
end