Class: Clicoder::SiteBase

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/clicoder/site_base.rb

Direct Known Subclasses

AOJ, AtCoder, SampleSite

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

#detect_main, #ext_to_language_name

Class Method Details

.new_with_config(config) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/clicoder/site_base.rb', line 28

def self.new_with_config(config)
  case config['site']
  when 'sample_site'
    SampleSite.new
  when 'aoj'
    AOJ.new(config['problem_number'])
  when 'atcoder'
    AtCoder.new(config['contest_id'], config['problem_number'])
  end
end

Instance Method Details

#configObject



127
128
129
# File 'lib/clicoder/site_base.rb', line 127

def config
  @config ||= Config.new
end

#copy_makefileObject



95
96
97
98
99
100
# File 'lib/clicoder/site_base.rb', line 95

def copy_makefile
  makefile = config.asset('makefile')
  return unless File.file?(makefile)
  ext = File.extname(makefile)
  FileUtils.cp(makefile, "#{working_directory}/Makefile")
end

#copy_templateObject



88
89
90
91
92
93
# File 'lib/clicoder/site_base.rb', line 88

def copy_template
  template_file = config.asset('template')
  return unless File.file?(template_file)
  ext = File.extname(template_file)
  FileUtils.cp(template_file, "#{working_directory}/main#{ext}")
end

#download_descriptionObject



60
61
62
63
64
65
66
# File 'lib/clicoder/site_base.rb', line 60

def download_description
  Dir.chdir(working_directory) do
    File.open('description.md', 'w') do |f|
      f.write(ReverseMarkdown.parse(fetch_description))
    end
  end
end

#download_inputsObject



68
69
70
71
72
73
74
75
76
# File 'lib/clicoder/site_base.rb', line 68

def download_inputs
  Dir.chdir("#{working_directory}/#{INPUTS_DIRNAME}") do
    fetch_inputs.each_with_index do |input, i|
      File.open("#{i}.txt", 'w') do |f|
        f.write(input.strip + "\n")
      end
    end
  end
end

#download_outputsObject



78
79
80
81
82
83
84
85
86
# File 'lib/clicoder/site_base.rb', line 78

def download_outputs
  Dir.chdir("#{working_directory}/#{OUTPUTS_DIRNAME}") do
    fetch_outputs.each_with_index do |output, i|
      File.open("#{i}.txt", 'w') do |f|
        f.write(output.strip + "\n")
      end
    end
  end
end

#fetch_descriptionObject



102
103
104
# File 'lib/clicoder/site_base.rb', line 102

def fetch_description
  xml_document.at_xpath(description_xpath)
end

#fetch_inputsObject



106
107
108
109
# File 'lib/clicoder/site_base.rb', line 106

def fetch_inputs
  input_nodes = xml_document.xpath(inputs_xpath)
  input_nodes.map(&:text)
end

#fetch_outputsObject



111
112
113
114
# File 'lib/clicoder/site_base.rb', line 111

def fetch_outputs
  outputs_nodes = xml_document.xpath(outputs_xpath)
  outputs_nodes.map(&:text)
end

#prepare_directoriesObject



51
52
53
54
55
56
57
58
# File 'lib/clicoder/site_base.rb', line 51

def prepare_directories
  FileUtils.mkdir_p(working_directory)
  Dir.chdir(working_directory) do
    FileUtils.mkdir_p(INPUTS_DIRNAME)
    FileUtils.mkdir_p(OUTPUTS_DIRNAME)
    FileUtils.mkdir_p(MY_OUTPUTS_DIRNAME)
  end
end

#startObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/clicoder/site_base.rb', line 39

def start
  prepare_directories
   do
    download_description
    download_inputs
    download_outputs
  end
  copy_template
  copy_makefile
  store_local_config
end

#store_local_configObject



116
117
118
119
120
121
# File 'lib/clicoder/site_base.rb', line 116

def store_local_config
  config.local['site'] = site_name
  File.open("#{working_directory}/.config.yml", 'w') do |f|
    f.write(config.local.to_yaml)
  end
end

#xml_documentObject



123
124
125
# File 'lib/clicoder/site_base.rb', line 123

def xml_document
  @xml_document ||= Nokogiri::HTML(open(problem_url))
end