Class: Mindee::Input::Source::UrlInputSource

Inherits:
Object
  • Object
show all
Defined in:
lib/mindee/input/sources/url_input_source.rb

Overview

Load a remote document from a file url.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ UrlInputSource

Returns a new instance of UrlInputSource.



15
16
17
18
19
# File 'lib/mindee/input/sources/url_input_source.rb', line 15

def initialize(url)
  raise 'URL must be HTTPS' unless url.start_with? 'https://'

  @url = url
end

Instance Attribute Details

#urlString (readonly)

Returns:

  • (String)


13
14
15
# File 'lib/mindee/input/sources/url_input_source.rb', line 13

def url
  @url
end

Instance Method Details

#as_local_input_source(filename: nil, username: nil, password: nil, token: nil, max_redirects: 3) ⇒ BytesInputSource

Downloads the file from the url, and returns a BytesInputSource wrapper object for it.

Parameters:

  • filename (String, nil) (defaults to: nil)

    Optional name to give to the file.

  • username (String, nil) (defaults to: nil)

    Optional username for authentication.

  • password (String, nil) (defaults to: nil)

    Optional password for authentication.

  • token (String, nil) (defaults to: nil)

    Optional token for JWT-based authentication.

  • max_redirects (Integer) (defaults to: 3)

    Maximum amount of redirects to follow.

Returns:



50
51
52
53
54
55
56
57
# File 'lib/mindee/input/sources/url_input_source.rb', line 50

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

#fetch_file_content(username: nil, password: nil, token: nil, max_redirects: 3) ⇒ String

Fetches the file content from the URL.

Parameters:

  • username (String, nil) (defaults to: nil)

    Optional username for authentication.

  • password (String, nil) (defaults to: nil)

    Optional password for authentication.

  • token (String, nil) (defaults to: nil)

    Optional token for JWT-based authentication.

  • max_redirects (Integer) (defaults to: 3)

    Maximum amount of redirects to follow.

Returns:

  • (String)

    The downloaded file content.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mindee/input/sources/url_input_source.rb', line 66

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

#save_to_file(path, filename: nil, username: nil, password: nil, token: nil, max_redirects: 3) ⇒ String

Downloads the file from the URL and saves it to the specified path.

Parameters:

  • path (String)

    Path to save the file to.

  • filename (String, nil) (defaults to: nil)

    Optional name to give to the file.

  • username (String, nil) (defaults to: nil)

    Optional username for authentication.

  • password (String, nil) (defaults to: nil)

    Optional password for authentication.

  • token (String, nil) (defaults to: nil)

    Optional token for JWT-based authentication.

  • max_redirects (Integer) (defaults to: 3)

    Maximum amount of redirects to follow.

Returns:

  • (String)

    The full path of the saved file.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mindee/input/sources/url_input_source.rb', line 30

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