Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluate Javascript for Enrichments #92

Merged
merged 2 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/supplejack/extraction/enrichment_extraction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ def initialize(request, record, page = 1, extraction_folder = nil)
@extraction_folder = extraction_folder
end

def extract
::Retriable.retriable do
@document = if @extraction_definition.evaluate_javascript?
Extraction::JavascriptRequest.new(url:, params:).get
else
Extraction::Request.new(url:, params:, headers:, method: http_method).send(http_method)
end
end
rescue StandardError => e
::Sidekiq.logger.info "Extraction error: #{e}" if defined?(Sidekiq)
end

def valid?
url.exclude?('evaluation-error')
end
Expand Down
19 changes: 19 additions & 0 deletions spec/supplejack/extraction/enrichment_extraction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@
end
end

context 'when the extraction requires JavaScript' do
let(:ed) { create(:extraction_definition, :enrichment, destination:, extraction_jobs: [extraction_job], base_url: "file://#{Rails.root.join('spec/stub_responses')}", evaluate_javascript: true) }
let!(:parameter) { create(:parameter, content: "javascript_example.html", kind: 'slug', request: request_two, content_type: 'static') }

context 'when the extraction is successful' do
it 'evaluates the JavaScript and saves the HTML as a document' do
document = subject.extract

document_html = Nokogiri::HTML(document.body).xpath('//body').to_html
expect(document_html).to include('This heading is rendered with JavaScript')
end

it 'returns a successful status code' do
document = subject.extract
expect(document.status).to eq 200
end
end
end

context 'when record extraction fails' do
before do
subject
Expand Down
Loading