|
2 | 2 |
|
3 | 3 | # config/initializers/geocoder.rb |
4 | 4 | Geocoder.configure( |
| 5 | + # Use test lookup in development/test to avoid external API calls |
| 6 | + lookup: Rails.env.production? ? :nominatim : :test, |
5 | 7 | always_raise: :all, |
6 | 8 | # geocoding service request timeout, in seconds (default 3): |
7 | 9 | timeout: 5, |
|
12 | 14 | # caching (see Caching section below for details): |
13 | 15 | cache: Geocoder::CacheStore::Generic.new(Rails.cache, {}) |
14 | 16 | ) |
| 17 | + |
| 18 | +# Configure test geocoding results for development/test environments |
| 19 | +unless Rails.env.production? |
| 20 | + Geocoder::Lookup::Test.add_stub( |
| 21 | + 'New York, NY', [ |
| 22 | + { |
| 23 | + 'latitude' => 40.7143528, |
| 24 | + 'longitude' => -74.0059731, |
| 25 | + 'address' => 'New York, NY, USA', |
| 26 | + 'state' => 'New York', |
| 27 | + 'state_code' => 'NY', |
| 28 | + 'country' => 'United States', |
| 29 | + 'country_code' => 'US' |
| 30 | + } |
| 31 | + ] |
| 32 | + ) |
| 33 | + |
| 34 | + Geocoder::Lookup::Test.add_stub( |
| 35 | + 'San Francisco, CA', [ |
| 36 | + { |
| 37 | + 'latitude' => 37.7749295, |
| 38 | + 'longitude' => -122.4194155, |
| 39 | + 'address' => 'San Francisco, CA, USA', |
| 40 | + 'state' => 'California', |
| 41 | + 'state_code' => 'CA', |
| 42 | + 'country' => 'United States', |
| 43 | + 'country_code' => 'US' |
| 44 | + } |
| 45 | + ] |
| 46 | + ) |
| 47 | + |
| 48 | + # Default stub for any address not specifically configured |
| 49 | + Geocoder::Lookup::Test.set_default_stub( |
| 50 | + [ |
| 51 | + { |
| 52 | + 'latitude' => 0.0, |
| 53 | + 'longitude' => 0.0, |
| 54 | + 'address' => 'Test Address', |
| 55 | + 'state' => 'Test State', |
| 56 | + 'country' => 'Test Country', |
| 57 | + 'country_code' => 'TC' |
| 58 | + } |
| 59 | + ] |
| 60 | + ) |
| 61 | +end |
0 commit comments