Module: TempmailSdk::Providers::Temporam

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

Overview

Temporam 渠道实现 API: https://temporam.com/api

Constant Summary collapse

CHANNEL =
"temporam"
ORIGIN =
"https://temporam.com"
HEADERS =
{
  "Accept" => "application/json",
  "User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36"
}.freeze

Class Method Summary collapse

Class Method Details

.fetch_domainsObject

获取可用域名列表



34
35
36
37
38
39
40
# File 'lib/tempmail_sdk/providers/temporam.rb', line 34

def fetch_domains
  data = get_json("/api/domains")
  domains = (data["data"] || []).select { |item| item.is_a?(Hash) }.map { |item| (item["domain"] || "").strip }.reject(&:empty?)
  raise "temporam: 域名列表为空" if domains.empty?

  domains
end

.generate_email(domain = nil) ⇒ EmailInfo

创建临时邮箱

Parameters:

  • domain (String, nil) (defaults to: nil)

Returns:



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/tempmail_sdk/providers/temporam.rb', line 56

def generate_email(domain = nil)
  domains = fetch_domains
  if domain && !domain.empty?
    raise "temporam: 不支持的域名 #{domain}" unless domains.include?(domain)

    selected = domain
  else
    selected = domains.sample
  end
  email = "#{random_local}@#{selected}"
  EmailInfo.new(channel: CHANNEL, email: email, token: email)
end

.get_emails(token, email) ⇒ Array<Email>

获取邮件列表

Parameters:

  • token (String)
  • email (String)

Returns:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tempmail_sdk/providers/temporam.rb', line 73

def get_emails(token, email)
  recipient = token.to_s.empty? ? email : token
  raise "temporam: 缺少邮箱地址" if recipient.to_s.strip.empty?

  data = get_json("/api/emails?email=#{URI.encode_www_form_component(recipient)}&limit=50")
  rows = data["data"] || []
  rows.select { |row| row.is_a?(Hash) }.map do |row|
    raw = row
    msg_id = (row["id"] || row["uuid"] || "").to_s
    unless msg_id.empty?
      begin
        detail = get_json("/api/emails/#{URI.encode_www_form_component(msg_id)}")
        raw = detail["data"] if detail["data"].is_a?(Hash)
      rescue StandardError
        # 详情获取失败时使用列表数据
      end
    end
    normalize_raw(raw, recipient)
  end
end

.get_json(path) ⇒ Object

GET 请求并解析 JSON



27
28
29
30
31
# File 'lib/tempmail_sdk/providers/temporam.rb', line 27

def get_json(path)
  resp = Http.get("#{ORIGIN}#{path}", headers: HEADERS)
  resp.raise_for_status
  resp.json
end

.normalize_raw(raw, email) ⇒ Object

将原始邮件映射为标准化格式



43
44
45
46
47
48
49
50
51
# File 'lib/tempmail_sdk/providers/temporam.rb', line 43

def normalize_raw(raw, email)
  flat = raw.merge(
    "id" => raw["id"] || raw["uuid"] || "",
    "from" => raw["fromEmail"] || raw["from"] || "",
    "to" => raw["toEmail"] || raw["to"] || email,
    "date" => raw["createdAt"] || raw["created_at"] || raw["date"] || ""
  )
  Normalize.normalize_email(flat, email)
end

.random_local(length = 18) ⇒ Object

生成随机用户名



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

def random_local(length = 18)
  chars = ("a".."z").to_a + ("0".."9").to_a
  "sdk" + Array.new(length) { chars.sample }.join
end