Class: Lazylead::Opts

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/lazylead/opts.rb

Overview

Default options for all lazylead tasks.

Author

Yurii Dubinka ([email protected])

Copyright

Copyright © 2019-2020 Yurii Dubinka

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(origin = {}) ⇒ Opts

Returns a new instance of Opts.



39
40
41
# File 'lib/lazylead/opts.rb', line 39

def initialize(origin = {})
  @origin = origin
end

Instance Method Details

#blank?(key) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/lazylead/opts.rb', line 55

def blank?(key)
  to_h[key].nil? || @origin[key].blank?
end

#construct(field, delim: ",") ⇒ Object



142
143
144
# File 'lib/lazylead/opts.rb', line 142

def construct(field, delim: ",")
  slice(field, delim).map(&:constantize).map(&:new)
end

#decrypt(key, sid) ⇒ Object

Decrypt particular option using cryptography salt

Parameters:

  • key

    option to be decrypted

  • sid

    the name of the salt to be used for the description

See Also:



80
81
82
83
84
85
# File 'lib/lazylead/opts.rb', line 80

def decrypt(key, sid)
  text = to_h[key]
  return text if text.blank? || text.nil?
  return Salt.new(sid).decrypt(text) if ENV.key? sid
  text
end

#jira_defaultsObject

Default Jira options to use during search for all Jira-based tasks.



64
65
66
67
68
69
# File 'lib/lazylead/opts.rb', line 64

def jira_defaults
  {
    max_results: fetch("max_results", 50),
    fields: jira_fields
  }
end

#jira_fieldsObject

Default fields which to fetch within the Jira issue



72
73
74
# File 'lib/lazylead/opts.rb', line 72

def jira_fields
  to_h.fetch("fields", "").split(",").map(&:to_sym)
end

#merge(args) ⇒ Object



87
88
89
90
# File 'lib/lazylead/opts.rb', line 87

def merge(args)
  return self unless args.is_a? Hash
  Opts.new @origin.merge(args)
end

#msg_attachments(delim = ",") ⇒ Object



112
113
114
# File 'lib/lazylead/opts.rb', line 112

def msg_attachments(delim = ",")
  sliced(delim, :attachments, "attachments").select { |f| File.file? f }
end

#msg_body(template = "template") ⇒ Object

Construct html document from template and binds.



93
94
95
96
97
98
# File 'lib/lazylead/opts.rb', line 93

def msg_body(template = "template")
  Email.new(
    to_h[template],
    to_h.merge(version: Lazylead::VERSION)
  ).render
end

#msg_cc(delim = ",") ⇒ Object



104
105
106
# File 'lib/lazylead/opts.rb', line 104

def msg_cc(delim = ",")
  sliced delim, :cc, "cc"
end

#msg_from(delim = ",") ⇒ Object



108
109
110
# File 'lib/lazylead/opts.rb', line 108

def msg_from(delim = ",")
  sliced delim, :from, "from"
end

#msg_to(delim = ",") ⇒ Object



100
101
102
# File 'lib/lazylead/opts.rb', line 100

def msg_to(delim = ",")
  sliced delim, :to, "to"
end

#numeric?(key) ⇒ Boolean

Ensure that particular key from options is a positive numer

Opts.new("key" => "1").numeric? "key"   => true
Opts.new("key" => "0").numeric? "key"   => false
Opts.new("key" => ".").numeric? "key"   => false
Opts.new("key" => "nil").numeric? "key" => false

Returns:

  • (Boolean)


138
139
140
# File 'lib/lazylead/opts.rb', line 138

def numeric?(key)
  to_h[key].to_i.positive?
end

#slice(key, delim) ⇒ Object

Split text value by delimiter, trim all spaces and reject blank items



44
45
46
47
# File 'lib/lazylead/opts.rb', line 44

def slice(key, delim)
  return [] unless to_h.key? key
  trim to_h[key].split(delim)
end

#sliced(delim, *keys) ⇒ Object

Find the option by key and split by delimiter

Opts.new("key" => "a,b").sliced(",", "key")     => [a, b]
Opts.new(key: "a,b").sliced(",", :key)          => [a, b]
Opts.new(key: "a,b").sliced(",", "key", :key)   => [a, b]
Opts.new(key: "").sliced ",", :key)             => []


123
124
125
126
127
128
129
130
131
# File 'lib/lazylead/opts.rb', line 123

def sliced(delim, *keys)
  return [] if keys.empty?
  key = keys.detect { |k| key? k }
  val = to_h[key]
  return [] if val.nil? || val.blank?
  return val if val.is_a? Array
  return [val] unless val.include? delim
  slice key, delim
end

#to_hObject



59
60
61
# File 'lib/lazylead/opts.rb', line 59

def to_h
  @origin
end

#trim(arr) ⇒ Object

Trim all spaces and reject blank items in array



50
51
52
53
# File 'lib/lazylead/opts.rb', line 50

def trim(arr)
  return [] if arr.nil?
  arr.map(&:chomp).map(&:strip).reject(&:blank?)
end