18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/kimurai/cli/generator.rb', line 18
def generate_spider(spider_name, in_project:)
spider_path = in_project ? "spiders/#{spider_name}.rb" : "./#{spider_name}.rb"
raise "Spider #{spider_path} already exists" if File.exist? spider_path
spider_class = to_spider_class(spider_name)
create_file spider_path do
<<~RUBY
class #{spider_class} < #{in_project ? 'ApplicationSpider' : 'Kimurai::Base'}
@name = "#{spider_name}"
@start_urls = []
@config = {}
def parse(response, url:, data: {})
end
end
RUBY
end
return if in_project
insert_into_file spider_path, " @engine = :mechanize\n", after: "@name = \"#{spider_name}\"\n"
prepend_to_file spider_path, "require 'kimurai'\n\n"
append_to_file spider_path, "\n#{spider_class}.crawl!"
end
|