Hi - I'm very new here (1st post). I read the introductory docs, and looked at the categories, but I didn't see a specific place for posting code for review/critique. My apologies if this is not the appropriate place to do that...
Here is some code I am hoping to get reviewed/critiqued by people who know Ruby. I don't know squat about Ruby, but I am pretty familiar with modern OO lang's (C++, Java, C#, PHP, etc.), and I wrote this after reading a few tutorials.
It is an implementation of an Artificial System Clock (ASC). The idea is that you will use the getSystemDate method of this class everywhere in your app. that gets the date/time directly. Then you can change the date-time context for the whole app. for testing purposes. When in "channel" mode, it will go fetch the date-time value from TimeChannels.com, so that you can integration-test with multiple interfacing apps. all on the same shared ASC value.
Here's the TimeChannels class I wrote in Ruby:
require 'date'
require 'net/http'
require 'open-uri'
class TimeChannels
@@tcURL = "http://www.timechannels.com/GetChannel"
@@tcURLms = "http://www.timechannels.com/GetChannelMS"
@@tcAscState = "TimeChannels.state.Production"
@@tcChannel = "0"
@@tcAscDateTime = nil
@@tcServiceURL = "http://www.timechannels.com/GetChannel"
@@tcMillisecondMode = "false"
def TimeChannels.setToProductionState
@@tcAscState = "TimeChannels.state.Production"
end
def TimeChannels.setToStaticTestState(aDateTimeValue)
@@tcAscState = "TimeChannels.state.TestDate"
@@tcAscDateTime = aDateTimeValue
end
def TimeChannels.setToTestState(aChannel)
@@tcAscState = "TimeChannels.state.TestChannel"
@@tcChannel = aChannel
end
def TimeChannels.setToMillisecondMode()
@@tcMillisecondMode = "true"
@@tcServiceURL = @@tcURLms
end
def TimeChannels.setToStandardMode
@@tcMillisecondMode = "false"
@@tcServiceURL = @@tcURL
end
def TimeChannels.getSystemDate
if @@tcAscState == "TimeChannels.state.Production"
return DateTime.now
end
if @@tcAscState == "TimeChannels.state.TestDate"
return @@tcAscDateTime
end
if @@tcAscState == "TimeChannels.state.TestChannel"
return getChannelFromTimeChannelsService(@@tcChannel)
end
DateTime.now
end
def TimeChannels.getHTTPContent(url)
#res = Net::HTTP.get_response(URI.parse(url))
#data = res.body
data = open(url).read
data
end
def TimeChannels.getChannelFromTimeChannelsService(aChannel)
url = @@tcServiceURL + "?channel=" + aChannel
result = getHTTPContent(url)
if (result.nil?)
return Time.now
end
if (@@tcMillisecondMode == "true")
retVal = result
#retVal = DateTime.parse(result)
else
retVal = DateTime.parse(result)
end
retVal
end
end
Many thanks in advance to all who reply...
John