You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

parser_spec.rb 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. require_relative '../lib/parser'
  2. RSpec.describe Parser do
  3. subject { Parser.new }
  4. describe '#threads' do
  5. let(:html) do
  6. Nokogiri::HTML(<<~HTML)
  7. <div class="even" id="thread_12345">
  8. <ul class="list read">
  9. <li class="member">
  10. <span>Thread By: </span>
  11. <a href="/member/view/creator1/" class="memberlink">creator1</a>
  12. </li>
  13. <li class="subject">
  14. <span>Subject: </span>
  15. <a href="/thread/view/12345/&p=999">
  16. <strong>Sticky:</sticky> Thread title 1
  17. </a>
  18. </li>
  19. <li class="posts"><span>Posts: </span>999</li>
  20. <li class="lastpost">
  21. <span>Last Post By:</span>
  22. <a href="/member/view/lastposter1/" class="memberlink">lastposter1</a> on Fri&nbsp;Apr&nbsp;10&nbsp;2020&nbsp;01:23&nbsp;am</li>
  23. </ul>
  24. </div>
  25. <div class="even" id="thread_123456">
  26. <ul class="list read">
  27. <li class="member">
  28. <span>Thread By: </span>
  29. <a href="/member/view/creator2/" class="memberlink">creator2</a>
  30. </li>
  31. <li class="subject">
  32. <span>Subject: </span>
  33. <a href="/thread/view/123456/&p=999">Thread title 2</a>
  34. </li>
  35. <li class="posts"><span>Posts: </span>999</li>
  36. <li class="lastpost">
  37. <span>Last Post By:</span>
  38. <a href="/member/view/lastposter2/" class="memberlink">lastposter2</a> on Fri&nbsp;Apr&nbsp;10&nbsp;2020&nbsp;01:23&nbsp;am</li>
  39. </ul>
  40. </div>
  41. HTML
  42. end
  43. it 'parses threads' do
  44. expect(subject.threads(html)).to eq([
  45. {remote_id: '12345', title: 'Sticky: Thread title 1', creator: 'creator1', is_sticky: true},
  46. {remote_id: '123456', title: 'Thread title 2', creator: 'creator2', is_sticky: false},
  47. ])
  48. end
  49. end
  50. describe '#posts' do
  51. let(:html) do
  52. Nokogiri::HTML(<<~HTML)
  53. <div>
  54. <div class="post">
  55. <ul class="view" id="post_69">
  56. <li class="info">
  57. <div class="postinfo">
  58. <a class="memberlink" href="/member/view/User1">User1</a>
  59. posted this October 26th, 2021 @ 12:34:56 am
  60. </div>
  61. </li>
  62. <li class="postbody">
  63. This is the body of the first post
  64. </li>
  65. </ul>
  66. </div>
  67. <div class="post">
  68. <ul class="view" id="post_420">
  69. <li class="info">
  70. <div class="postinfo">
  71. <a class="memberlink" href="/member/view/User2">User2</a>
  72. posted this October 27th, 2021 @ 12:34:56 am
  73. </div>
  74. </li>
  75. <li class="postbody">
  76. This is the body of the second post
  77. </li>
  78. </ul>
  79. </div>
  80. </div>
  81. HTML
  82. end
  83. it 'parses posts' do
  84. expect(subject.posts({ id: 666 }, html)).to match_array([
  85. {
  86. remote_id: 69,
  87. creator: 'User1',
  88. thread_id: 666,
  89. created_at: Time.new(2021, 10, 26, 0, 34, 56),
  90. body: 'This is the body of the first post'
  91. },
  92. {
  93. remote_id: 420,
  94. creator: 'User2',
  95. thread_id: 666,
  96. created_at: Time.new(2021, 10, 27, 0, 34, 56),
  97. body: 'This is the body of the second post'
  98. }
  99. ])
  100. end
  101. end
  102. describe '#thread_created_at' do
  103. let(:html) do
  104. Nokogiri::HTML(<<~HTML)
  105. <div class="postinfo">
  106. <a class="memberlink" href="/member/view/User1">User1</a>
  107. posted this October 27th, 2021 @ 12:34:56 am
  108. </div>
  109. HTML
  110. end
  111. it 'parses the timestamp of the first post' do
  112. expect(subject.thread_created_at(html))
  113. .to eq(Time.new(2021, 10, 27, 00, 34, 56))
  114. end
  115. end
  116. end