tags:

views:

106

answers:

1

I'm trying to write an API wrapper in ruby and am stumped on how I can call HTTParty methods from a subclass.

For instance, 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
A: 

all() is an instance method, not a class method, but you are calling it like a class method. Try it like this.

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