Class: Roast::Resources::UrlResource

Inherits:
BaseResource show all
Defined in:
lib/roast/resources/url_resource.rb

Overview

Resource implementation for URLs

Instance Attribute Summary

Attributes inherited from BaseResource

#target

Instance Method Summary collapse

Methods inherited from BaseResource

#initialize

Constructor Details

This class inherits a constructor from Roast::Resources::BaseResource

Instance Method Details

#contentsObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/roast/resources/url_resource.rb', line 35

def contents
  return unless target

  begin
    uri = URI.parse(target)
    Net::HTTP.get(uri)
  rescue StandardError => e
    # Log the error but don't crash
    Roast::Helpers::Logger.error("Error fetching URL contents: #{e.message}")
    nil
  end
end

#exists?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/roast/resources/url_resource.rb', line 15

def exists?
  return false unless target

  begin
    uri = URI.parse(target)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = (uri.scheme == "https")

    # Just check the head to see if the resource exists
    response = http.request_head(uri.path.empty? ? "/" : uri.path)

    # Consider 2xx and 3xx as success
    response.code.to_i < 400
  rescue StandardError => e
    # Log the error but don't crash
    Roast::Helpers::Logger.error("Error checking URL existence: #{e.message}")
    false
  end
end

#nameObject



48
49
50
51
52
53
54
55
56
# File 'lib/roast/resources/url_resource.rb', line 48

def name
  if target
    URI.parse(target).host
  else
    "Unnamed URL"
  end
rescue URI::InvalidURIError
  "Invalid URL"
end

#processObject



10
11
12
13
# File 'lib/roast/resources/url_resource.rb', line 10

def process
  # URLs don't need special processing, just return as is
  target
end

#typeObject



58
59
60
# File 'lib/roast/resources/url_resource.rb', line 58

def type
  :url
end