Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure character birth century #208

Closed
wants to merge 11 commits into from
18 changes: 18 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,21 @@ task "jobs:work" => "resque:work"
task :test do
Dir['./spec/**/*_spec.rb'].each { |f| load f }
end

desc "Adds missing centuries to birth dates (example: 0020 to 2020)"
task "cleanup_birth" do
characters = Character.where(({'birth' => {'$lt' => Date.parse('1999-01-01')}}) )

puts "Found #{characters.count}"

puts "Updating..." if characters.count > 0

characters.each do |character|
with_century = DateHelper::ensure_century(character['birth'])

character.update(birth: with_century)
end

puts "Done"

end
5 changes: 1 addition & 4 deletions helpers/account_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module AccountHelper
:name => "birth",
:label => "Birth",
:group => :general,
:value => Proc.new { |v| v[:birth] }
:value => Proc.new { |v| DateHelper::ensure_century(v[:birth]) }
},
{
:name => "aetheria",
Expand Down Expand Up @@ -775,7 +775,4 @@ def self.field_value(group, name)
field.length == 1 ? field.first[:value] : Proc.new { "" }
end

def self.parse_birth(birth)
DateTime.strptime("#{birth} EST", "%m/%d/%Y %H:%M:%S %p %Z")
end
Comment on lines -778 to -780
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is unused.

end
24 changes: 0 additions & 24 deletions helpers/character_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,6 @@ module CharacterHelper
"self" => "Self"
}

def self.parse_birth(birth)
parsed = nil

# Try our first format
begin
parsed = DateTime.strptime("#{birth} EST", "%m/%d/%Y %H:%M:%S %p %Z")
rescue ArgumentError
puts "ArgumentError caught trying to parse '#{birth} EST' as a DateTime with format %m/%d/%Y %H:%M:%S %p %Z"
puts "Error was `#{$!}`"
end

# Try our second one
if parsed.nil?
begin
parsed = DateTime.strptime("#{birth} EST", "%m/%d/%Y %H:%M:%S %Z")
rescue ArgumentError
puts "ArgumentError caught trying to parse '#{birth} EST' as a DateTime with format %m/%d/%Y %H:%M:%S %Z"
puts "Error was `#{$!}`"
end
end

parsed
end

def self.tag_html(character)
html_strings = []
html_strings << "<div class='tag'>" # Open up tag div
Expand Down
27 changes: 27 additions & 0 deletions helpers/date_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module DateHelper

FORMATS = [
"%m/%d/%Y %H:%M:%S %p %Z",
"%m/%d/%Y %H:%M:%S %Z"
]

def self.ensure_century(date)
adjustment = (date.year < 1999) ? 2000 : 0
date + adjustment.years
end

def self.parse(date_string)
results = FORMATS.lazy.map do |format|
begin
parsed = DateTime.strptime("#{date_string} EST", format)
ensure_century(parsed)
rescue ArgumentError
puts "ArgumentError caught trying to parse '#{date_string} EST' as a DateTime with format #{format}"
puts "Error was `#{$!}`"
end
end

results.detect { |date| !date.nil? }
end

end
2 changes: 1 addition & 1 deletion helpers/rankings_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ def self.generate_aggregation_args(name, params)
:match => { "a" => { "$exists" => true }, "b" => { "$ne" => nil } },
:project => { "_id" => 0, "n" => 1, "s" => 1, "b" => 1 },
:sort => { "b" => 1 },
:accessor => Proc.new { |v| v["b"] }
:accessor => Proc.new { |v| DateHelper::ensure_century(v["b"]) }
},
:deaths => {
:display => "Deaths",
Expand Down
5 changes: 4 additions & 1 deletion routes/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ def self.registered(app)
end

content_type 'application/json'
JSON.pretty_generate(@character.serializable_hash({}).tap {|h| h.delete("id")})
JSON.pretty_generate(@character.serializable_hash({}).tap do |h|
h.delete("id")
h['birth'] = DateHelper::ensure_century(h['birth'])
end)
end

app.get '/:server/:name/?' do |s,n|
Expand Down
2 changes: 1 addition & 1 deletion routes/upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def self.registered(app)

# Convert "birth" field so it's stored as DateTime with GMT-5
if(json_text.has_key?("birth"))
json_text["birth"] = CharacterHelper::parse_birth(json_text["birth"])
json_text["birth"] = DateHelper::parse(json_text["birth"])
end

# Log extra debug info if birth ends up being nil
Expand Down
27 changes: 27 additions & 0 deletions spec/unit/date_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require_relative '../spec_helper'
require './helpers/date_helper'

describe DateHelper do
describe '.ensure_century' do
before do
date = Date.strptime(date_string, '%m/%d/%Y')
@result = DateHelper.ensure_century(date)
end

describe 'when date string contains century' do
let(:date_string) { '10/07/2021' }

it 'returns date with correct century' do
assert_equal(2021, @result.year)
end
end

describe 'when date string does NOT contain century' do
let(:date_string) { '10/07/21' }

it 'returns date with correct century' do
assert_equal(2021, @result.year)
end
end
end
end
2 changes: 1 addition & 1 deletion views/_other_pane.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
%td.specialized{:colspan => 2} General
%tr
%td Birth
%td= @character.birth
%td= DateHelper::ensure_century(@character.birth)
%tr
%td Deaths
%td= @character.deaths
Expand Down