-
-
Save Thermatix/9b0b7b4b49ddb9b6c832a8590130cc4f to your computer and use it in GitHub Desktop.
Dynamised executable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require_relative '../lib/dynamised' | |
require 'commander' | |
module Dynamised | |
class CLI | |
include Commander::Methods | |
def run | |
program :name, "Dynamised" | |
program :version, META::Version | |
program :description, META::Description | |
command :run do |c| | |
c.syntax = 'dynamised run <script>' | |
c.description = 'scrapes with given scraper' | |
c.option "--[no-]db", 'Turns off db, scraper will pull and store directly to end file, no resume' | |
c.action do |args,options| | |
options.default db: true | |
# nodb = {nodb: options.db} | |
script_path = check_and_convert(args.first) | |
class_name = get_class_name(args.first) | |
create_temp_class(class_name,File.read(script_path)) | |
class_ref = Scraper.fetch(class_name) | |
spinner = TTY::Spinner.new("[:spinner] scraping with %s" % class_name) | |
class_ref.new.pull_and_store do | |
spinner.spin | |
end | |
spinner.success("(Successfull)") | |
end | |
end | |
command :test do |c| | |
c.syntax = 'dynamised test <script>' | |
c.description = "tests given scraper" | |
c.action do |args,options| | |
script_path = check_and_convert(args.first) | |
class_name = get_class_name(args.first) | |
create_temp_class(class_name,File.read(script_path)) | |
class_ref = Scraper.fetch(class_name) | |
class_ref.new.pull_and_check | |
end | |
end | |
alias_command :r, :run | |
alias_command :t, :test | |
default_command :help | |
run! | |
end | |
def check_and_convert(path) | |
unless path | |
puts "No script file given" | |
exit | |
end | |
script_path = File.expand_path(path, Dir.pwd ) | |
unless File.exists?(script_path) | |
puts "File name %s doesn't exist" % script_path | |
exit | |
end | |
script_path | |
end | |
def get_class_name(string) | |
string.split('/').last.split('.').first.gsub(/ /,'_').capitalize | |
end | |
def create_temp_class(class_name,script) | |
Dynamised.module_eval <<-RUBY | |
class #{class_name} < Scraper | |
#{script} | |
end | |
RUBY | |
end | |
end | |
end | |
Dynamised::CLI.new.run if $0 == __FILE__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment