如何在子类中使用 HTTParty 方法?
我正在尝试用 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
I'm trying to write an API wrapper in Ruby and am stumped on how I can call HTTParty methods from a subclass.
I want the user to create a connection to the API and then be able to query results from subclasses.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
all()
是一个实例方法,而不是类方法,但您像调用类方法一样调用它。试试这样:all()
is an instance method, not a class method, but you are calling it like a class method. Try it like this: