Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 0 additions & 6 deletions bin/mindee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,6 @@
sync: false,
async: true,
},
"fr-carte-vitale" => {
description: "FR Carte Vitale",
doc_class: Mindee::Product::FR::CarteVitale::CarteVitaleV1,
sync: true,
async: false,
},
"fr-id-card" => {
description: "FR ID Card",
doc_class: Mindee::Product::FR::IdCard::IdCardV2,
Expand Down
19 changes: 0 additions & 19 deletions docs/code_samples/carte_vitale_v1.txt

This file was deleted.

108 changes: 108 additions & 0 deletions lib/mindee/input/sources/url_input_source.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# frozen_string_literal: true

require 'net/http'
require 'uri'
require 'fileutils'

module Mindee
module Input
module Source
Expand All @@ -13,6 +17,110 @@ def initialize(url)

@url = url
end

# Downloads the file from the URL and saves it to the specified path.
#
# @param path [String] Path to save the file to.
# @param filename [String, nil] Optional name to give to the file.
# @param username [String, nil] Optional username for authentication.
# @param password [String, nil] Optional password for authentication.
# @param token [String, nil] Optional token for JWT-based authentication.
# @param max_redirects [Integer] Maximum amount of redirects to follow.
# @return [String] The full path of the saved file.
def save_to_file(path, filename: nil, username: nil, password: nil, token: nil, max_redirects: 3)
response_body = fetch_file_content(username: username, password: password, token: token,
max_redirects: max_redirects)

filename = fill_filename(filename)

full_path = File.join(path.chomp('/'), filename)
File.write(full_path, response_body)

full_path
end

# Downloads the file from the url, and returns a BytesInputSource wrapper object for it.
#
# @param filename [String, nil] Optional name to give to the file.
# @param username [String, nil] Optional username for authentication.
# @param password [String, nil] Optional password for authentication.
# @param token [String, nil] Optional token for JWT-based authentication.
# @param max_redirects [Integer] Maximum amount of redirects to follow.
# @return [BytesInputSource] The full path of the saved file.
def as_local_input_source(filename: nil, username: nil, password: nil, token: nil, max_redirects: 3)
filename = fill_filename(filename)
response_body = fetch_file_content(username: username, password: password, token: token,
max_redirects: max_redirects)
bytes = StringIO.new(response_body)

BytesInputSource.new(bytes.read, filename)
end

# Fetches the file content from the URL.
#
# @param username [String, nil] Optional username for authentication.
# @param password [String, nil] Optional password for authentication.
# @param token [String, nil] Optional token for JWT-based authentication.
# @param max_redirects [Integer] Maximum amount of redirects to follow.
# @return [String] The downloaded file content.
def fetch_file_content(username: nil, password: nil, token: nil, max_redirects: 3)
uri = URI.parse(@url)
request = Net::HTTP::Get.new(uri)

request['Authorization'] = "Bearer #{token}" if token
request.basic_auth(username, password) if username && password

response = make_request(uri, request, max_redirects)
if response.code.to_i > 299
raise "Failed to download file: HTTP status code #{response.code}"
elsif response.code.to_i < 200
raise "Failed to download file: Invalid response code #{response.code}."
end

response.body
end

private

def extract_filename_from_url(uri)
filename = File.basename(uri.path)
filename.empty? ? '' : filename
end

def fill_filename(filename)
filename ||= extract_filename_from_url(URI.parse(@url))
if filename.empty? || File.extname(filename).empty?
filename = generate_file_name(extension: get_file_extension(filename))
end
filename
end

def make_request(uri, request, max_redirects)
Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
response = http.request(request)
if response.is_a?(Net::HTTPRedirection) && max_redirects.positive?
location = response['location']
raise 'No location in redirection header.' if location.nil?

new_uri = URI.parse(location)
request = Net::HTTP::Get.new(new_uri)
make_request(new_uri, request, max_redirects - 1)
else
response
end
end
end

def get_file_extension(filename)
ext = File.extname(filename)
ext.empty? ? nil : ext.downcase
end

def generate_file_name(extension: nil)
extension ||= '.tmp'
random_string = Array.new(8) { rand(36).to_s(36) }.join
"mindee_temp_#{Time.now.strftime('%Y-%m-%d_%H-%M-%S')}_#{random_string}#{extension}"
end
end
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/mindee/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
require_relative 'product/fr/bank_account_details/bank_account_details_v2'
require_relative 'product/fr/bank_statement/bank_statement_v1'
require_relative 'product/fr/carte_grise/carte_grise_v1'
require_relative 'product/fr/carte_vitale/carte_vitale_v1'
require_relative 'product/fr/id_card/id_card_v1'
require_relative 'product/fr/id_card/id_card_v2'
require_relative 'product/fr/energy_bill/energy_bill_v1'
Expand Down
41 changes: 0 additions & 41 deletions lib/mindee/product/fr/carte_vitale/carte_vitale_v1.rb

This file was deleted.

52 changes: 0 additions & 52 deletions lib/mindee/product/fr/carte_vitale/carte_vitale_v1_document.rb

This file was deleted.

34 changes: 0 additions & 34 deletions lib/mindee/product/fr/carte_vitale/carte_vitale_v1_page.rb

This file was deleted.

32 changes: 0 additions & 32 deletions spec/document/fr/carte_vitale_v1_spec.rb

This file was deleted.

12 changes: 0 additions & 12 deletions spec/input/sources_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,6 @@
end
end

context 'A remote url file' do
it 'should not send an invalid URL' do
expect do
Mindee::Input::Source::UrlInputSource.new('http://invalid-url')
end.to raise_error 'URL must be HTTPS'
end
it 'should send a valid URL' do
input = Mindee::Input::Source::UrlInputSource.new('https://platform.mindee.com')
expect(input.url).to eq('https://platform.mindee.com')
end
end

context 'A broken fixable PDF' do
mindee_client = Mindee::Client.new(api_key: 'invalid-api-key')
it 'Should not raise a mime error' do
Expand Down
18 changes: 18 additions & 0 deletions spec/input/url_input_source_integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require 'rspec'
require 'mindee'

describe Mindee::Input::Source::UrlInputSource do
it 'retrieves response from a remote file' do
api_key = ENV.fetch('MINDEE_API_KEY', nil)
client = Mindee::Client.new(api_key: api_key)
remote_input = Mindee::Input::Source::UrlInputSource.new('https://github.com/mindee/client-lib-test-data/blob/main/products/invoice_splitter/invoice_5p.pdf?raw=true')

local_input = remote_input.as_local_input_source
expect(local_input.filename).to eq('invoice_5p.pdf')

result = client.parse(local_input, Mindee::Product::Invoice::InvoiceV4)
expect(result.document.n_pages).to eq(5)
end
end
Loading
Loading