我正在尝试在 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