Last active
December 21, 2015 02:09
-
-
Save lee-dohm/6233062 to your computer and use it in GitHub Desktop.
Stuff for David Vo
This file contains hidden or 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
require 'date' | |
# Set the expriation to now plus 30 days | |
expriation = Time.now + (30 * 24 * 60 * 60) | |
# Output the filename in the desired format | |
puts expiration.strftime('discourse-%Y-%m-%d.dump') | |
# Or if you want the time too | |
puts expiration.strftime('discourse-%Y-%m-%d-%H-%M-%S.dump') | |
# You can also get fancy by reopening the Fixnum class and adding some helper methods | |
# | |
# Inspired by the activesupport gem: http://rubydoc.info/gems/activesupport/Time | |
# You could use activesupport, but it is a big gem and if all you need is the time | |
# calculation stuff, that might be overkill. Then again, activesupport is used by | |
# many many more code bases than my thrown together (and untested) code here. | |
class Fixnum | |
def seconds | |
self | |
end | |
alias_method :second, :seconds | |
def minutes | |
seconds * 60 | |
end | |
alias_method :minute, :minutes | |
def hours | |
minutes * 60 | |
end | |
alias_method :hour, :hours | |
def days | |
hours * 24 | |
end | |
alias_method :day, :days | |
def weeks | |
days * 7 | |
end | |
alias_method :week, :weeks | |
def months | |
days * 30 | |
end | |
alias_method :month, :months | |
def years | |
days * 365 | |
end | |
alias_method :year, :years | |
end | |
expiration = Time.now + 30.days | |
expiration = Time.now + 1.week | |
two_years_ago = Time.now - 2.years | |
# Or even fancier | |
class Fixnum | |
def ago | |
Time.now - self | |
end | |
def from_now | |
Time.now + self | |
end | |
end | |
expiration = 30.days.from_now | |
expiration = 1.week.from_now | |
two_years_ago = 2.years.ago | |
# Yes, you can do time comparisons | |
# Both of these print 'true' | |
puts Time.now < 30.days.from_now | |
puts Time.now >= 2.years.ago |
This file contains hidden or 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
# I'm pretty sure this should work | |
# Read date_calculations.rb if you don't know how I did the 30.days.from_now thing | |
expiration = 30.days.from_now | |
# I wrapped all of this up in a block to the File.open call so that the file | |
# on disk gets properly closed after upload automatically | |
s3_file = nil | |
File.open("/home/discourse/#{filename}") do |input_file| | |
s3_file = directory.files.create( | |
key: filename, | |
body: input_file, | |
public: true, | |
metadata: { Expires: expiration.utc.to_s }) | |
end |
This file contains hidden or 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
# I've noticed that you have a tendency to write code like the following: | |
obj.some_function("#{value}") | |
# This is not idiomatic Ruby because either `value` already contains a string, | |
# in which case the code above is essentially equivalent to this: | |
obj.some_function("" + value + "") | |
# Or `value` does not contain a string, in which case the code on line 2 | |
# is essentially equivalent to this: | |
obj.some_function("" + value.to_s + "") | |
# In other words, the code on line 6 can be simplified to this: | |
obj.some_function(value) | |
# And the code on line 10 can be simplified to this: | |
obj.some_function(value.to_s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment