如何在子类中使用HTTParty方法?

时间:2010-02-12 19:58:29

标签: ruby 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

1 个答案:

答案 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