Skip to content

Commit fd76541

Browse files
committed
Make sure methods get expected values and fix tests
This is still kind of messy, I just wanted to make sure methods get the expected values for now. But I should clean up the fallback method, and the parse method.
1 parent d0b9a94 commit fd76541

File tree

7 files changed

+22
-17
lines changed

7 files changed

+22
-17
lines changed

app/models/media.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,15 @@ def process_and_return_json(options = {})
8282
cache = Pender::Store.current
8383
if options.delete(:force) || cache.read(id, :json).nil?
8484
handle_exceptions(self, StandardError) { self.parse }
85+
clean_data = clean_json(self.data)
86+
self.fallback
87+
8588
if data[:error].blank?
86-
cache.write(id, :json, clean_json(data))
89+
cache.write(id, :json, clean_data)
8790
end
8891
self.upload_images
8992
end
90-
self.fallback
93+
9194
archive_if_conditions_are_met(options, id, cache)
9295
parser_requests_metrics
9396
cache.read(id, :json) || clean_json(data)
@@ -207,8 +210,14 @@ def parse
207210
self.doc,
208211
self.original_url,
209212
self.data.dig('raw', 'json+ld')
213+
)
214+
self.data.deep_merge!(
215+
{
216+
provider: self.provider,
217+
type: self.type,
218+
url: self.parser.url
219+
}.merge(parsed_data)
210220
)
211-
self.data.deep_merge!(parsed_data)
212221
self.url = self.parser.url
213222
self.get_oembed_data
214223
parsed = true

test/helpers/medias_test.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ def setup
2626
test "should not crash if jsonld content is null" do
2727
null_content = '<script type="application/ld+json">null</script>'
2828
m = create_media url: 'https://www.facebook.com/dina.samak/posts/10153679232246949'
29-
m.data = Media.minimal_data(m)
3029
Media.any_instance.stubs(:doc).returns(Nokogiri::HTML(null_content))
3130
assert_nothing_raised do
3231
get_jsonld_data(m)
@@ -39,7 +38,6 @@ def setup
3938
doc = ''
4039
File.open('test/data/page-with-json-ld.html') { |f| doc = f.read }
4140
Media.any_instance.stubs(:doc).returns(Nokogiri::HTML(doc))
42-
m.data = Media.minimal_data(m)
4341
assert_nothing_raised do
4442
m.get_jsonld_data(m)
4543
end

test/integration/parser/facebook_item_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ class FacebookItemIntegrationTest < ActiveSupport::TestCase
3636
assert_equal 'item', data['type']
3737
assert_equal '111111111111111_1111111111111111', data['external_id']
3838
assert_match(/facebook.com\/111111111111111\/posts\/1111111111111111/, data['title'])
39+
assert_match(/facebook.com\/111111111111111\/posts\/1111111111111111/, data['description'])
3940
assert_equal '', data['username']
4041
assert_equal '', data['author_name']
4142
assert_equal '', data['author_picture']
4243
assert_equal '', data['author_url']
43-
assert_equal '', data['description']
4444
assert_equal '', data['picture']
4545
assert_equal '', data['published_at']
4646
end

test/integration/parser/facebook_profile_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,11 @@ class FacebookProfileIntegrationTest < ActiveSupport::TestCase
3737
data = media.process_and_return_json
3838

3939
assert_match(/facebook.com\/pages\/fakepage\/1111111111111/, data['title'])
40+
assert_match(/facebook.com\/pages\/fakepage\/1111111111111/, data['description'])
4041
assert_equal 'fakepage', data['username']
41-
assert data['description'].blank?
4242
assert data['picture'].blank?
4343
assert data['published_at'].blank?
4444
assert_equal 'facebook', data['provider']
4545
assert_equal 'profile', data['type']
4646
end
4747
end
48-

test/integration/parser/kwai_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class KwaiIntegrationTest < ActiveSupport::TestCase
1818
data = m.process_and_return_json
1919

2020
assert_equal 'https://kwai-video.com/p/aaaaaaaa', data['title']
21-
assert data['description'].blank?
21+
assert_equal 'https://kwai-video.com/p/aaaaaaaa', data['description']
2222
assert data['username'].blank?
2323
assert_equal 'kwai', data['provider']
2424
assert_equal 'item', data['type']

test/models/media_test.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class MediaTest < ActiveSupport::TestCase
9494
m = create_media url: 'http://xkcd.com/448/'
9595
data = m.process_and_return_json
9696
assert_match /Good Morning/, data['title']
97-
assert_equal '', data['description']
97+
assert_equal 'https://xkcd.com/448/', data['description']
9898
assert_equal '', data['published_at']
9999
assert_equal '', data['username']
100100
assert_match 'https://xkcd.com', data['author_url']
@@ -226,12 +226,11 @@ class MediaTest < ActiveSupport::TestCase
226226
doc = ''
227227
File.open('test/data/page-with-json-ld.html') { |f| doc = f.read }
228228
Media.any_instance.stubs(:doc).returns(Nokogiri::HTML(doc))
229-
m.data = Media.minimal_data(m)
230-
m.get_jsonld_data(m)
229+
data = m.process_and_return_json
231230

232-
assert !m.data['raw']['json+ld'].empty?
233-
assert m.data['raw']['json+ld'].is_a? Array
234-
assert m.data['raw']['json+ld'].first.is_a? Hash
231+
assert_not_empty data['raw']['json+ld']
232+
assert_kind_of Array, data['raw']['json+ld']
233+
assert_kind_of Hash, data['raw']['json+ld'].first
235234
end
236235

237236
test "should handle errors when call parse on each parser" do

test/models/parser/facebook_item_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ def apify_content_not_available_response
454454
assert_match "<div class=\"fb-post\" data-href=\"#{url}\"></div>", data['html']
455455
end
456456

457-
test "should return html and empty description when FB url is private" do
457+
test "should return html and description with the original url when FB url is private" do
458458
url = 'https://www.facebook.com/caiosba/posts/1913749825339929'
459459

460460
WebMock.stub_request(:post, /api\.apify\.com\/v2\/acts\/apify/).to_return(status: 200, body: apify_response_not_found)
@@ -471,7 +471,7 @@ def apify_content_not_available_response
471471
media = Media.new(url: url)
472472
data = media.process_and_return_json
473473

474-
assert data[:description].empty?
474+
assert_equal url, data[:description]
475475
assert_match "<div class=\"fb-post\" data-href=\"https://www.facebook.com/caiosba/posts/1913749825339929\">", data['html']
476476
end
477477

0 commit comments

Comments
 (0)