PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Faker/Internet.cs

http://github.com/slashdotdash/faker-cs
C# | 95 lines | 83 code | 12 blank | 0 comment | 0 complexity | 56f064f7abeb8ffd4fbab381302aa92f MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using Faker.Extensions;
  6. namespace Faker
  7. {
  8. public static class Internet
  9. {
  10. public static string Email()
  11. {
  12. return String.Format("{0}@{1}", UserName(), DomainName());
  13. }
  14. public static string Email(string name)
  15. {
  16. return String.Format("{0}@{1}", UserName(name), DomainName());
  17. }
  18. public static string FreeEmail()
  19. {
  20. return String.Format("{0}@{1}", UserName(), Resources.Internet.FreeMail.Split(Config.Separator).Random());
  21. }
  22. public static string UserName()
  23. {
  24. return _userNameFormats.Random();
  25. }
  26. public static string UserName(string name)
  27. {
  28. return Regex.Replace(name, @"[^\w]+", new MatchEvaluator(x => new [] { ".", "_" }.Random()), RegexOptions.Compiled).ToLowerInvariant();
  29. }
  30. public static string DomainName()
  31. {
  32. return String.Format("{0}.{1}", DomainWord(), DomainSuffix());
  33. }
  34. public static string DomainWord()
  35. {
  36. return Company.Name().Split(' ').First().AlphanumericOnly().ToLowerInvariant();
  37. }
  38. public static string DomainSuffix()
  39. {
  40. return Resources.Internet.DomainSuffix.Split(Config.Separator).Random();
  41. }
  42. public static string IPv4Address()
  43. {
  44. Random random = new Random();
  45. int min = 2;
  46. int max = 255;
  47. string[] parts = new string[] {
  48. random.Next(min, max).ToString(),
  49. random.Next(min, max).ToString(),
  50. random.Next(min, max).ToString(),
  51. random.Next(min, max).ToString(),
  52. };
  53. return String.Join(".", parts);
  54. }
  55. public static string IPv6Address()
  56. {
  57. Random random = new Random();
  58. int min = 0;
  59. int max = 65536;
  60. string[] parts = new string[] {
  61. random.Next(min, max).ToString("x"),
  62. random.Next(min, max).ToString("x"),
  63. random.Next(min, max).ToString("x"),
  64. random.Next(min, max).ToString("x"),
  65. random.Next(min, max).ToString("x"),
  66. random.Next(min, max).ToString("x"),
  67. random.Next(min, max).ToString("x"),
  68. random.Next(min, max).ToString("x"),
  69. };
  70. return String.Join(":", parts);
  71. }
  72. public static string Url()
  73. {
  74. return String.Format("http://{0}/{1}", DomainName(), UserName());
  75. }
  76. private static readonly IEnumerable<Func<string>> _userNameFormats = new List<Func<string>>
  77. {
  78. () => Name.First().AlphanumericOnly().ToLowerInvariant(),
  79. () => string.Format("{0}{1}{2}", Name.First().AlphanumericOnly(),
  80. new [] { ".", "_" }.Random(), Name.Last().AlphanumericOnly()).ToLowerInvariant()
  81. };
  82. }
  83. }