0

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

1 回答 1

3

all()是一个实例方法,而不是一个类方法,但你调用它就像一个类方法。试试这样:

module ApiWrapper
  class Contact
    def self.all
      # issue httparty get request here that is created from the Connection class
    end
  end
end
于 2010-02-12T21:36:54.127 回答