Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
Add location endpoint and specs
Browse files Browse the repository at this point in the history
/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
Vincent Martinez authored and eyko committed Mar 7, 2017
1 parent be067ef commit 7af5e7a
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ group :test do
gem 'timecop'
gem 'coveralls', require: false
gem 'poltergeist'
gem 'rspec-json_expectations'
end

# Rails Assets - reference any Bower components that you need as gems.
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ GEM
rspec-expectations (3.4.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-json_expectations (2.1.0)
rspec-mocks (3.4.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
Expand Down Expand Up @@ -607,6 +608,7 @@ DEPENDENCIES
redis (>= 3.2.0)
remotipart (~> 1.2)
rmagick
rspec-json_expectations
rspec-rails
rubocop
sass-rails (~> 5.0.6)
Expand Down
27 changes: 27 additions & 0 deletions app/controllers/api/stateless/location_controller.rb
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
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@
post :facebook
get :test_authentication
end

get :location, to: 'location#index'
end

resources :members
Expand Down
1 change: 1 addition & 0 deletions lib/api/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Api
module Exceptions
class AuthenticationError < StandardError; end
class LocationNotFound < StandardError; end
class UnauthorizedError < AuthenticationError; end
class InvalidTokenError < AuthenticationError; end
class ExpiredTokenError < AuthenticationError; end
Expand Down
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
require 'rspec/json_expectations'
require 'database_cleaner'
require 'devise'
require 'support/helper_functions'
Expand Down
34 changes: 34 additions & 0 deletions spec/requests/api/stateless/location_spec.rb
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

0 comments on commit 7af5e7a

Please sign in to comment.