Aaron Moodie
Aaron Moodie

Reputation: 1965

How do I set a global variable in Sinatra depending on the production or development environment?

I'm using Sinatra's set method in order to assign a global variable:

set :location, 'Melbourne'

I want to update this so that the variable is static or dynamic depending on whether the app is in development or production. I tried this below, which works in development, but not in production:

set :location, production? ? request.location.city : 'Melbourne'

The request.location.city is from the geolocation gem, and this method work fine in production in other situations. Is there something in the if statement that I'm missing, or does the Sinatra set method not accept statements?

Upvotes: 2

Views: 1203

Answers (1)

Michał Kwiatkowski
Michał Kwiatkowski

Reputation: 9764

Request is not available at the top level, only inside request handlers.

Write a method instead of a global setting, e.g.:

def location(request)
  production? ? request.location.city : 'Melbourne'
end

Upvotes: 2

Related Questions