1

I'm creating tests for my senders_controller and am trying to retrieve my base url to be able to call an HTTParty request. I basically need the same method to be able to retrieve me one of these two links depending on the environment:

  • in development: http://localhost:3100/senders
  • in production: https://example.com/senders

My tests work in my spec/senders_controller.rb like this:

HTTParty.post("http://localhost:3100/senders", query: {email: user.email})

But I want that link to also work in production, which is why I'm looking for a dynamic way to get the base url in both development and production.

Any ideas?

  • 1
    Are you trying to use development (http://localhost:3100) and production(https://example.com) as a base URL for the spec or for the code in the controller? You can use `request.base_url` for the latter. For the former, perhaps this may help you? https://stackoverflow.com/a/29037481/7485031 – Ali Ilman Aug 29 '19 at 00:43

2 Answers2

3

An option that I have used in the past is to use the config gem.

You can create variables for different environments and then just call that variable.

For example, you can have a config/settings/development.yml that defines the url one way:

base_url: "http://localhost:3100/senders"

And a config/settings/production.yml file that defines it another:

base_url: "https://example.com/senders"

Then call it with Settings.base_url

It automatically resolves the environment and populates the correct settings for that environment.

Joe Edgar
  • 872
  • 5
  • 13
2

Try this, nice and simple:

url = Rails.env.development? ? "localhost:3100/senders" : "example.com/senders"

HTTParty.post(url, query: {email: user.email})
Gotenks-J
  • 79
  • 1
  • 5
  • 1
    I had to pass in `Rails.env.test` rather than `Rails.env.development` since I was using the links for testing. It should have worked except for other configuration I had.. Great idea, though. Thanks for the help! – Juliette Chevalier Aug 29 '19 at 20:58
  • Would you mind accepting this answer if it was helpful and got you to your end goal? Thanks! – Gotenks-J Nov 13 '19 at 00:58