Class: Kimurai::CLI::Generator

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/kimurai/cli/generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



6
7
8
# File 'lib/kimurai/cli/generator.rb', line 6

def self.source_root
  File.dirname(File.expand_path('..', __FILE__))
end

Instance Method Details

#generate_project(project_name) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/kimurai/cli/generator.rb', line 10

def generate_project(project_name)
  directory "template", project_name
  inside(project_name) do
    run "bundle install"
    run "git init"
  end
end

#generate_scheduleObject



43
44
45
# File 'lib/kimurai/cli/generator.rb', line 43

def generate_schedule
  copy_file "template/config/schedule.rb", "./schedule.rb"
end

#generate_spider(spider_name, in_project:) ⇒ Object



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.exists? 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

  unless 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
end