Skip to content

Instantly share code, notes, and snippets.

@vano468
Last active August 29, 2015 14:20
Show Gist options
  • Save vano468/41c6aee8e32c8fc4f816 to your computer and use it in GitHub Desktop.
Save vano468/41c6aee8e32c8fc4f816 to your computer and use it in GitHub Desktop.
# 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