Class: SearchLogger::Exec
- Inherits:
-
Object
- Object
- SearchLogger::Exec
- Defined in:
- lib/search_logger/exec.rb
Instance Attribute Summary collapse
-
#argv ⇒ Object
readonly
Returns the value of attribute argv.
-
#command ⇒ Object
Returns the value of attribute command.
Instance Method Summary collapse
- #asks_for_database_config ⇒ Object
- #export_to_csv_file ⇒ Object
-
#initialize(argv) ⇒ Exec
constructor
A new instance of Exec.
- #input_text ⇒ Object
- #load_xml ⇒ Object
- #run ⇒ Object
- #save_into_mysql(google_results) ⇒ Object
- #search_google(query_string) ⇒ Object
- #valid_argv? ⇒ Boolean
- #valid_file? ⇒ Boolean
Constructor Details
#initialize(argv) ⇒ Exec
Returns a new instance of Exec.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/search_logger/exec.rb', line 8 def initialize argv @argv = argv unless valid_argv? puts "Please, specify a xml file with keywords." puts "" puts "Example:" puts "" puts "\s\ssearch_logger ~/my/folder/keywords.xml" exit end unless valid_file? puts "The file you specified doesn't exist." exit end puts "Please, enter your MySQL database information." asks_for_database_config end |
Instance Attribute Details
#argv ⇒ Object (readonly)
Returns the value of attribute argv.
6 7 8 |
# File 'lib/search_logger/exec.rb', line 6 def argv @argv end |
#command ⇒ Object
Returns the value of attribute command.
5 6 7 |
# File 'lib/search_logger/exec.rb', line 5 def command @command end |
Instance Method Details
#asks_for_database_config ⇒ Object
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 |
# File 'lib/search_logger/exec.rb', line 27 def asks_for_database_config database_config = { database: "search_logger", host: "localhost", username: "root" } print "Host address (defaults to 'localhost'): " input = input_text and !input.empty? and database_config[:host] = input print "Username (defaults to 'root'): " input = input_text and !input.empty? and database_config[:username] = input system "stty -echo" print "Password: " database_config[:password] = input_text system "stty echo" begin @database_connection = SearchLogger::Persistence.new(database_config) puts "\n\nA connection was established, starting operation.\n\n" rescue puts "The specified DB does not exists. Please, try again.\n\n" asks_for_database_config end end |
#export_to_csv_file ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/search_logger/exec.rb', line 117 def export_to_csv_file csv_path = ENV["HOME"] + "/search_logger.csv" csv_file = File.open(csv_path, "wb") print "3) Loading data from MySQL google_results table... " data = @database_connection.table("google_results").load_data print "\e[0;32mdone.\n\e[0m" print "4) Creating CSV file and adding data in #{csv_path}..." File.delete csv_path if File.exists? csv_path CSVExporter.new.export data, to: csv_file print "\e[0;32mdone.\n\e[0m" end |
#input_text ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/search_logger/exec.rb', line 54 def input_text begin STDOUT.flush STDIN.gets.strip rescue end end |
#load_xml ⇒ Object
93 94 95 |
# File 'lib/search_logger/exec.rb', line 93 def load_xml xml_parser = SearchLogger::XmlParser.new(@argv.first).parse end |
#run ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/search_logger/exec.rb', line 70 def run puts "1) Parsing the XML file" xml = load_xml puts "2) Searching Google and saving to MySQL (first 2 pages, 100 results each)" xml.each do |value| puts "Keyword: #{value.to_s}" print "\s\sGoogle: " google_results = search_google(value) print "\e[0;32mdone.\e[0m " print "\s\sMySQL: " save_into_mysql(google_results) print "\e[0;32mdone.\e[0m\n" end puts "" export_to_csv_file puts "\nCongratulations! Everything worked as expected. Please audit the CSV file to guarantee the quality of the data." end |
#save_into_mysql(google_results) ⇒ Object
112 113 114 115 |
# File 'lib/search_logger/exec.rb', line 112 def save_into_mysql(google_results) persistence = @database_connection persistence.data(google_results).table('google_results').save end |
#search_google(query_string) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/search_logger/exec.rb', line 97 def search_google(query_string) # source = File.open('spec/support/file_repository/google_result_2.html').read page_one = SearchLogger::GoogleParser.new.query(query_string).per_page(100).page(1).search page_two = SearchLogger::GoogleParser.new.query(query_string).per_page(100).page(2).last_result(page_one).search # page_one = SearchLogger::GoogleParser.new(source).search # page_two = SearchLogger::GoogleParser.new(source).last_result(page_one).search results = [] position = 1 (page_one + page_two).each do |e| results << e.as_ary position += 1 end results end |
#valid_argv? ⇒ Boolean
62 63 64 |
# File 'lib/search_logger/exec.rb', line 62 def valid_argv? @argv.length > 0 end |
#valid_file? ⇒ Boolean
66 67 68 |
# File 'lib/search_logger/exec.rb', line 66 def valid_file? File.exists? @argv[0] end |