Skip to content

Commit 04d5876

Browse files
committed
WIP: Make sure we are passing all the expected values forward
1 parent d0b9a94 commit 04d5876

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

app/models/media.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,14 @@ 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+
self.fallback
86+
8587
if data[:error].blank?
8688
cache.write(id, :json, clean_json(data))
8789
end
8890
self.upload_images
8991
end
90-
self.fallback
92+
9193
archive_if_conditions_are_met(options, id, cache)
9294
parser_requests_metrics
9395
cache.read(id, :json) || clean_json(data)
@@ -201,15 +203,23 @@ def parse
201203

202204
PARSERS.each do |parser|
203205
if parseable = parser.match?(self.url)
206+
#byebug
204207
self.parser = parseable
205208
self.provider, self.type = self.parser.type.split('_')
206209
parsed_data = self.parser.parse_data(
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
222+
#byebug
213223
self.get_oembed_data
214224
parsed = true
215225
Rails.logger.info level: 'INFO', message: '[Parser] Parsing new URL', url: self.url, parser: self.parser.to_s, provider: self.provider, type: self.type

test/models/media_test.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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)