Module: TempmailSdk::Providers::M2u
- Defined in:
- lib/tempmail_sdk/providers/m2u.rb
Overview
m2u.io 渠道实现
Constant Summary collapse
- CHANNEL =
"m2u"- API_BASE =
"https://api.m2u.io"- 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
- .fetch_detail(token, view_token, message_id) ⇒ Object
- .flatten_message(raw, recipient) ⇒ Object
- .generate_email ⇒ EmailInfo
- .get_emails(token, email) ⇒ Array<Email>
- .pack_token(token, view_token) ⇒ Object
- .unpack_token(token) ⇒ Object
Class Method Details
.fetch_detail(token, view_token, message_id) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/tempmail_sdk/providers/m2u.rb', line 83 def fetch_detail(token, view_token, ) resp = Http.get( "#{API_BASE}/v1/mailboxes/#{URI.encode_www_form_component(token)}/messages/#{URI.encode_www_form_component()}?view=#{URI.encode_www_form_component(view_token)}", headers: HEADERS, timeout: 15 ) return nil unless resp.ok? data = resp.json return nil unless data.is_a?(Hash) detail = data["message"] detail.is_a?(Hash) ? detail : nil rescue StandardError nil end |
.flatten_message(raw, recipient) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/tempmail_sdk/providers/m2u.rb', line 40 def (raw, recipient) { "id" => (raw["id"] || raw["message_id"]).to_s, "from" => (raw["from_addr"] || raw["from_address"] || raw["from"]).to_s, "to" => recipient, "subject" => (raw["subject"] || "").to_s, "text" => (raw["text_body"] || raw["body_text"] || raw["text"]).to_s, "html" => (raw["html_body"] || raw["body_html"] || raw["html"]).to_s, "date" => (raw["received_at"] || raw["created_at"]).to_s, "is_read" => raw["is_read"] || raw["isRead"] || raw["seen"] || false, "attachments" => raw["attachments"].is_a?(Array) ? raw["attachments"] : [] } end |
.generate_email ⇒ EmailInfo
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/m2u.rb', line 55 def generate_email resp = Http.post( "#{API_BASE}/v1/mailboxes/auto", headers: HEADERS.merge("Content-Type" => "application/json"), json: {}, timeout: 15 ) resp.raise_for_status data = resp.json mailbox = data.is_a?(Hash) ? data["mailbox"] : nil raise "m2u: invalid mailbox response" unless mailbox.is_a?(Hash) local_part = mailbox["local_part"].to_s.strip domain = mailbox["domain"].to_s.strip token = mailbox["token"].to_s.strip view_token = mailbox["view_token"].to_s.strip email = !local_part.empty? && !domain.empty? ? "#{local_part}@#{domain}" : "" raise "m2u: invalid mailbox response" if email.empty? || token.empty? || view_token.empty? EmailInfo.new( channel: CHANNEL, email: email, token: pack_token(token, view_token), expires_at: mailbox["expires_at"], created_at: mailbox["created_at"] ) end |
.get_emails(token, email) ⇒ Array<Email>
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/tempmail_sdk/providers/m2u.rb', line 102 def get_emails(token, email) mailbox_token, view_token = unpack_token(token) address = email.to_s.strip raise "m2u: missing token" if mailbox_token.empty? raise "m2u: missing view token" if view_token.empty? raise "m2u: empty email" if address.empty? resp = Http.get( "#{API_BASE}/v1/mailboxes/#{URI.encode_www_form_component(mailbox_token)}/messages?view=#{URI.encode_www_form_component(view_token)}", headers: HEADERS, timeout: 15 ) resp.raise_for_status data = resp.json rows = data.is_a?(Hash) ? (data["messages"] || []) : [] return [] unless rows.is_a?(Array) rows.select { |r| r.is_a?(Hash) }.map do |item| = (item["id"] || item["message_id"]).to_s.strip detail = .empty? ? nil : fetch_detail(mailbox_token, view_token, ) Normalize.normalize_email((detail || item, address), address) end end |
.pack_token(token, view_token) ⇒ Object
19 20 21 |
# File 'lib/tempmail_sdk/providers/m2u.rb', line 19 def pack_token(token, view_token) JSON.generate({ "token" => token, "viewToken" => view_token }) end |
.unpack_token(token) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/tempmail_sdk/providers/m2u.rb', line 23 def unpack_token(token) value = token.to_s.strip return ["", ""] if value.empty? if value.start_with?("{") begin data = JSON.parse(value) if data.is_a?(Hash) return [data["token"].to_s.strip, data["viewToken"].to_s.strip] end rescue StandardError return ["", ""] end end [value, ""] end |