Module: TempmailSdk::Providers::ShittyEmail
- Defined in:
- lib/tempmail_sdk/providers/shitty_email.rb
Overview
shitty.email 渠道 — https://shitty.email
Constant Summary collapse
- CHANNEL =
"shitty-email"- API_BASE =
"https://shitty.email/api"- HEADERS =
{ "Accept" => "application/json", "User-Agent" => "Mozilla/5.0" }.freeze
Class Method Summary collapse
-
.generate_email ⇒ EmailInfo
创建临时邮箱.
-
.get_emails(token, email) ⇒ Array<Email>
获取邮件列表.
- .headers_with_token(token = nil) ⇒ Object
Class Method Details
.generate_email ⇒ EmailInfo
创建临时邮箱
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/tempmail_sdk/providers/shitty_email.rb', line 23 def generate_email resp = Http.post("#{API_BASE}/inbox", headers: HEADERS, timeout: 15) resp.raise_for_status data = resp.json raise "shitty-email: 响应格式无效" unless data.is_a?(Hash) email = (data["email"] || "").strip token = (data["token"] || "").strip raise "shitty-email: 创建邮箱响应无效" if email.empty? || !email.include?("@") || token.empty? EmailInfo.new(channel: CHANNEL, email: email, token: token, expires_at: data["expiresAt"]) end |
.get_emails(token, email) ⇒ Array<Email>
获取邮件列表
40 41 42 43 44 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 73 74 75 76 77 78 79 80 81 |
# File 'lib/tempmail_sdk/providers/shitty_email.rb', line 40 def get_emails(token, email) session_token = (token || "").strip address = (email || "").strip raise "shitty-email: token 为空" if session_token.empty? raise "shitty-email: 邮箱地址为空" if address.empty? resp = Http.get("#{API_BASE}/inbox", headers: headers_with_token(session_token), timeout: 15) resp.raise_for_status data = resp.json return [] unless data.is_a?(Hash) rows = data["emails"] return [] unless rows.is_a?(Array) rows.filter_map do |item| next unless item.is_a?(Hash) = (item["id"] || "").to_s.strip detail = nil unless .empty? begin dr = Http.get("#{API_BASE}/email/#{URI.encode_www_form_component()}", headers: headers_with_token(session_token), timeout: 15) detail = dr.json if dr.ok? rescue StandardError # 单封邮件获取失败不影响其他 end end source = (detail.is_a?(Hash) ? detail : item) raw = { "id" => source["id"], "from" => source["from"], "to" => source["to"] || address, "subject" => source["subject"], "text" => source["text"], "html" => source["html"], "date" => source["date"] } Normalize.normalize_email(raw, address) end end |
.headers_with_token(token = nil) ⇒ Object
15 16 17 18 19 |
# File 'lib/tempmail_sdk/providers/shitty_email.rb', line 15 def headers_with_token(token = nil) h = HEADERS.dup h["X-Session-Token"] = token if token && !token.empty? h end |