Class: Code::Object::Smtp

Inherits:
Dictionary show all
Defined in:
lib/code/object/smtp.rb

Constant Summary collapse

CLASS_DOCUMENTATION =
{
  name: "Smtp",
  description: "stores smtp settings and sends email messages.",
  examples: %w[Smtp Smtp.new Smtp.new.functions.keys.include?(:send)]
}.freeze
INSTANCE_FUNCTIONS =
{
  "send" => {
    name: "send",
    description: "sends an email using the receiver's smtp settings.",
    examples: %w[
      Smtp.new.respond_to?(:send)
      Smtp.new.functions.keys.include?(:send)
      Smtp.new.instance_functions.keys.include?(:send)
    ]
  }
}.freeze
DEFAULT_PORT =
587
DEFAULT_TIMEOUT =
10
DEFAULT_DOMAIN =
"localhost.localdomain"
ALLOWED_PORTS =
[465, 587].freeze

Constants inherited from Dictionary

Dictionary::CLASS_FUNCTIONS

Constants inherited from Code::Object

CLASS_FUNCTIONS, NUMBER_CLASSES

Constants included from Concerns::Shared

Concerns::Shared::COMPOUND_ASSIGNMENT_OPERATORS, Concerns::Shared::OPERATOR_METHOD_ALIASES, Concerns::Shared::SHARED_OPERATORS

Instance Attribute Summary

Attributes included from Concerns::Shared

#functions, #raw

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Dictionary

#<=>, call, #code_any?, code_assign, #code_associate, #code_clear, #code_compact, #code_compact!, #code_deep_duplicate, #code_delete, #code_delete_if, #code_delete_unless, #code_dig, #code_each, #code_each_key, #code_each_pair, #code_each_value, #code_empty?, code_entries, #code_except, #code_fetch, #code_fetch_values, #code_flatten, code_from_entries, #code_get, #code_has_key?, code_has_own?, #code_has_own?, #code_has_value?, #code_invert, #code_keep_if, #code_keep_unless, #code_key, #code_keys, #code_map, #code_merge, #code_merge!, #code_reject, #code_reject!, #code_replace, #code_right_associate, #code_select, #code_select!, #code_set, #code_shift, #code_size, #code_slice, #code_store, #code_to_context, #code_to_dictionary, #code_to_list, #code_to_query, #code_transform_keys, #code_transform_keys!, #code_transform_values, #code_transform_values!, #code_update, #code_values, #code_values_at, #initialize, #present?

Methods inherited from Code::Object

class_documentation, class_functions, code_new, #code_new, documentation, documentation_for, documented_functions_for, function_documentation_for, function_documentation_registry_for, functions, inherited_function_documentation_for, #initialize, instance_functions, maybe, #name, repeat, sorted_dictionary, |

Methods included from Concerns::Shared

#<=>, #==, #as_json, #blank?, #code_and, #code_as_json, #code_blank?, #code_class_functions, #code_compare, #code_deep_duplicate, #code_different, #code_documentable_functions, #code_documentation, #code_duplicate, #code_dynamic_call, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_false?, #code_falsy?, code_fetch, #code_fetch, #code_functions, code_get, #code_get, #code_greater, #code_greater_or_equal, #code_has_key?, #code_inclusive_range, #code_inspect, #code_instance_functions, #code_instance_of?, #code_is_a?, #code_itself, #code_less, #code_less_or_equal, #code_name, #code_nothing?, #code_or, #code_presence, #code_presence_in, #code_present?, #code_respond_to?, #code_same_object?, #code_self, code_set, #code_set, #code_something?, #code_strict_different, #code_strict_equal, #code_tap, #code_then, #code_to_boolean, #code_to_class, #code_to_date, #code_to_decimal, #code_to_dictionary, #code_to_duration, #code_to_integer, #code_to_json, #code_to_list, #code_to_nothing, #code_to_parameter, #code_to_range, #code_to_string, #code_to_time, #code_true?, #code_truthy?, #eql?, #falsy?, #hash, #inspect, #multi_fetch, #nothing?, #present?, #sig, #something?, #succ, #to_code, #to_i, #to_json, #to_s, #truthy?

Constructor Details

This class inherits a constructor from Code::Object::Dictionary

Class Method Details

.function_documentation(scope) ⇒ Object



23
24
25
26
27
# File 'lib/code/object/smtp.rb', line 23

def self.function_documentation(scope)
  return INSTANCE_FUNCTIONS if scope == :instance

  {}
end

Instance Method Details

#call(**args) ⇒ Object



34
35
36
37
38
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
# File 'lib/code/object/smtp.rb', line 34

def call(**args)
  code_operator = args.fetch(:operator, nil).to_code
  code_arguments = args.fetch(:arguments, []).to_code
  code_value = code_arguments.code_first

  case code_operator.to_s
  when "send"
    sig(args) do
      {
        from: String.maybe,
        to: String.maybe,
        subject: String.maybe,
        body: String.maybe,
        body_text: String.maybe,
        body_html: String.maybe
      }
    end

    code_send(
      from: code_value.code_get("from"),
      to: code_value.code_get("to"),
      subject: code_value.code_get("subject"),
      body: code_value.code_get("body"),
      body_text: code_value.code_get("body_text"),
      body_html: code_value.code_get("body_html")
    )
  else
    super
  end
end

#code_send(from: nil, to: nil, subject: nil, body: nil, body_text: nil, body_html: nil) ⇒ Object



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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/code/object/smtp.rb', line 65

def code_send(
  from: nil,
  to: nil,
  subject: nil,
  body: nil,
  body_text: nil,
  body_html: nil
)
  code_from = from.to_code
  code_to = to.to_code
  code_subject = subject.to_code
  code_body = body.to_code
  code_body_text = body_text.to_code
  code_body_html = body_html.to_code

  mail = Mail.new
  mail.from = code_from.to_s
  mail.to = code_to.to_s
  mail.subject = code_subject.to_s

  text_part = Mail::Part.new
  text_part.content_type = "text/plain; charset=UTF-8"
  text_part.body = code_body_text.to_s.presence || code_body.to_s

  html_part = Mail::Part.new
  html_part.content_type = "text/html; charset=UTF-8"
  html_part.body = code_body_html.to_s

  mail.content_type = "multipart/alternative"
  mail.add_part(text_part)
  mail.add_part(html_part) if code_body_html.to_s.present?

  address = code_get("address").to_s
  port = code_get("port").nothing? ? DEFAULT_PORT : code_get("port").to_i
  authentication = code_get("authentication").to_s
  starttls = code_get("enable_starttls_auto")
  if starttls.something? && starttls.falsy?
    raise Error, "smtp: tls is required"
  end

  resolved_ip = validate_delivery_target!(address, port)

  encoded_message = mail.encoded
  ::Code.ensure_input_size!(encoded_message, label: "smtp message")

  deliver_mail(
    mail,
    address: address,
    resolved_ip: resolved_ip,
    port: port,
    user_name: code_get("user_name").to_s,
    password: code_get("password").to_s,
    authentication: authentication
  )

  Nothing.new
end