Module: FFaker::Internet

Extended by:
Internet, ModuleUtils
Included in:
Internet, InternetSE
Defined in:
lib/ffaker/internet.rb

Constant Summary collapse

BYTE =
[*'0'..'255'].freeze
HOSTS =
%w[gmail.com yahoo.com hotmail.com].freeze
DISPOSABLE_HOSTS =
%w[mailinator.com suremail.info spamherelots.com binkmail.com safetymail.info].freeze
DOMAIN_SUFFIXES =
%w[co.uk com us ca biz info name].freeze
SAFE_DOMAIN_SUFFIXES =
%w[org com net].freeze
SLUG_DELIMITERS =
%w[- _ .].freeze
MAC_LIMIT =
281_474_976_710_655

Instance Method Summary collapse

Methods included from ModuleUtils

const_missing, k, underscore, unique

Methods included from RandomUtils

#fetch_sample, #rand, #shuffle

Instance Method Details

#disposable_email(name = nil) ⇒ Object

returns an email address of an online disposable email service (like tempinbox.com). you can really send an email to these addresses an access it by going to the service web pages.


22
23
24
# File 'lib/ffaker/internet.rb', line 22

def disposable_email(name = nil)
  [user_name(name), fetch_sample(DISPOSABLE_HOSTS)].join('@')
end

#domain_nameObject


48
49
50
# File 'lib/ffaker/internet.rb', line 48

def domain_name
  "#{domain_word}.#{domain_suffix}"
end

#domain_suffixObject


59
60
61
# File 'lib/ffaker/internet.rb', line 59

def domain_suffix
  fetch_sample(DOMAIN_SUFFIXES)
end

#domain_wordObject


52
53
54
55
56
57
# File 'lib/ffaker/internet.rb', line 52

def domain_word
  dw = Company.name.split(' ').first
  dw.gsub!(/\W/, '')
  dw.downcase!
  dw
end

#email(name = nil) ⇒ Object


16
17
18
# File 'lib/ffaker/internet.rb', line 16

def email(name = nil)
  [user_name(name), domain_name].join('@')
end

#free_email(name = nil) ⇒ Object


26
27
28
# File 'lib/ffaker/internet.rb', line 26

def free_email(name = nil)
  "#{user_name(name)}@#{fetch_sample(HOSTS)}"
end

#http_urlObject


67
68
69
# File 'lib/ffaker/internet.rb', line 67

def http_url
  uri('http')
end

#ip_v4_addressObject


71
72
73
# File 'lib/ffaker/internet.rb', line 71

def ip_v4_address
  (1..4).map { fetch_sample(BYTE) }.join('.')
end

#mac(delimiter = ':') ⇒ Object


87
88
89
# File 'lib/ffaker/internet.rb', line 87

def mac(delimiter = ':')
  rand(0...MAC_LIMIT).to_s(16).rjust(12, '0').scan(/.{2}/).join(delimiter)
end

#password(min_length = 8, max_length = 16) ⇒ Object


81
82
83
84
85
# File 'lib/ffaker/internet.rb', line 81

def password(min_length = 8, max_length = 16)
  length =
    min_length > max_length ? min_length : fetch_sample([*min_length..max_length])
  String.from_regexp(/\w{#{length}}/)
end

#safe_email(name = nil) ⇒ Object


30
31
32
# File 'lib/ffaker/internet.rb', line 30

def safe_email(name = nil)
  "#{user_name(name)}@example.#{fetch_sample(SAFE_DOMAIN_SUFFIXES)}"
end

#slug(words = nil, glue = nil) ⇒ Object


75
76
77
78
79
# File 'lib/ffaker/internet.rb', line 75

def slug(words = nil, glue = nil)
  words ||= Lorem.words(2).join(' ')
  glue ||= fetch_sample(SLUG_DELIMITERS)
  words.downcase.gsub(/[^a-z0-9]+/, glue)
end

#uri(protocol) ⇒ Object


63
64
65
# File 'lib/ffaker/internet.rb', line 63

def uri(protocol)
  "#{protocol}://#{domain_name}"
end

#user_name(name = nil) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ffaker/internet.rb', line 34

def user_name(name = nil)
  if name
    parts = shuffle(name.scan(/\w+/)).join(fetch_sample(%w[. _]))
    parts.downcase
  else
    case rand(0..1)
    when 0
      sanitize(Name.first_name)
    when 1
      [Name.first_name, Name.last_name].map { |n| sanitize(n) }.join(fetch_sample(%w[. _]))
    end
  end
end