Module: TempmailSdk::Providers::Duckmail
- Defined in:
- lib/tempmail_sdk/providers/duckmail.rb
Overview
DuckMail 渠道实现(与 mail.tm 同构 API) API: https://api.duckmail.sbs
Constant Summary collapse
- CHANNEL =
"duckmail"- BASE_URL =
"https://api.duckmail.sbs"- DEFAULT_HEADERS =
{ "Content-Type" => "application/json", "Accept" => "application/json", "Origin" => "https://duckmail.sbs", "Referer" => "https://duckmail.sbs/" }.freeze
Class Method Summary collapse
- .fetch_domains ⇒ Object
- .generate_email ⇒ EmailInfo
- .get_emails(email, token) ⇒ Array<Email>
- .random_string(length) ⇒ Object
Class Method Details
.fetch_domains ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/tempmail_sdk/providers/duckmail.rb', line 24 def fetch_domains resp = Http.get("#{BASE_URL}/domains?page=1", headers: DEFAULT_HEADERS) resp.raise_for_status data = resp.json members = data.is_a?(Array) ? data : (data["hydra:member"] || []) members.select { |d| d["isActive"] }.map { |d| d["domain"] } end |
.generate_email ⇒ EmailInfo
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/tempmail_sdk/providers/duckmail.rb', line 33 def generate_email domains = fetch_domains raise "No available domains" if domains.empty? address = "#{random_string(12)}@#{domains.sample}" password = random_string(16) acc = Http.post("#{BASE_URL}/accounts", headers: DEFAULT_HEADERS.merge("Content-Type" => "application/ld+json"), json: { "address" => address, "password" => password }) acc.raise_for_status tok = Http.post("#{BASE_URL}/token", headers: DEFAULT_HEADERS, json: { "address" => address, "password" => password }) tok.raise_for_status EmailInfo.new(channel: CHANNEL, email: address, token: tok.json["token"], created_at: acc.json["createdAt"]) end |
.get_emails(email, token) ⇒ Array<Email>
53 54 55 56 57 58 59 60 |
# File 'lib/tempmail_sdk/providers/duckmail.rb', line 53 def get_emails(email, token) resp = Http.get("#{BASE_URL}/messages?page=1", headers: DEFAULT_HEADERS.merge("Authorization" => "Bearer #{token}")) resp.raise_for_status data = resp.json = data.is_a?(Array) ? data : (data["hydra:member"] || []) .map { |msg| Normalize.normalize_email(MailTm.flatten(msg, email), email) } end |
.random_string(length) ⇒ Object
19 20 21 22 |
# File 'lib/tempmail_sdk/providers/duckmail.rb', line 19 def random_string(length) chars = ("a".."z").to_a + ("0".."9").to_a Array.new(length) { chars.sample }.join end |