Class: DoiExtractor::Options

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

Constant Summary collapse

PROJECTS =
IpumsUriBuilder::project_list
ENVIRONMENTS =
%w(local demo internal staging live)
COMMANDS =
%w(create download status cancel)
OPTIONS =
[
    :command,
    :verbose,
    :environment,
    :project,
    :api_uri,
    :api_username,
    :api_password,
    :download_base_path,
    :doi_version,
    :year,
    :email,
    :force,
    :extract_group_id
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = nil) ⇒ Options

Returns a new instance of Options.



35
36
37
# File 'lib/doi_extractor/options.rb', line 35

def initialize(attrs = nil)
  assign_attrs(attrs, OPTIONS)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



32
33
34
# File 'lib/doi_extractor/options.rb', line 32

def errors
  @errors
end

Class Method Details

.for_command(cmd, attrs = nil) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/doi_extractor/options.rb', line 24

def self.for_command(cmd, attrs = nil)
  if COMMANDS.include? cmd
    new({command: cmd}.merge(attrs || {}))
  else
    raise 'Invalid Command'
  end
end

Instance Method Details

#to_hashObject



104
105
106
# File 'lib/doi_extractor/options.rb', line 104

def to_hash
  Hash[OPTIONS.map { |o| [o, self.send(o)] }].merge(errors: self.errors)
end

#valid?Boolean

Returns:

  • (Boolean)


99
100
101
102
# File 'lib/doi_extractor/options.rb', line 99

def valid?
  validate
  errors.empty?
end

#validateObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/doi_extractor/options.rb', line 39

def validate
  @errors = []

  if environment.nil?
    self.environment = 'live'
  end

  if !COMMANDS.include?(command)
    errors << 'Invalid Command'
  end

  if !ENVIRONMENTS.include?(environment)
    errors << 'Invalid Environment'
  end

  if !PROJECTS.include?(project)
    errors << 'Invalid Project'
  end

  # if command, env or project are invalid, stop performing further validation
  return if errors.any?

  self.api_uri = calculate_api_uri if api_uri.nil?
  errors << 'Missing API URI' if self.api_uri.nil?

  # For demo, internal, and staging envs, use the basic auth creds by default
  if %w(demo internal staging).include?(environment)
    self.api_username ||= 'hcp'
    self.api_password ||= '1234'
  end

  if command == 'create'
    self.year ||= Time.now.year.to_s

    if self.year !~ /^\d{4}$/
      errors << 'Year must be a 4 digit year'
    end

    if m = /[vV]?(\d+\.\d+)/.match(self.doi_version)
      self.doi_version = m[1]
    else
      errors << 'DOI version is required and must match the pattern VX.X'
    end
  end

  if %w(download status).include?(command)
    self.download_base_path ||= calculate_download_base_path

    if self.download_base_path.nil? || self.download_base_path.strip.empty?
      errors << 'Unable to determine download base path'
    end
  end

  if %w(download cancel).include?(command)
    unless self.extract_group_id
      errors << 'Missing extract request group id'
    end
  end
end