Module: TempmailSdk::Providers::Inboxkitten

Defined in:
lib/tempmail_sdk/providers/inboxkitten.rb

Overview

InboxKitten 渠道 — https://inboxkitten.com

Constant Summary collapse

CHANNEL =
"inboxkitten"
API_BASE =
"https://inboxkitten.com/api/v1/mail"
DOMAIN =
"inboxkitten.com"
HEADERS_JSON =
{ "Accept" => "application/json" }.freeze
HEADERS_HTML =
{ "Accept" => "text/html,*/*" }.freeze

Class Method Summary collapse

Class Method Details

.detail_raw(row, recipient) ⇒ Object



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
# File 'lib/tempmail_sdk/providers/inboxkitten.rb', line 45

def detail_raw(row, recipient)
  storage = row["storage"].is_a?(Hash) ? row["storage"] : {}
  message = row["message"].is_a?(Hash) ? row["message"] : {}
  headers = message["headers"].is_a?(Hash) ? message["headers"] : {}
  envelope = row["envelope"].is_a?(Hash) ? row["envelope"] : {}
  region = storage["region"].to_s.strip
  key = storage["key"].to_s.strip
  raw = {
    "id" => key, "messageId" => key,
    "from" => headers["from"] || envelope["sender"] || "",
    "to" => row["recipient"] || envelope["targets"] || recipient,
    "subject" => headers["subject"] || "", "timestamp" => row["timestamp"]
  }
  return raw if region.empty? || key.empty?

  begin
    meta = fetch_meta(region, key)
    html = fetch_html(region, key)
    raw["from"] = meta["name"] || raw["from"]
    raw["to"] = meta["recipients"] || raw["to"]
    raw["subject"] = meta["subject"] || raw["subject"]
    raw["text"] = HtmlUtils.html_to_text(html)
    raw["html"] = html
  rescue StandardError
    # 忽略详情失败
  end
  raw
end

.fetch_html(region, key) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/tempmail_sdk/providers/inboxkitten.rb', line 36

def fetch_html(region, key)
  resp = Http.get(
    "#{API_BASE}/getHtml?region=#{URI.encode_www_form_component(region)}&key=#{URI.encode_www_form_component(key)}",
    headers: HEADERS_HTML, timeout: 15
  )
  resp.raise_for_status
  resp.body
end

.fetch_meta(region, key) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/tempmail_sdk/providers/inboxkitten.rb', line 26

def fetch_meta(region, key)
  resp = Http.get(
    "#{API_BASE}/getKey?region=#{URI.encode_www_form_component(region)}&key=#{URI.encode_www_form_component(key)}",
    headers: HEADERS_JSON, timeout: 15
  )
  resp.raise_for_status
  data = resp.json
  data.is_a?(Hash) ? data : {}
end

.generate_emailEmailInfo

无需请求服务端,随机本地部分即可

Returns:



22
23
24
# File 'lib/tempmail_sdk/providers/inboxkitten.rb', line 22

def generate_email
  EmailInfo.new(channel: CHANNEL, email: "#{random_local}@#{DOMAIN}")
end

.get_emails(email) ⇒ Array<Email>

Parameters:

  • email (String)

Returns:



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tempmail_sdk/providers/inboxkitten.rb', line 76

def get_emails(email)
  local = email.to_s.strip.split("@").first.to_s
  raise "inboxkitten: empty email" if local.empty?

  resp = Http.get("#{API_BASE}/list?recipient=#{URI.encode_www_form_component(local)}",
                  headers: HEADERS_JSON, timeout: 15)
  resp.raise_for_status
  rows = resp.json
  return [] unless rows.is_a?(Array)

  rows.select { |r| r.is_a?(Hash) }.map { |item| Normalize.normalize_email(detail_raw(item, email), email) }
end

.random_localObject



15
16
17
18
# File 'lib/tempmail_sdk/providers/inboxkitten.rb', line 15

def random_local
  chars = "abcdefghijklmnopqrstuvwxyz0123456789".chars
  "sdk" + Array.new(16) { chars.sample }.join
end