PageRenderTime 64ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/FakerSharp/Internet.cs

https://github.com/officedrop/FakerSharp
C# | 80 lines | 69 code | 11 blank | 0 comment | 3 complexity | 8b1bc7ea4d35d3adb50ab52c930a5ee3 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace FakerSharp
  7. {
  8. public class Internet : Base<Internet>
  9. {
  10. private static Converter<string, string> getInternetName = (str) => new Regex(@"\W").Replace(str, "").ToLower();
  11. public static string GetEmail() { return GetEmail(null); }
  12. public static string GetEmail(string name)
  13. {
  14. return string.Join("@", GetUserName(name), DomainName);
  15. }
  16. public static string GetFreeEmail() { return GetFreeEmail(null); }
  17. public static string GetFreeEmail(string name)
  18. {
  19. return string.Join("@", GetUserName(name), TypeFetch("FreeEmail"));
  20. }
  21. public static string GetUserName() { return GetUserName(null); }
  22. public static string GetUserName(string name)
  23. {
  24. string[] separators = new[] { ".", " ", "_" };
  25. if (name != null)
  26. {
  27. var parts = from part in new Regex(@"(\w+)").Split(name)
  28. where !string.IsNullOrWhiteSpace(part)
  29. select part;
  30. return string.Join(separators.Pick(), parts).ToLower();
  31. }
  32. return new StringGenerator[]{
  33. () => getInternetName(Name.FirstName),
  34. () => string.Join(separators.Pick(), new[]{ Name.FirstName, Name.LastName }.Select(n => getInternetName(n)))
  35. }.Pick()();
  36. }
  37. public static string DomainName
  38. {
  39. get { return string.Join(".", DomainWord, DomainSuffix); }
  40. }
  41. public static string DomainWord
  42. {
  43. get { return getInternetName(Company.Name.Split(' ').First()); }
  44. }
  45. public static string DomainSuffix
  46. {
  47. get { return TypeFetch("DomainSuffix"); }
  48. }
  49. public static string IPv4Address
  50. {
  51. get
  52. {
  53. int max = 255;
  54. return string.Join(".", random.Next(2, max), random.Next(2, max), random.Next(2, max), random.Next(2, max));
  55. }
  56. }
  57. public static string IPv6Address
  58. {
  59. get
  60. {
  61. string[] container = new string[8];
  62. int max = 65535;
  63. for (int i = 0; i < container.Length; i++)
  64. container[i] = random.Next(max).ToString("X");
  65. return string.Join(":", container);
  66. }
  67. }
  68. }
  69. }