Class: Quandl::Command::Tasks::Schedule

Inherits:
Quandl::Command::Task show all
Defined in:
lib/quandl/command/tasks/schedule.rb

Constant Summary collapse

DAYS_OF_THE_WEEK =
%w(Sunday Monday Tuesday Wednesday Thursday Friday Saturday)

Constants included from Quandl::Command::Task::Updatable

Quandl::Command::Task::Updatable::VERSION_URL

Instance Method Summary collapse

Methods included from Quandl::Command::Task::Inputable

#args_or_stdin, #each_line_in_background, #file_or_stdin

Methods included from Quandl::Command::Task::Reportable

#ask_to_send_crash_reports, #call

Methods included from Quandl::Command::Task::Clientable

#current_user

Methods included from Quandl::Command::Task::Updatable

#check_for_update, #check_for_update_once_daily

Methods included from Quandl::Command::Task::Threading

#await_thread_pool_lock!, #background_job, #exiting?, #force_load_json_support, #mutex, #obtain_thread_pool_lock, #release_thread_pool_lock, #shutdown_thread_pool_on_sigint, #thread_pool, #thread_pool_locked?, #thread_pool_locks, #threads, #threads?

Methods included from Quandl::Command::Task::Presentation

#present

Methods included from Quandl::Command::Task::Logging

#configure_logger, #debug, #error, #fatal, #info, #initialize_logger, #log_request_time, #logger, #start_request_timer, #stderr_logger, #summarize, #summarize_hash, #table

Methods included from Quandl::Command::Task::Commandable

#initialize

Methods included from Quandl::Command::Task::Validations

#run_task_validations!

Methods included from Quandl::Command::Task::Callbacks

#call

Methods included from Quandl::Command::Task::Configurable

#ask_for_confirmation!, #config, #confirmed?, #declared_params, #force_yes?, #page, #trace?, #verbose?

Instance Method Details

#addObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/quandl/command/tasks/schedule.rb', line 62

def add
  upload_file = Tempfile.new('temp')
  begin
    result=Quandl::Client::Scraper.create( name: name, scraper: file_to_send(args, upload_file), schedule_at: cron_at_utc)
    if result.valid?
      info("You have successfully scheduled '#{scraper.name}'.")
      info("#{schedule_message}")
    else
      error "#{Quandl::Command::Presenter.pretty_errors(result.errors.messages).to_s.gsub("\n", ' ')}"
    end
  ensure
    upload_file.close!
  end
end

#deleteObject



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/quandl/command/tasks/schedule.rb', line 86

def delete
  (info("'#{name}' does not exist "); return;) if scraper.blank?
  info("You are about to delete '#{scraper.name}'")
  return unless confirmed?
  result=scraper.destroy if scraper.respond_to?(:destroy)
  if result.valid?
    info("You have successfully deleted '#{scraper.name}'")
  else
    error "#{Quandl::Command::Presenter.pretty_errors(result.errors.messages).to_s.gsub("\n", ' ')}"
  end

end

#downloadObject



77
78
79
80
81
82
83
84
# File 'lib/quandl/command/tasks/schedule.rb', line 77

def download
  begin
    $stdout.binmode
    $stdout << open(scraper.scraper_url, "rb", {ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE}).read
  rescue => err
    present err
  end
end

#executeObject



43
44
45
46
47
48
49
50
# File 'lib/quandl/command/tasks/schedule.rb', line 43

def execute
  # fire a subcommand if specified
  return self.send(args.shift) if args.first.present? && self.respond_to?(args.first)
  # otherwise fire add
  return add if args.first.present? && args.first.downcase != "help"
  # otherwise display syntax
  info(syntax)
end

#listObject



52
53
54
55
56
57
58
59
60
# File 'lib/quandl/command/tasks/schedule.rb', line 52

def list
  if args.first
    (info("'#{name}' does not exist "); return;) if scraper.blank?
    script_info scraper
  else
    # create new blank scope and iterate over all pages
    Quandl::Client::Scraper.scope.new.each_in_page{|x| script_info(x) }
  end
end

#runObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/quandl/command/tasks/schedule.rb', line 99

def run
  (info("'#{name}' does not exist "); return;) if scraper.blank?
  info("You are about to run '#{scraper.name}'")
  return unless confirmed?
  result=scraper.run_now if scraper.respond_to?(:run_now)
  if result.valid?
    info("Your '#{scraper.name}' will run immediately.")
  else
    if result.status == 429
      info("You have exceeded your daily run limit.")
    else
      error "#{Quandl::Command::Presenter.pretty_errors(result.errors.messages).to_s.gsub("\n", ' ')}"
    end
  end

end