Module: TempmailSdk::Providers::NeighboursSh

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

Overview

neighbours-sh 渠道实现 公共收件箱模式,任意用户名即可收信,无需注册 API: https://neighbours.sh/api/v1

Constant Summary collapse

CHANNEL =
"neighbours-sh"
API_BASE =
"https://neighbours.sh/api/v1"
DOMAIN =
"neighbours.sh"
HEADERS =
{
  "Accept" => "application/json",
  "User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36"
}.freeze

Class Method Summary collapse

Class Method Details

.fetch_detail(address, uid) ⇒ Object

获取单封邮件详情



77
78
79
80
81
82
83
# File 'lib/tempmail_sdk/providers/neighbours_sh.rb', line 77

def fetch_detail(address, uid)
  data = request_json("/inbox/#{URI.encode_www_form_component(address)}/#{URI.encode_www_form_component(uid)}", allow_404: true)
  return nil unless data.is_a?(Hash)

  detail = data["data"]
  detail.is_a?(Hash) ? detail : nil
end

.first_address(value) ⇒ Object

从 from/to 结构中提取首个可用地址



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tempmail_sdk/providers/neighbours_sh.rb', line 26

def first_address(value)
  return "" if value.nil?
  return value.strip if value.is_a?(String)

  if value.is_a?(Array)
    value.each do |item|
      hit = first_address(item)
      return hit unless hit.empty?
    end
    return ""
  end

  if value.is_a?(Hash)
    addr = value["address"].to_s.strip
    return addr unless addr.empty?

    text = value["text"].to_s.strip
    return text if text.include?("@")

    return first_address(value["value"])
  end

  value.to_s.strip
end

.flatten_message(detail, recipient) ⇒ Object

将邮件详情映射为标准化中间格式



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tempmail_sdk/providers/neighbours_sh.rb', line 62

def flatten_message(detail, recipient)
  {
    "id" => (detail["uid"] || "").to_s,
    "from" => first_address(detail["from"]),
    "to" => first_address(detail["to"]).then { |v| v.empty? ? recipient : v },
    "subject" => (detail["subject"] || "").to_s,
    "text" => (detail["text"] || "").to_s,
    "html" => (detail["html"] || "").to_s,
    "date" => (detail["date"] || "").to_s,
    "is_read" => false,
    "attachments" => detail["attachments"] || []
  }
end

.generate_emailEmailInfo

创建临时邮箱(公共收件箱,本地生成地址)

Returns:



87
88
89
90
# File 'lib/tempmail_sdk/providers/neighbours_sh.rb', line 87

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

.get_emails(email) ⇒ Array<Email>

获取邮件列表

Parameters:

  • email (String)

Returns:



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/tempmail_sdk/providers/neighbours_sh.rb', line 95

def get_emails(email)
  address = email.to_s.strip
  raise "neighbours-sh: 缺少邮箱地址" if address.empty?

  data = request_json("/inbox/#{URI.encode_www_form_component(address)}", allow_404: true)
  return [] unless data

  rows = data["data"]
  return [] unless rows.is_a?(Array)

  rows.select { |item| item.is_a?(Hash) }.filter_map do |item|
    uid = item["uid"].to_s.strip
    next if uid.empty?

    detail = fetch_detail(address, uid)
    next unless detail

    Normalize.normalize_email(flatten_message(detail, address), address)
  end
end

.random_localObject

生成随机用户名:前缀 sdk + 10 位小写字母数字



20
21
22
23
# File 'lib/tempmail_sdk/providers/neighbours_sh.rb', line 20

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

.request_json(path, allow_404: false) ⇒ Object

请求 JSON,allow_404 时 404 返回 nil



52
53
54
55
56
57
58
59
# File 'lib/tempmail_sdk/providers/neighbours_sh.rb', line 52

def request_json(path, allow_404: false)
  resp = Http.get("#{API_BASE}#{path}", headers: HEADERS, timeout: 15)
  return nil if allow_404 && resp.status_code == 404

  resp.raise_for_status
  data = resp.json
  data.is_a?(Hash) ? data : nil
end