Last active
August 29, 2015 13:58
-
-
Save woshidan/10005439 to your computer and use it in GitHub Desktop.
make-test-data
This file contains 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
def convert_to_records(raw_records, data_column) | |
if raw_records.first.length != data_column.length | |
puts '取得したデータの列の数が必要なデータの列の数と違います' | |
return false | |
end | |
records = [] | |
raw_records.each do |raw_record| | |
record = {} | |
data_column.each_with_index do |data_column, i| | |
record[data_column] = raw_record[i] | |
end | |
records << record | |
end | |
records | |
end |
This file contains 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
def get_file_contents(html_path) | |
file = open(html_path) | |
contents = file.read | |
file.close | |
contents | |
end |
This file contains 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
def get_raw_records(contents) | |
names = contents.scan(/h[\d].+nme[\d][^\/]+\//) | |
introductions = contents.scan(/pft[^v]+v/) | |
records = [] | |
names.each_with_index do |name, i| | |
name = name.split(/(\w|\/)+/)[2] | |
words = introductions[i].split(/(\w|\/|\n|;|&)+/) | |
introduction = '' | |
words.each do |word| | |
if /^([ぁ-ん]|[ァ-ヴ]|\d)/ =~ word | |
introduction += word + ' ' | |
end | |
end | |
record = [name, introduction] | |
records << record | |
end | |
records | |
end |
This file contains 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
namespace :db do | |
task guide: :environment do | |
Page.create!(name: "フシギダネ", introduction: "うまれたときから せなかに しょくぶつの タネが あって すこしずつ おおきく そだつ。 ") | |
Page.create!(name: "フシギソウ", introduction: "つぼみが せなかに ついていて ようぶんを きゅうしゅうしていくと おおきな はなが さくという。 ") | |
Page.create!(name: "フシギバナ", introduction: "はなから うっとりする かおりが ただよい たたかうものの きもちを なだめてしまう。 ") | |
. | |
. | |
. | |
end | |
end |
This file contains 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
def make_rake_task(records, object_name, data_column, task_name) | |
start_pattern = "namespace :db do\n task #{task_name}: :environment do\n" | |
end_pattern = " end\nend" | |
create_pattern = "" | |
records.each do |record| | |
hash = "" | |
command = " #{object_name}.create!(" | |
data_column.each do |column| | |
value = record[column] | |
hash += ", #{column}: \"#{value}\"" | |
end | |
hash.slice!(0,2) | |
command += "#{hash})" | |
create_pattern += "#{command}\n" | |
end | |
create_pattern | |
rake_task = start_pattern + create_pattern + end_pattern | |
end |
This file contains 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
def make_test_data(html_path, object_name, data_column, task_name) | |
html_file = get_file_contents(html_path) | |
contents = trim_file(html_file) | |
raw_records = get_raw_records(contents) | |
records = convert_to_records(raw_records, data_column) | |
rake_task = make_rake_task(records, object_name, data_column, task_name) | |
rake_file = File.open("#{task_name}.rake","w") | |
rake_file.write(rake_task) | |
rake_file.close | |
end |
This file contains 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
def trim_file(html_file) | |
contents_start_pattern = "<script src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/show_ads.js\"><\/script>" | |
contents_end_pattern = "<div\sclass=\"ad\">" | |
contents = html_file.scan(/#{contents_start_pattern}.+#{contents_end_pattern}/m)[0] | |
contents.delete!(contents_start_pattern, contents_end_pattern) | |
contents | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment