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 に答える