Class: NetworkUtils::UrlInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/network_utils/url_info.rb

Overview

Simple class to get URL info (validation/existance, headers, content-type) Allows to get all this stuff without actually downloading huge files like CSVs, images, videos, etc.

Instance Method Summary collapse

Constructor Details

#initialize(url, request_timeout = 10) ⇒ UrlInfo

Initialise a UrlInfo for a particular URL

Parameters:

  • url (String)

    the URL you want to get info about

  • request_timeout (Integer) (defaults to: 10)

    Max time to wait for headers from the server



23
24
25
26
# File 'lib/network_utils/url_info.rb', line 23

def initialize(url, request_timeout = 10)
  @url = url.dup.to_s.force_encoding('UTF-8')
  @request_timeout = request_timeout
end

Instance Method Details

#content_typeString

A shortcut method to get the Content-Type of the remote resource

Returns:

  • (String)

    remote resource Content-Type Header content



66
67
68
69
70
# File 'lib/network_utils/url_info.rb', line 66

def content_type
  headers&.fetch('content-type', nil)
         &.split(/,\s/)
         &.map { |ct| ct.split(/;\s/).first }
end

#headersHash?

A method to get the remote resource HTTP headers Caches the result and returns memoised version

Returns:

  • (Hash, nil)

    remote resource HTTP headers list or nil



76
77
78
# File 'lib/network_utils/url_info.rb', line 76

def headers
  @headers ||= request_headers
end

#is?(type) ⇒ Boolean

Check the Content-Type of the resource

Parameters:

  • type (String, Symbol, Array)

    the prefix (before “/”) or full Content-Type content

Returns:

  • (Boolean)

    true if Content-Type matches something from the types list



32
33
34
35
36
37
38
39
# File 'lib/network_utils/url_info.rb', line 32

def is?(type)
  return false if type.to_s.empty?

  expected_types = Array.wrap(type).map(&:to_s)
  content_type && expected_types.select do |t|
    content_type.select { |ct| ct.start_with?(t) }
  end.any?
end

#sizeInteger

A shortcut method to get the remote resource size

Returns:

  • (Integer)

    remote resource size (bytes), 0 if there’s nothing



59
60
61
# File 'lib/network_utils/url_info.rb', line 59

def size
  headers&.fetch('content-length', 0).to_i
end

#valid?Boolean

Check offline URL validity

Returns:

  • (Boolean)

    true if the URL is valid from the point of view of the standard



44
45
46
# File 'lib/network_utils/url_info.rb', line 44

def valid?
  @url.match?(UrlRegex.get(mode: :validation))
end

#valid_online?Boolean

Check online URL validity (& format validity as well)

Returns:

  • (Boolean)

    true if the URL is valid from the point of view of the standard & exists (has headers)



52
53
54
# File 'lib/network_utils/url_info.rb', line 52

def valid_online?
  valid? && headers
end