Class: Quandl::Command::Tasks::Delete

Inherits:
Base
  • Object
show all
Defined in:
lib/quandl/command/tasks/delete.rb

Instance Attribute Summary

Attributes inherited from Base

#args, #options, #request_timer

Instance Method Summary collapse

Methods inherited from Base

#ask_yes_or_no, authenticated_users_only!, autoload_client_library, call, #call, command_name, configure, #current_user, #debug, description, disable!, disable_in_gem!, disabled?, #error, #fatal, #force_yes?, #info, inherited, #initialize, lang, language, #logger, options, #summarize, #summarize_hash, syntax, t, #table, #verbose?, warn_unauthenticated_users

Constructor Details

This class inherits a constructor from Quandl::Command::Tasks::Base

Instance Method Details

#confirmed?(dataset) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
# File 'lib/quandl/command/tasks/delete.rb', line 85

def confirmed?(dataset)
  return true if force_yes?
  info summarize dataset.attributes.slice('source_code', 'code', 'name', 'from_date', 'to_date', 'column_names', 'created_at')
  ask_yes_or_no
end

#delete(code) ⇒ Object



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

def delete(code)
  return error("You must provide a code!") if code.blank?
  # find by code
  dataset = Quandl::Client::Dataset.find(code)
  # fail fast if dataset does not exist
  return error(table(Quandl::Client::HTTP_STATUS_CODES[404], code)) if dataset.nil?
  return report(dataset) unless dataset.exists?
  # confirm deletion
  return error("You must confirm deletion!") unless confirmed?(dataset)
  # delete if exists
  dataset.destroy if dataset.exists?
  # output status
  report(dataset)
end

#delete_each_argumentObject



36
37
38
39
40
41
# File 'lib/quandl/command/tasks/delete.rb', line 36

def delete_each_argument
  # delete each arg
  args.each{|code| delete_with_pool(code) }
  # wait for all deletes to finish
  pool.shutdown
end

#delete_each_stdinObject



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

def delete_each_stdin
  return error("Cannot delete datasets from STDIN unless --force-yes is set!") unless force_yes?
  # treat each line from stdin as a code
  $stdin.each_line{|code| delete_with_pool(code) }
  # wait for deletes to finish
  pool.shutdown
end

#delete_with_pool(code) ⇒ Object



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

def delete_with_pool(code)
  # sanitize
  code = code.to_s.strip.rstrip
  # if --force-yes send the deletes async
  if force_yes?
    pool.process{ delete( code ) }
  # otherwise send each delete sync and wait for confirmation
  else
    delete( code )
  end
end

#executeObject



29
30
31
32
33
34
# File 'lib/quandl/command/tasks/delete.rb', line 29

def execute
  # download using arguments when present
  return delete_each_argument if args.first.present?
  # otherwise delete using stdin
  delete_each_stdin
end

#report(dataset) ⇒ Object



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

def report(dataset)
  # message
  m = table dataset.elapsed_request_time_ms, dataset.full_code
  # report
  dataset.status == 200 ? info(table("Deleted", m)) : error(table(dataset.human_status, m))
end