Class: SeoKeywordsGenerator::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/seo-keywords-generator-ruby.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



8
9
10
# File 'lib/seo-keywords-generator-ruby.rb', line 8

def initialize(argv)
    @argv = argv
end

Instance Method Details

#runObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/seo-keywords-generator-ruby.rb', line 12

def run
    options = {
        engines: ['ac', 'rs', 'rq'],
        depth_limit: 0,
        save_to: 'CSV',
        api_key: '5868ece26d41221f5e19ae8b3e355d22db23df1712da675d144760fc30d57988',
        domain: 'google.com',
        country: 'us',
        lang: 'en',
    }

    OptionParser.new do |opts|
        opts.banner = 'Usage: seo [options]'

        opts.on('-q', '--query QUERY', String, 'Search query (required)') do |q|
            options[:query] = q
        end

        opts.on('-e', '--engines ENGINES', Array, 'Choices of engines to extract: Autocomplete (ac), Related Searches (rs), People Also Ask (rq). You can select multiple engines by separating them with a comma: ac,rs. All engines are selected by default.') do |e|
            options[:engines] = e
        end

        opts.on('-d', '--depth-limit LIMIT', Integer, 'Depth limit for People Also Ask. Default is 0, first 2-4 results.') do |dl|
            options[:depth_limit] = dl
        end

        opts.on('-s', '--save-to SAVE', String, 'Saves the results in the current directory in the selected format (CSV, JSON, TXT). Default CSV.') do |st|
            options[:save_to] = st
        end

        opts.on('-k', '--api-key KEY', String, 'Your SerpApi API key: https://serpapi.com/manage-api-key. Default is a test API key to test CLI.') do |ak|
            options[:api_key] = ak
        end

        opts.on('-g', '--domain DOMAIN', String, 'Google domain: https://serpapi.com/google-domains. Default google.com.') do |gd|
            options[:domain] = gd
        end

        opts.on('-c', '--country COUNTRY', String, 'Country of the search: https://serpapi.com/google-countries. Default us.') do |gl|
            options[:country] = gl
        end

        opts.on('-l', '--language LANGUAGE', String, 'Language of the search: https://serpapi.com/google-languages. Default en.') do |hl|
            options[:lang] = hl
        end
    end.parse!(@argv)

    keyword_research = SeoKeywordsGenerator.new(
        query=options[:query],
        api_key=options[:api_key],
        lang=options[:lang],
        country=options[:country],
        domain=options[:domain]
    )

    data = {}

    options[:engines]&.each do |engine|
        case engine.downcase
        when 'ac'
            data['auto_complete'] = keyword_research.get_auto_complete()
        when 'rs'
            data['related_searches'] = keyword_research.get_related_searches()
        when 'rq'
            data['related_questions'] = keyword_research.get_related_questions(options[:depth_limit])
        end
    end

    if !data.empty?
        keyword_research.print_data(data)
        puts "Saving data in #{options[:save_to].upcase} format..."

        case options[:save_to].upcase
        when 'CSV'
            keyword_research.save_to_csv(data)
        when 'JSON'
            keyword_research.save_to_json(data)
        when 'TXT'
            keyword_research.save_to_txt(data)
        end

        puts "Data successfully saved to #{options[:query].gsub(' ', '_')}.#{options[:save_to].downcase} file"
    end
end