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



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

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:



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

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

#find(key, default = nil) ⇒ Object

Get the value by key

Parameters:

  • key

    The key to fetch value. The key might be a plain text or symbol

  • default (defaults to: nil)

    The default/alternative value if key not found

Returns:

  • The value by key or alternative/default value.



153
154
155
156
# File 'lib/lazylead/opts.rb', line 153

def find(key, default = nil)
  return default if key.nil?
  to_h[key.to_s] || to_h[key.to_sym] || default
end

#jira_defaultsObject

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



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

def jira_defaults
  {
    max_results: fetch("max_results", 50),
    fields: jira_fields,
    limit: to_h["limit"]
  }
end

#jira_fieldsObject

Default fields which to fetch within the Jira issue



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

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

#merge(args) ⇒ Object



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

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

#msg_attachments(delim = ",") ⇒ Object



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

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.



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

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

#msg_cc(delim = ",") ⇒ Object



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

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

#msg_from(delim = ",") ⇒ Object



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

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

#msg_to(delim = ",") ⇒ Object



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

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)


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

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)             => []


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

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