Last active
August 29, 2015 14:20
-
-
Save vano468/41c6aee8e32c8fc4f816 to your computer and use it in GitHub Desktop.
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
# Preparation | |
# gem install sinatra | |
# gem install timezone | |
# | |
# Launch | |
# RACK_ENV=production ruby time_utc.rb | |
# | |
# Check working | |
# curl http://localhost:4567/time?Moscow,New%20York | |
# | |
# Stress testing | |
# siege -c100 http://localhost:4567/time?Moscow,New%20York -b -t10s | |
require 'sinatra' | |
require 'timezone' | |
require 'find' | |
module Timezone | |
module Loader | |
def self.get_zone_data(zone) | |
path = '' | |
Find.find ZONE_FILE_PATH do |_path| | |
if _path =~ /.*#{zone}$/ | |
path = _path | |
break | |
end | |
end | |
unless File.exists? path | |
raise Timezone::Error::InvalidZone, "'#{zone}' is not a valid zone." | |
end | |
File.read path | |
end | |
end | |
end | |
class TimeUTC | |
class << self | |
def default | |
Time.now.utc | |
end | |
def by_city(city) | |
timezone = Timezone::Zone.new zone: prepare_city(city) | |
timezone.time Time.now | |
rescue Timezone::Error::InvalidZone => e | |
puts e.message | |
end | |
private | |
def prepare_city(city) | |
city.sub ' ', '_' | |
end | |
end | |
end | |
class TimePresenter | |
def initialize(cities) | |
@cities = cities || '' | |
end | |
def to_s | |
text = "UTC: #{TimeUTC.default}\n" | |
@cities.split(',').each do |city| | |
time = TimeUTC.by_city city | |
text += "#{city}: #{time}\n" if time | |
end | |
text | |
end | |
end | |
get '/time' do | |
"#{TimePresenter.new params.keys[0]}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment