This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
/api/stateless/location returns the location from Geocoder, and extends it with a currency. When there’s a timeout (or when the location can’t be determined), we return a 504 Gateway Timeout error.
- Loading branch information
Showing
7 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
module Stateless | ||
class LocationController < StatelessController | ||
def index | ||
render json: { location: location_with_currency } | ||
rescue Api::Exceptions::LocationNotFound | ||
head status: 504 | ||
end | ||
|
||
private | ||
|
||
def location | ||
@location ||= request.location | ||
raise Api::Exceptions::LocationNotFound unless @location | ||
@location.data | ||
end | ||
|
||
def location_with_currency | ||
@location_with_currency ||= location.merge( | ||
currency: Donations::Utils.currency_from_country_code(location['country_code']) | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
require 'rails_helper' | ||
|
||
describe 'API::Stateless Location' do | ||
describe 'GET /api/stateless/location' do | ||
context 'Geocoder Success' do | ||
it 'responds with (at least) a country code and a currency' do | ||
get '/api/stateless/location' | ||
expect(response.status).to eq(200) | ||
expect(response.body).to include_json( | ||
location: { | ||
country_code: 'RD', | ||
currency: 'USD' | ||
} | ||
) | ||
end | ||
end | ||
|
||
context 'Geocoder Timeout' do | ||
before do | ||
allow(Geocoder).to receive(:search) { [] } | ||
end | ||
|
||
it 'responds with a 504 Gateway Timeout' do | ||
get '/api/stateless/location' | ||
expect(response.status).to eq(504) | ||
end | ||
|
||
after do | ||
allow(Geocoder).to receive(:search).and_call_original | ||
end | ||
end | ||
end | ||
end |