Module: Roast::Resources

Extended by:
Resources
Included in:
Resources
Defined in:
lib/roast/resources.rb,
lib/roast/resources/api_resource.rb,
lib/roast/resources/url_resource.rb,
lib/roast/resources/base_resource.rb,
lib/roast/resources/file_resource.rb,
lib/roast/resources/none_resource.rb,
lib/roast/resources/directory_resource.rb

Overview

The Resources module contains classes for handling different types of resources that workflows can operate on. Each resource type implements a common interface.

Defined Under Namespace

Classes: ApiResource, BaseResource, DirectoryResource, FileResource, NoneResource, UrlResource

Instance Method Summary collapse

Instance Method Details

#detect_type(target) ⇒ Symbol

Determines the resource type from the target

Parameters:

  • target (String)

    The target specified in the workflow

Returns:

  • (Symbol)

    :file, :directory, :url, :api, or :none



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/roast/resources.rb', line 44

def detect_type(target)
  return :none if target.nil? || target.strip.empty?

  # Check for command syntax $(...)
  if target.match?(/^\$\(.*\)$/)
    return :command
  end

  # Check for URLs
  if target.start_with?("http://", "https://", "ftp://")
    return :url
  end

  # Try to parse as URI to detect other URL schemes
  begin
    uri = URI.parse(target)
    return :url if uri.scheme && uri.host
  rescue URI::InvalidURIError
    # Not a URL, continue with other checks
  end

  # Check for directory
  if Dir.exist?(target)
    return :directory
  end

  # Check for glob patterns (containing * or ?)
  if target.include?("*") || target.include?("?")
    matches = Dir.glob(target)
    return :none if matches.empty?
    # If the glob matches only directories, treat as directory type
    return :directory if matches.all? { |path| Dir.exist?(path) }

    # Otherwise treat as file type (could be mixed or all files)
    return :file
  end

  # Check for file
  if File.exist?(target)
    return :file
  end

  # Check for API configuration in Fetch API style format
  begin
    potential_config = JSON.parse(target)
    if potential_config.is_a?(Hash) && potential_config.key?("url") && potential_config.key?("options")
      return :api
    end
  rescue JSON::ParserError
    # Not a JSON string, continue with other checks
  end

  # Default to file for anything else
  :file
end

#for(target) ⇒ BaseResource

Create the appropriate resource based on the target

Parameters:

  • target (String)

    The target specified in the workflow

Returns:

  • (BaseResource)

    A resource object of the appropriate type



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/roast/resources.rb', line 20

def for(target)
  type = detect_type(target)

  case type
  when :file
    FileResource.new(target)
  when :directory
    DirectoryResource.new(target)
  when :url
    UrlResource.new(target)
  when :api
    ApiResource.new(target)
  when :command
    CommandResource.new(target)
  when :none
    NoneResource.new(target)
  else
    BaseResource.new(target) # Default to base resource
  end
end