Class: DoiExtractor::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/doi_extractor/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, std_out = STDOUT, input_callback = nil) ⇒ Command

Returns a new instance of Command.



22
23
24
25
26
27
# File 'lib/doi_extractor/command.rb', line 22

def initialize(options, std_out = STDOUT, input_callback = nil)
  @options = options
  @std_out = std_out
  @user_input_callback = input_callback || default_input_callback
  @log = Logger::get_log
end

Instance Attribute Details

#logObject

Returns the value of attribute log.



20
21
22
# File 'lib/doi_extractor/command.rb', line 20

def log
  @log
end

#optionsObject (readonly)

Returns the value of attribute options.



19
20
21
# File 'lib/doi_extractor/command.rb', line 19

def options
  @options
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



19
20
21
# File 'lib/doi_extractor/command.rb', line 19

def start_time
  @start_time
end

#std_outObject

Returns the value of attribute std_out.



20
21
22
# File 'lib/doi_extractor/command.rb', line 20

def std_out
  @std_out
end

#user_input_callbackObject

Returns the value of attribute user_input_callback.



20
21
22
# File 'lib/doi_extractor/command.rb', line 20

def user_input_callback
  @user_input_callback
end

Class Method Details

.for(options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/doi_extractor/command.rb', line 4

def self.for(options)
  case options.command
    when 'status'
      StatusCommand.new(options)
    when 'create'
      CreateCommand.new(options)
    when 'download'
      DownloadCommand.new(options)
    when 'cancel'
      CancelCommand.new(options)
    else
      raise "Invalid options: #{options.inspect}"
  end
end

Instance Method Details

#executeObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/doi_extractor/command.rb', line 29

def execute
  begin
    @start_time = Time.now
    cmd = self.class.name.split('::').last.sub('Command', '')
    log.info "Starting #{cmd} Command:\n    #{options.to_hash}"
    say("starting #{cmd} Command", true)
    say("api: #{options.api_uri}", true)
    say("path: #{options.download_base_path}", true)
    _execute
    log.info "Finished #{cmd} Command"
    say("Finished in #{(Time.now - @start_time).round(2)} seconds", true)
  rescue CommandFailError => e
    log.error "#{e.class}: #{e.message}\n    #{e.backtrace.join("\n    ")}"
    raise e
  rescue => e
    log.error "#{e.class}: #{e.message}\n    #{e.backtrace.join("\n    ")}"
    raise CommandFailError.new('An Unexpected Error Has Occurred', e)
  end
end