http://host/timezone/name?lat=55.2341&lng=43.23352
CREATE DATABASE tz_service
CREATE EXTENSION postgis; CREATE EXTENSION postgis_topology; CREATE EXTENSION fuzzystrmatch; CREATE EXTENSION postgis_tiger_geocoder;
$ /usr/lib/postgresql/9.1/bin/shp2pgsql -D tz_world.shp > dump.sql $ psql -d tz_service -f dump.sql
SELECT tzid FROM tz_world WHERE ST_Contains(the_geom, ST_MakePoint(-122.420706, 37.776685));
tzid --------------------- America/Los_Angeles (1 ROW)
source "https://rubygems.org" gem 'rake' gem 'activerecord' gem 'pg' gem 'grape' group :development, :test do gem 'shotgun' gem 'byebug' gem 'pry' gem 'pry-byebug' gem 'rspec' end
$ bundle install
development: &config adapter: postgresql host: localhost username: user password: password database: tz_service encoding: utf8 test: <<: *config poduction: <<: *config
class Configuration DB_CONFIG = YAML.load_file(File.expand_path('../database.yml', __FILE__))[ENV['RACK_ENV']] class << self def adapter DB_CONFIG['adapter'] end def host DB_CONFIG['host'] end def username DB_CONFIG['username'] end def password DB_CONFIG['password'] end def database DB_CONFIG['database'] end def encoding DB_CONFIG['encoding'] end end end
require 'bundler' Bundler.require(:default) $: << File.expand_path('../', __FILE__) $: << File.expand_path('../../', __FILE__) $: << File.expand_path('../../config', __FILE__) $: << File.expand_path('../services', __FILE__) ENV['RACK_ENV'] ||= 'development' require 'grape' require 'json' require 'pry' require 'active_record' require 'timezone_name_service' require 'configuration' require 'application' require 'time_zone_api'
ActiveRecord::Base.establish_connection( adapter: Configuration.adapter, host: Configuration.host, database: Configuration.database, username: Configuration.username, password: Configuration.password, encoding: Configuration.encoding )
ENV['RACK_ENV'] ||= 'test' require_relative '../app/environment' require 'rack/test' RSpec.configure do |config| config.treat_symbols_as_metadata_keys_with_true_values = true config.run_all_when_everything_filtered = true config.filter_run :focus config.order = 'random' config.include Rack::Test::Methods def app TimeZoneAPI end end
describe 'API' do let(:params) { { lat: 55.7914056, lng: 49.1120427 } } # let(:error) { { error: 'lat is missing, lng is missing' } } # let(:name_response) { { timezone: 'Europe/Moscow' } } # # it 'should greet us' do get '/' expect(last_response).to be_ok expect(last_response.body).to eq(%Q{"Welcome to Time Zone API Service"}) end # describe 'Timezone name' do subject { last_response } # context 'with wrong params' do before do get '/timezone/name' end its(:status) {should eq 400} its(:body) {should eq error.to_json} end context 'with right params' do before do get '/timezone/name', params end its(:status) {should eq 200} its(:body) {should eq name_response.to_json} end end end
$ bundle exec rspec
class TimezoneNameService def initialize(lat, lng) @lat = lat @lng = lng end def name #"" . , sql = "SELECT tzid FROM tz_world WHERE ST_Contains(geom, ST_MakePoint(#{ActiveRecord::Base.sanitize(@lng)}, #{ActiveRecord::Base.sanitize(@lat)}));" name_hash = ActiveRecord::Base.connection.execute(sql).first name_hash['tzid'] rescue nil end end
class TimeZoneAPI < Grape::API format :json default_format :json default_error_formatter :txt desc 'Start page' get '/' do 'Welcome to Time Zone API Service' end namespace 'timezone' do desc 'Time zone name by coordinates' params do requires :lat, type: Float, desc: 'Latitude' requires :lng, type: Float, desc: 'Longitude' end # get '/name' do name = TimezoneNameService.new(params[:lat], params[:lng]).name { timezone: name } end end end
$ bundle exec rackup
Source: https://habr.com/ru/post/246077/
All Articles