Module: Html2rss::Utils

Defined in:
lib/html2rss/utils.rb

Overview

The collecting tank for utility methods.

Class Method Summary collapse

Class Method Details

.build_absolute_url_from_relative(url, base_url) ⇒ Addressable::URI

Parameters:

  • url (String, Addressable::URI)
  • base_url (String)

Returns:

  • (Addressable::URI)


20
21
22
23
24
25
26
27
28
29
# File 'lib/html2rss/utils.rb', line 20

def self.build_absolute_url_from_relative(url, base_url)
  url = Addressable::URI.parse(url.to_s) unless url.is_a?(Addressable::URI)

  return url if url.absolute?

  base_uri = Addressable::URI.parse(base_url)
  base_uri.path = '/' if base_uri.path.empty?

  base_uri.join(url).normalize
end

.build_regexp_from_string(string) ⇒ Regexp

Parses the given String and builds a Regexp out of it.

It will remove one pair of surrounding slashes (‘/’) from the String to maintain backwards compatibility before building the Regexp.

Parameters:

  • string (String)

Returns:

  • (Regexp)

Raises:

  • (ArgumentError)


97
98
99
100
101
102
# File 'lib/html2rss/utils.rb', line 97

def self.build_regexp_from_string(string)
  raise ArgumentError, 'must be a string!' unless string.is_a?(String)

  string = string[1..-2] if string.start_with?('/') && string.end_with?('/')
  Regexp::Parser.parse(string, options: ::Regexp::EXTENDED | ::Regexp::IGNORECASE).to_re
end

.guess_content_type_from_url(url) ⇒ String

Guesses the content type based on the file extension of the URL.

Parameters:

  • url (String, Addressable::URI)

Returns:

  • (String)

    guessed content type, defaults to ‘application/octet-stream’



109
110
111
112
113
114
# File 'lib/html2rss/utils.rb', line 109

def self.guess_content_type_from_url(url)
  url = url.to_s.split('?').first

  content_type = MIME::Types.type_for(File.extname(url).delete('.'))
  content_type.first&.to_s || 'application/octet-stream'
end

.request_url(url, headers: {}) ⇒ Faraday::Response

Returns body of the HTTP response.

Parameters:

  • url (String, Addressable::URI)
  • headers (Hash) (defaults to: {})

    additional HTTP request headers to use for the request

Returns:

  • (Faraday::Response)

    body of the HTTP response

Raises:

  • (ArgumentError)


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/html2rss/utils.rb', line 77

def self.request_url(url, headers: {})
  url = Addressable::URI.parse(url.to_s) unless url.is_a?(Addressable::URI)

  raise ArgumentError, 'URL must be absolute' unless url.absolute?
  raise ArgumentError, 'URL must not contain an @ characater' if url.to_s.include?('@')

  Faraday.new(url:, headers:) do |faraday|
    faraday.use Faraday::FollowRedirects::Middleware
    faraday.adapter Faraday.default_adapter
  end.get
end

.sanitize_url(url) ⇒ Addressable::URI?

Removes any space, parses and normalizes the given url.

Parameters:

  • url (String)

Returns:

  • (Addressable::URI, nil)

    normalized URL, or nil if input is empty



35
36
37
38
39
40
# File 'lib/html2rss/utils.rb', line 35

def self.sanitize_url(url)
  url = url.to_s.gsub(/\s+/, ' ').strip
  return if url.empty?

  Addressable::URI.parse(url).normalize
end

.titleized_url(url) ⇒ String

Builds a titleized representation of the URL.

Parameters:

  • url (String, Addressable::URI)

Returns:

  • (String)


65
66
67
68
69
70
71
# File 'lib/html2rss/utils.rb', line 65

def self.titleized_url(url)
  uri = Addressable::URI.parse(url)
  host = uri.host

  nicer_path = uri.path.split('/').reject(&:empty?)
  nicer_path.any? ? "#{host}: #{nicer_path.map(&:capitalize).join(' ')}" : host
end

.use_zone(time_zone, default_time_zone: Time.now.getlocal.zone) { ... } ⇒ Object

Allows override of time zone locally inside supplied block; resets previous time zone when done.

Parameters:

  • time_zone (String)
  • default_time_zone (String) (defaults to: Time.now.getlocal.zone)

Yields:

  • block to execute with the given time zone

Returns:

  • (Object)

    whatever the given block returns



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/html2rss/utils.rb', line 49

def self.use_zone(time_zone, default_time_zone: Time.now.getlocal.zone)
  raise ArgumentError, 'a block is required' unless block_given?

  time_zone = TZInfo::Timezone.get(time_zone)

  prev_tz = ENV.fetch('TZ', default_time_zone)
  ENV['TZ'] = time_zone.name
  yield
ensure
  ENV['TZ'] = prev_tz if prev_tz
end