Class: Sekitori::Generators::ListGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/sekitori/generators/list_generator.rb

Constant Summary collapse

CACHE_DIR =
File.join(Dir.tmpdir, 'sekitori_checker')
TARGET_XPATH_LIST =
[].freeze
IGNORE_XPATH_LIST =
[].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clean(path = CACHE_DIR) ⇒ Object



64
65
66
# File 'lib/sekitori/generators/list_generator.rb', line 64

def self.clean(path = CACHE_DIR)
  FileUtils.rm_rf(path, secure: true)
end

.get(url) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sekitori/generators/list_generator.rb', line 37

def self.get(url)
  file_name = url.split('/').last
  path = File.join(CACHE_DIR, file_name)
  cache = get_from_file(path)
  return cache if cache

  puts "Creating cache for #{file_name}"
  html = get_from_http(url)
  FileUtils.mkdir_p CACHE_DIR
  open(path, 'w') { |file| file.write html }
  html
end

.get_from_file(path) ⇒ Object



50
51
52
# File 'lib/sekitori/generators/list_generator.rb', line 50

def self.get_from_file(path)
  File.read path if File.exist? path
end

.get_from_http(url) ⇒ Object



54
55
56
57
# File 'lib/sekitori/generators/list_generator.rb', line 54

def self.get_from_http(url)
  encode_url = URI.escape(url)
  html = open(encode_url).read
end

.parse(html, url, xpath_list) ⇒ Object



59
60
61
62
# File 'lib/sekitori/generators/list_generator.rb', line 59

def self.parse(html, url, xpath_list)
  document = Nokogiri::HTML(html, url)
  xpath_list.flat_map { |xpath| document.xpath(xpath).map(&:text) }
end

Instance Method Details

#executeObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sekitori/generators/list_generator.rb', line 16

def execute
  return @list if @list.present?
  url = self.class::URL
  target_xpaths = self.class::TARGET_XPATH_LIST
  ignore_xpath = self.class::IGNORE_XPATH_LIST

  html = ListGenerator.get url
  list = ListGenerator.parse html, url, target_xpaths
  ignore_list = ListGenerator.parse html, url, ignore_xpath
  @list = list - ignore_list
  @list
end

#listObject



29
30
31
# File 'lib/sekitori/generators/list_generator.rb', line 29

def list
  execute
end

#to_hObject



33
34
35
# File 'lib/sekitori/generators/list_generator.rb', line 33

def to_h
  { self.class::BANZUKE => execute }
end