代码之家  ›  专栏  ›  技术社区  ›  cpjolicoeur

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

  •  0
  • cpjolicoeur  · 技术社区  · 15 年前

    我正在尝试用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 回复  |  直到 13 年前
        1
  •  3
  •   the Tin Man    13 年前

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

    module ApiWrapper
      class Contact
        def self.all
          # issue httparty get request here that is created from the Connection class
        end
      end
    end