Skip to content

Instantly share code, notes, and snippets.

@elihart
Created February 25, 2015 22:43
Show Gist options
  • Save elihart/e85c782356373040c1c6 to your computer and use it in GitHub Desktop.
Save elihart/e85c782356373040c1c6 to your computer and use it in GitHub Desktop.
Compare upload phrases methods
require 'time'
require 'json'
require "rexml/document"
# Get the secret from monorail's encrypted databag
# (e.g. /srv/airbnb/on/config/configatron/secure.yml)
# under `api_private_translate`, `secret`.
SECRET = ARGV[0]
URL = 'https://www.airbnb.com'
new_phrases = []
# Make changes to the string to get it ready for the translation system
# String will be reformatted after it is brought down
def unescape_backslash_sequences(thing)
thing.gsub('\n', "\n").gsub("\\'", "'").gsub('\\"', '"')
end
def old_method
phrases = []
# Find the string keys with no descriptions
body = File.read './airbnb/src/main/res/values/strings.xml'
matches = body.scan(/\<string[\s]+name=[\s]*[\"|\']([^'"]*)[\"|\'][\s]*>([^<]*)<\/[\s]*string[\s]*>/i)
matches.each do |match|
phrases << {
:key => 'android.strings.' + match[0],
:value => unescape_backslash_sequences(match[1])
}
end
# Find the string keys with descriptions
matches = body.scan(/\<string[\s]+name=[\s]*[\"|\']([^'"]*)[\"|\'][\s]*description=[\s]*[\"|\']([^'"]*)[\"|\'][\s]*>([^<]*)<\/[\s]*string[\s]*>/i)
matches.each do |match|
phrases << {
:key => 'android.strings.' + match[0],
:description => match[1],
:value => unescape_backslash_sequences(match[2])
}
end
return phrases
end
def parse_phrases_from_strings_file
phrases = []
f = File.open('./airbnb/src/main/res/values/strings.xml')
xml = REXML::Document.new(f)
# Iterate through all "<string>" nodes
xml.elements.each("*/string") do |stringNode|
# Attributes have a key/value pattern. Get the value using either to_s or value depending on if you want to replace entities or not
# http://ruby-doc.org/stdlib-1.9.3/libdoc/rexml/rdoc/REXML/Attribute.html
name = stringNode.attribute("name")
if name.nil?
raise "Name must exist: #{stringNode}"
end
description = stringNode.attribute("description")
# Throw an error if there are any unexpected attributes
stringNode.attributes.each do |key, value|
if key != "name" && key != "description"
raise "Unsupported attribute #{key} in #{stringNode}"
end
end
phrase = {:key => "android.strings.#{name}"}
unless description.nil?
# Use .to_s to replace html entities. If we want to denormalize this use description.value instead
phrase[:description] = description.to_s
end
value = stringNode.get_text.to_s # Use the raw text node so it doesn't parse html entities
phrase[:value] = unescape_backslash_sequences(value)
phrases << phrase
end
f.close
return phrases
end
oldResults = old_method
newResults = parse_phrases_from_strings_file
oldResults.each do |old|
oldName = old[:key]
oldDescription = old[:description]
oldValue = old[:value]
match = false
newResults.delete_if do |newResult|
newName = newResult[:key]
newDescription = newResult[:description]
newValue = newResult[:value]
if newName == oldName
match = true
if newDescription != oldDescription
puts "Description mismatch #{old} : #{newResult}"
end
if newValue != oldValue
puts "Value mismatch #{old} : #{newResult}"
end
end
newName == oldName
end
next if match
puts "New method doesn't include: #{old}"
end
newResults.each do |newResult|
newName = newResult[:key]
newDescription = newResult[:description]
newValue = newResult[:value]
match = false
oldResults.each do |old|
oldName = old[:key]
oldDescription = old[:description]
oldValue = old[:value]
if newName == oldName
match = true
if newDescription != oldDescription
puts "Description mismatch #{old} : #{newResult}"
end
if newValue != oldValue
puts "Value mismatch #{old} : #{newResult}"
end
break
end
end
next if match
puts "Old method doesn't include: #{newResult}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment