Class: Quandl::Command::Tasks::Download

Inherits:
Base
  • Object
show all
Defined in:
lib/quandl/command/tasks/download.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!, call, #call, command_name, configure, #current_user, #debug, description, disable!, disabled?, #error, #fatal, #force_yes?, #info, #initialize, #logger, options, #summarize, #summarize_hash, syntax, #table, #verbose?, warn_unauthenticated_users

Constructor Details

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

Instance Method Details

#collapseObject



88
89
90
# File 'lib/quandl/command/tasks/download.rb', line 88

def collapse
  options.collapse
end

#data_paramsObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/quandl/command/tasks/download.rb', line 96

def data_params
  params = {}
  self.class.options.each do |class_type, opts|
    opts.each do |name, desc|
      if options.is_a?(OpenStruct)
        params[name] = self.options.send(name)
      else
        params[name] = self.options[name] if self.options[name].present?
      end
      
    end
  end
  params
end

#download(code) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/quandl/command/tasks/download.rb', line 52

def download(code)
  timer = Time.now
  # find dataset
  dataset = Quandl::Client::Dataset.find( code )
  # fail fast
  return error(table(Quandl::Client::HTTP_STATUS_CODES[404], code)) if dataset.nil?
  # set data operations
  dataset.data.assign_attributes(data_params) unless dataset.blank?
  # send request & check for errors.
  if !dataset.exists? && !dataset.valid?
    # raise detailed error
    return error( table( dataset.human_status, code, dataset.elapsed_request_time_ms ) )
  end
  # generate qdf
  elapsed = timer.elapsed_ms
  qdf = dataset.to_qdf
  # write to STDOUT
  mutex.synchronize{
    debug("# #{dataset.try(:full_url)} downloaded in #{elapsed}")
    info(qdf)
  }
  true
end

#download_each_argumentObject



38
39
40
41
42
43
# File 'lib/quandl/command/tasks/download.rb', line 38

def download_each_argument
  args.each do |code|
    pool.process{ download(code) }
  end
  pool.shutdown
end

#download_each_stdinObject



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

def download_each_stdin
  $stdin.each_line do |code|
    pool.process{ download(code.strip.rstrip) }
  end
  pool.shutdown
end

#executeObject



31
32
33
34
35
36
# File 'lib/quandl/command/tasks/download.rb', line 31

def execute
  # download using arguments when present
  return download_each_argument if args.first.present?
  # otherwise download using stdin
  download_each_stdin
end

#orderObject



84
85
86
# File 'lib/quandl/command/tasks/download.rb', line 84

def order
  options.order
end

#transformObject



92
93
94
# File 'lib/quandl/command/tasks/download.rb', line 92

def transform
  options.transform
end

#trim_endObject



80
81
82
# File 'lib/quandl/command/tasks/download.rb', line 80

def trim_end
  options.trim_end
end

#trim_startObject



76
77
78
# File 'lib/quandl/command/tasks/download.rb', line 76

def trim_start
  options.trim_start
end