From 3bef6c0cbefc05ad9b920b0c4293faaa2bb8fa9f Mon Sep 17 00:00:00 2001 From: Manu Vasconcelos <87862340+vasconsaurus@users.noreply.github.com> Date: Wed, 19 Jul 2023 16:52:58 -0300 Subject: [PATCH] 3019 - add safe operator to avoid NoMethodError (kwai) (#364) * add safe operator * removed test that checks for NoMethodError This test only worked when the two selectors we look for don't exist, but sometimes a link might not have those specific selectors and still have relevant information to be returned. By adding a safe operator we are not getting the NoMethodError anymore, and are returning relevant data. But this test stops making sense. We thought about adding tests to specific links or errors, but in the end we think those are not necessary. --- app/models/parser/kwai_item.rb | 2 +- test/models/parser/kwai_test.rb | 16 ---------------- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/app/models/parser/kwai_item.rb b/app/models/parser/kwai_item.rb index 6153444b..23188b4c 100644 --- a/app/models/parser/kwai_item.rb +++ b/app/models/parser/kwai_item.rb @@ -30,7 +30,7 @@ def parse_data_for_parser(doc, _original_url, _jsonld_array) end def get_kwai_text_from_tag(doc, selector) - doc&.at_css(selector)&.text&.to_s.strip + doc&.at_css(selector)&.text&.to_s&.strip end end end diff --git a/test/models/parser/kwai_test.rb b/test/models/parser/kwai_test.rb index 08289d64..6d49ff56 100644 --- a/test/models/parser/kwai_test.rb +++ b/test/models/parser/kwai_test.rb @@ -2,8 +2,6 @@ class KwaiIntegrationTest < ActiveSupport::TestCase test "should parse Kwai URL" do - skip "Kwai parsing is not currently working; pending until we look into" - m = create_media url: 'https://s.kw.ai/p/1mCb9SSh' data = m.as_json assert_equal 'Reginaldo Silva2871', data['username'] @@ -47,18 +45,4 @@ def teardown assert_equal 'Reginaldo Silva2871', data[:author_name] assert_equal 'Reginaldo Silva2871', data[:username] end - - test "returns a hash with error message and sends error to Errbit if there is an error parsing" do - mocked_sentry = MiniTest::Mock.new - mocked_sentry.expect :call, :return_value, [StandardError, Hash] - - data = nil - empty_doc = Nokogiri::HTML('') - PenderSentry.stub(:notify, mocked_sentry) do - data = Parser::KwaiItem.new('https://s.kw.ai/p/example').parse_data(empty_doc) - end - mocked_sentry.verify - assert_equal 5, data[:error][:code] - assert_match /NoMethodError/, data[:error][:message] - end end