Module: TempmailSdk::Providers::Fmail

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

Overview

fmail.men 渠道实现

Constant Summary collapse

CHANNEL =
"fmail"
BASE_URL =
"https://fmail.men"
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_json(path) ⇒ Object



17
18
19
20
21
22
# File 'lib/tempmail_sdk/providers/fmail.rb', line 17

def fetch_json(path)
  resp = Http.get("#{BASE_URL}#{path}", headers: HEADERS, timeout: 15)
  resp.raise_for_status
  data = resp.json
  data.is_a?(Hash) ? data : {}
end

.flatten_message(raw, recipient) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tempmail_sdk/providers/fmail.rb', line 24

def flatten_message(raw, recipient)
  {
    "id" => (raw["id"] || raw["uid"] || raw["token"]).to_s,
    "from" => (raw["sender"] || raw["from"]).to_s,
    "to" => (raw["recipient"] || raw["to"] || recipient).to_s,
    "subject" => (raw["subject"] || "").to_s,
    "text" => (raw["body_text"] || raw["text"] || raw["snippet"]).to_s,
    "html" => (raw["body_html"] || raw["html"]).to_s,
    "date" => (raw["received_at"] || raw["timestamp"] || raw["date"]).to_s,
    "is_read" => raw["is_read"] || false,
    "attachments" => raw["attachments"].is_a?(Array) ? raw["attachments"] : []
  }
end

.generate_email(domain = nil) ⇒ EmailInfo

Parameters:

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

    可选域名

Returns:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tempmail_sdk/providers/fmail.rb', line 40

def generate_email(domain = nil)
  selected = domain.to_s.strip.delete_prefix("@")
  path = "/v1/random"
  path = "#{path}?domain=#{URI.encode_www_form_component(selected)}" unless selected.empty?

  data = fetch_json(path)
  email = data["address"].to_s.strip
  if email.empty?
    username = data["username"].to_s.strip
    dom = data["domain"].to_s.strip
    email = "#{username}@#{dom}" if !username.empty? && !dom.empty?
  end
  raise "fmail: invalid random response" if email.empty? || !email.include?("@")

  EmailInfo.new(channel: CHANNEL, email: email)
end

.get_emails(email) ⇒ Array<Email>

Parameters:

  • email (String)

Returns:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/tempmail_sdk/providers/fmail.rb', line 59

def get_emails(email)
  value = email.to_s.strip
  local, _, domain = value.partition("@")
  raise "fmail: invalid email" if local.empty? || domain.empty?

  data = fetch_json(
    "/v1/inbox/#{URI.encode_www_form_component(local)}?domain=#{URI.encode_www_form_component(domain)}&limit=50"
  )
  rows = data["emails"]
  return [] unless rows.is_a?(Array)

  rows.select { |r| r.is_a?(Hash) }.map do |row|
    token = (row["token"] || row["id"]).to_s.strip
    if token.empty?
      Normalize.normalize_email(flatten_message(row, value), value)
    else
      begin
        detail = fetch_json("/v1/email/#{URI.encode_www_form_component(token)}")
        nested = detail["email"].is_a?(Hash) ? detail["email"] : detail
        Normalize.normalize_email(flatten_message(nested.is_a?(Hash) ? nested : row, value), value)
      rescue StandardError
        Normalize.normalize_email(flatten_message(row, value), value)
      end
    end
  end
end