Skip to content

Commit

Permalink
Add types for email_address gem (#447)
Browse files Browse the repository at this point in the history
The email_address gem provides a ruby language library for working
with email addresses.

refs: https://github.com/afair/email_address

Co-authored-by: Masataka Pocke Kuwabara <[email protected]>
  • Loading branch information
tk0miya and pocke authored Oct 28, 2023
1 parent a9b2ab4 commit 6ff75d3
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@
[submodule "gems/browser/5.3/_src"]
path = gems/browser/5.3/_src
url = https://github.com/fnando/browser.git
[submodule "gems/email_address/0.2/_src"]
path = gems/email_address/0.2/_src
url = https://github.com/afair/email_address.git
[submodule "gems/json-jwt/1.16/_src"]
path = gems/json-jwt/1.16/_src
url = https://github.com/nov/json-jwt.git
21 changes: 21 additions & 0 deletions gems/email_address/0.2/_scripts/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

# Exit command with non-zero status code, Output logs of every command executed, Treat unset variables as an error when substituting.
set -eou pipefail
# Internal Field Separator - Linux shell variable
IFS=$'\n\t'
# Print shell input lines
set -v

# Set RBS_DIR variable to change directory to execute type checks using `steep check`
RBS_DIR=$(cd $(dirname $0)/..; pwd)
# Set REPO_DIR variable to validate RBS files added to the corresponding folder
REPO_DIR=$(cd $(dirname $0)/../../..; pwd)
# Validate RBS files, using the bundler environment present
bundle exec rbs --repo $REPO_DIR -r email_address:0.2 validate --silent

cd ${RBS_DIR}/_test
# Run type checks
bundle exec steep check

$(git rev-parse --show-toplevel)/bin/check-untyped-call.rb
1 change: 1 addition & 0 deletions gems/email_address/0.2/_src
Submodule _src added at b626ac
11 changes: 11 additions & 0 deletions gems/email_address/0.2/_test/Steepfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
D = Steep::Diagnostic

target :test do
check "."
signature "."

repo_path "../../../"
library "email_address"

configure_code_diagnostics(D::Ruby.all_error)
end
38 changes: 38 additions & 0 deletions gems/email_address/0.2/_test/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Write Ruby code to test the RBS.
# It is type checked by `steep check` command.

require "email_address"

address = "[email protected]"
EmailAddress.valid?(address) #=> true
EmailAddress.normal(address) #=> "[email protected]"
EmailAddress.canonical(address) #=> "[email protected]"
EmailAddress.reference(address) #=> "c5be3597c391169a5ad2870f9ca51901"
EmailAddress.redact(address) #=> "{bea3f3560a757f8142d38d212a931237b218eb5e}@gmail.com"
EmailAddress.munge(address) #=> "cl*****@gm*****"
EmailAddress.matches?(address, 'google') #=> 'google' (true)
EmailAddress.error("#[email protected]") #=> "Invalid Mailbox"

email = EmailAddress.new(address) #=> #<EmailAddress::Address:0x007fe6ee150540 ...>
email.normal #=> "[email protected]"
email.canonical #=> "[email protected]"
email.original #=> "[email protected]"
email.valid? #=> true

email.redact #=> "{bea3f3560a757f8142d38d212a931237b218eb5e}@gmail.com"
email.sha1 #=> "bea3f3560a757f8142d38d212a931237b218eb5e"
email.sha256 #=> "9e2a0270f2d6778e5f647fc9eaf6992705ca183c23d1ed1166586fd54e859f75"
email.md5 #=> "c5be3597c391169a5ad2870f9ca51901"
email.host_name #=> "gmail.com"
email.provider #=> :google
email.mailbox #=> "clark.kent"
email.tag #=> "scoops"

exchanger = email.host.exchangers.first or raise
exchanger[:ip] #=> "2a00:1450:400b:c02::1a"
email.host.txt_hash #=> {:v=>"spf1", :redirect=>"\_spf.google.com"}

EmailAddress.normal("HIRO@こんにちは世界.com")
#=> "[email protected]"
EmailAddress.normal("[email protected]", host_encoding: :unicode)
#=> "hiro@こんにちは世界.com"
74 changes: 74 additions & 0 deletions gems/email_address/0.2/email_address.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class EmailAddress
class Address
include Rewriter

attr_accessor original: String
attr_accessor local: Local
attr_accessor host: Host
attr_accessor config: Hash[untyped, untyped]
attr_accessor reason: untyped
attr_accessor locale: String
attr_reader error_message: String?

def initialize: (String email_address, ?Hash[untyped, untyped] config, ?String locale) -> Address
def mailbox: () -> String
def tag: () -> String?
def host_name: () -> String?
def provider: () -> Symbol?

def normal: () -> String
alias to_s normal

def canonical: () -> String
def canonical?: () -> bool
def base: () -> String
def redact: () -> String
def redacted?: () -> bool
def munge: () -> String

def reference: (?Symbol form) -> String
alias md5 reference

def sha1: (?Symbol form) -> String
def sha256: (?Symbol form) -> String

def matches?: (*String | Array[String] rules) -> bool
def valid?: (?Hash[untyped, untyped] options) -> bool
def error: () -> String?
end

class Config
end

class Exchanger
type exchange = { host: String, ip: String, priority: Integer }
include Enumerable[exchange]

def each: () { (exchange) -> void } -> void
end

class Local
end

class Host
def valid?: (?Hash[untyped, untyped] rules) -> bool
def exchangers: () -> Exchanger
def txt_hash: (?String alternate_host) -> Hash[Symbol, String]
end

module Rewriter
def srs: (String sending_domain, ?Hash[untyped, untyped] options) ? { (String) -> String } -> String
end

def self.valid?: (String email_address, ?Hash[untyped, untyped] config, ?String local) -> bool
def self.error: (String email_address, ?Hash[untyped, untyped] config, ?String local) -> String?
def self.normal: (String email_address, ?Hash[untyped, untyped] config, ?String local) -> String
def self.redact: (String email_address, ?Hash[untyped, untyped] config, ?String local) -> String
def self.munge: (String email_address, ?Hash[untyped, untyped] config, ?String local) -> String
def self.canonical: (String email_address, ?Hash[untyped, untyped] config, ?String local) -> String
def self.reference: (String email_address, ?Hash[untyped, untyped] config, ?String local) -> String
def self.base: (String email_address, ?Hash[untyped, untyped] config, ?String local) -> String

def self.new: (String email_address, ?Hash[untyped, untyped] config, ?String locale) -> Address
def self.matches?: (String email_address, String | Array[String] rules, ?Hash[untyped, untyped] config, ?String locale) -> Address
end

0 comments on commit 6ff75d3

Please sign in to comment.