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



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

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['task_id'])
  end
end

Instance Method Details

#configObject



130
131
132
# File 'lib/clicoder/site_base.rb', line 130

def config
  @config ||= Config.new
end

#copy_makefileObject



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

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



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

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



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

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



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

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



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

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



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

def fetch_description
  xml_document.at_xpath(description_xpath)
end

#fetch_inputsObject



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

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

#fetch_outputsObject



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

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

#prepare_directoriesObject



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

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



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

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

#store_local_configObject



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

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



124
125
126
127
128
# File 'lib/clicoder/site_base.rb', line 124

def xml_document
   do |mechanize, contest_page|
    @xml_document ||= Nokogiri::HTML(mechanize.get(problem_url).content)
  end
end