Search code examples
ruby-on-railshttparty

undefined method base_uri on httparty


I am using httparty to get create a gem to allow user to get an auth_token from a site.

I need to do POST method session:

POST https://www.mysite.io/api/v1/sessions

followed by email and password (both are strings) in the body

Here is what I have:

require 'httparty'

class Gemname
  include HTTParty
  base_uri "https://www.mysite.io/api/v1/sessions"

  def initialize(email, password)
    response = self.class.post(self.base_uri, body: {"email": email, "password": password}) 
    raise InvalidStudentCodeError.new() if response.code == 401
    @auth_token = response["auth_token"]
  end

end

Gemname works fine on irb. However, I am having trouble entering my email and password. Whenever I do Gemname.new("[email protected]", "mypassword"), I get NoMethodError: undefined methodbase_uri'` error.

How can I fix my code so that I HTTParty recognize my email and password to be passed down to self.class.post properly?


Solution

  • self.base_uri in your example refers to a member of the instance being constructed while HTTParty's base_uri is a class method.

    You can access it with:

    self.class.base_uri