Class: Roast::Resources::ApiResource
Overview
Resource implementation for API endpoints using Fetch API-style format
Instance Attribute Summary
Attributes inherited from BaseResource
#target
Instance Method Summary
collapse
#initialize
Instance Method Details
#api_url ⇒ Object
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
target
end
end
|
#config ⇒ Object
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
|
#contents ⇒ Object
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 config
JSON.pretty_generate({
"url" => api_url,
"method" => (options["method"] || "GET").upcase,
"headers" => options["headers"] || {},
"body" => options["body"],
})
else
begin
uri = URI.parse(target)
Net::HTTP.get(uri)
rescue StandardError => e
Roast::Helpers::Logger.error("Error fetching API contents: #{e.message}")
nil
end
end
end
|
#exists? ⇒ 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")
request = Net::HTTP::Head.new(uri.path.empty? ? "/" : uri.path)
if options["headers"].is_a?(Hash)
options["headers"].each do |key, value|
processed_value = process_env_vars(value.to_s)
request[key] = processed_value
end
end
response = http.request(request)
response.code.to_i < 400
rescue StandardError => e
Roast::Helpers::Logger.error("Error checking API existence: #{e.message}")
false
end
|
#http_method ⇒ Object
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
|
#name ⇒ Object
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
|
#options ⇒ Object
37
38
39
40
41
|
# File 'lib/roast/resources/api_resource.rb', line 37
def options
return {} unless config
config["options"] || {}
end
|
#process ⇒ Object
11
12
13
14
|
# File 'lib/roast/resources/api_resource.rb', line 11
def process
target
end
|
#type ⇒ Object
122
123
124
|
# File 'lib/roast/resources/api_resource.rb', line 122
def type
:api
end
|