Class: Roast::Resources::ApiResource

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

Overview

Resource implementation for API endpoints using Fetch API-style format

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

#api_urlObject



28
29
30
31
32
33
34
35
# File 'lib/roast/resources/api_resource.rb', line 28

def api_url
  if config && config["url"]
    config["url"]
  else
    # Assume direct URL
    target
  end
end

#configObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/roast/resources/api_resource.rb', line 16

def config
  return @config if defined?(@config)

  @config = if target.is_a?(String) && target.match?(/^\s*{/)
    begin
      JSON.parse(target)
    rescue JSON::ParserError
      nil
    end
  end
end

#contentsObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/roast/resources/api_resource.rb', line 86

def contents
  return unless target

  # If it's a configuration, return a prepared request object
  if config
    JSON.pretty_generate({
      "url" => api_url,
      "method" => (options["method"] || "GET").upcase,
      "headers" => options["headers"] || {},
      "body" => options["body"],
    })
  else
    # Assume it's a direct API URL, do a simple GET
    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 API contents: #{e.message}")
      nil
    end
  end
end

#exists?Boolean

Returns:

  • (Boolean)


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
# File 'lib/roast/resources/api_resource.rb', line 56

def exists?
  return false unless target

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

  # Use HEAD request to check existence
  request = Net::HTTP::Head.new(uri.path.empty? ? "/" : uri.path)

  # Add headers if present in options
  if options["headers"].is_a?(Hash)
    options["headers"].each do |key, value|
      # Process any environment variables in header values
      processed_value = process_env_vars(value.to_s)
      request[key] = processed_value
    end
  end

  # Make the request
  response = http.request(request)

  # 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 API existence: #{e.message}")
  false
end

#http_methodObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/roast/resources/api_resource.rb', line 43

def http_method
  method_name = (options["method"] || "GET").upcase
  case method_name
  when "GET" then Net::HTTP::Get
  when "POST" then Net::HTTP::Post
  when "PUT" then Net::HTTP::Put
  when "DELETE" then Net::HTTP::Delete
  when "PATCH" then Net::HTTP::Patch
  when "HEAD" then Net::HTTP::Head
  else Net::HTTP::Get
  end
end

#nameObject



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/roast/resources/api_resource.rb', line 110

def name
  if target
    if config && config["url"]
      "API #{config["url"]} (#{(options["method"] || "GET").upcase})"
    else
      "API #{target}"
    end
  else
    "Unnamed API"
  end
end

#optionsObject



37
38
39
40
41
# File 'lib/roast/resources/api_resource.rb', line 37

def options
  return {} unless config

  config["options"] || {}
end

#processObject



11
12
13
14
# File 'lib/roast/resources/api_resource.rb', line 11

def process
  # For API resources, the target might be a JSON configuration or endpoint URL
  target
end

#typeObject



122
123
124
# File 'lib/roast/resources/api_resource.rb', line 122

def type
  :api
end