Skip to content

Commit 2d67523

Browse files
CopilotGrantBirki
andcommitted
Implement JSON default format with configuration support
Co-authored-by: GrantBirki <[email protected]>
1 parent 09a8393 commit 2d67523

File tree

6 files changed

+55
-18
lines changed

6 files changed

+55
-18
lines changed

lib/hooks/app/api.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ def self.create(config:, endpoints:, log:)
3939
content_type :xml, "application/xml"
4040
content_type :any, "*/*"
4141

42-
format :txt # TODO: make this configurable via config[:format] (defaults to :json in the future)
43-
default_format :txt # TODO: make this configurable via config[:default_format] (defaults to :json in the future)
42+
format config[:format] || :json
43+
default_format config[:default_format] || :json
4444
end
4545

4646
api_class.class_eval do
@@ -118,8 +118,7 @@ def self.create(config:, endpoints:, log:)
118118
log.info("successfully processed webhook event with handler: #{handler_class_name}")
119119
log.debug("processing duration: #{Time.now - start_time}s")
120120
status 200
121-
content_type "application/json"
122-
response.to_json
121+
response
123122
rescue Hooks::Plugins::Handlers::Error => e
124123
# Handler called error! method - immediately return error response and exit the request
125124
log.debug("handler #{handler_class_name} called `error!` method")
@@ -132,8 +131,8 @@ def self.create(config:, endpoints:, log:)
132131
content_type "text/plain"
133132
error_response = e.body
134133
else
135-
content_type "application/json"
136-
error_response = e.body.to_json
134+
# Let Grape handle JSON conversion with the default format
135+
error_response = e.body
137136
end
138137

139138
return error_response
@@ -164,8 +163,7 @@ def self.create(config:, endpoints:, log:)
164163
error_response[:handler] = handler_class_name unless config[:production]
165164

166165
status determine_error_code(e)
167-
content_type "application/json"
168-
error_response.to_json
166+
error_response
169167
end
170168
end
171169
end

lib/hooks/app/endpoints/catchall.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ module App
1717
class CatchallEndpoint < Grape::API
1818
include Hooks::App::Helpers
1919

20+
# Set up content types and default format to JSON to match main API
21+
content_type :json, "application/json"
22+
content_type :txt, "text/plain"
23+
content_type :xml, "application/xml"
24+
content_type :any, "*/*"
25+
format :json
26+
default_format :json
27+
2028
def self.mount_path(config)
2129
# :nocov:
2230
"#{config[:root_path]}/*path"
@@ -81,8 +89,7 @@ def self.route_block(captured_config, captured_logger)
8189
log.info("successfully processed webhook event with handler: #{handler_class_name}")
8290
log.debug("processing duration: #{Time.now - start_time}s")
8391
status 200
84-
content_type "application/json"
85-
response.to_json
92+
response
8693
rescue StandardError => e
8794
err_msg = "Error processing webhook event with handler: #{handler_class_name} - #{e.message} " \
8895
"- request_id: #{request_id} - path: #{full_path} - method: #{http_method} - " \
@@ -102,8 +109,7 @@ def self.route_block(captured_config, captured_logger)
102109
error_response[:handler] = handler_class_name unless config[:production]
103110

104111
status determine_error_code(e)
105-
content_type "application/json"
106-
error_response.to_json
112+
error_response
107113
end
108114
end
109115
end

lib/hooks/app/endpoints/health.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@
66
module Hooks
77
module App
88
class HealthEndpoint < Grape::API
9+
# Set up content types and default format to JSON
10+
content_type :json, "application/json"
11+
content_type :txt, "text/plain"
12+
content_type :xml, "application/xml"
13+
content_type :any, "*/*"
14+
format :json
15+
default_format :json
16+
917
get do
10-
content_type "application/json"
1118
{
1219
status: "healthy",
1320
timestamp: Time.now.utc.iso8601,
1421
version: Hooks::VERSION,
1522
uptime_seconds: (Time.now - Hooks::App::API.server_start_time).to_i
16-
}.to_json
23+
}
1724
end
1825
end
1926
end

lib/hooks/app/endpoints/version.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@
66
module Hooks
77
module App
88
class VersionEndpoint < Grape::API
9+
# Set up content types and default format to JSON
10+
content_type :json, "application/json"
11+
content_type :txt, "text/plain"
12+
content_type :xml, "application/xml"
13+
content_type :any, "*/*"
14+
format :json
15+
default_format :json
16+
917
get do
10-
content_type "application/json"
1118
{
1219
version: Hooks::VERSION,
1320
timestamp: Time.now.utc.iso8601
14-
}.to_json
21+
}
1522
end
1623
end
1724
end

lib/hooks/core/config_loader.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ class ConfigLoader
2020
production: true,
2121
endpoints_dir: "./config/endpoints",
2222
use_catchall_route: false,
23-
normalize_headers: true
23+
normalize_headers: true,
24+
format: :json,
25+
default_format: :json
2426
}.freeze
2527

2628
SILENCE_CONFIG_LOADER_MESSAGES = ENV.fetch(
@@ -141,6 +143,8 @@ def self.load_env_config
141143
"HOOKS_ENDPOINTS_DIR" => :endpoints_dir,
142144
"HOOKS_USE_CATCHALL_ROUTE" => :use_catchall_route,
143145
"HOOKS_NORMALIZE_HEADERS" => :normalize_headers,
146+
"HOOKS_FORMAT" => :format,
147+
"HOOKS_DEFAULT_FORMAT" => :default_format,
144148
"HOOKS_SOME_STRING_VAR" => :some_string_var # Added for test
145149
}
146150

@@ -155,6 +159,9 @@ def self.load_env_config
155159
when :use_catchall_route, :normalize_headers
156160
# Convert string to boolean
157161
env_config[config_key] = %w[true 1 yes on].include?(value.downcase)
162+
when :format, :default_format
163+
# Convert string to symbol
164+
env_config[config_key] = value.to_sym
158165
else
159166
env_config[config_key] = value
160167
end

spec/unit/lib/hooks/core/config_loader_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
production: true,
2020
endpoints_dir: "./config/endpoints",
2121
use_catchall_route: false,
22-
normalize_headers: true
22+
normalize_headers: true,
23+
format: :json,
24+
default_format: :json
2325
)
2426
end
2527
end
@@ -197,6 +199,16 @@
197199
expect(config[:normalize_headers]).to be true
198200
expect(config[:some_string_var]).to eq("test_value") # Check the string var
199201
end
202+
203+
it "converts format environment variables to symbols" do
204+
ENV["HOOKS_FORMAT"] = "txt"
205+
ENV["HOOKS_DEFAULT_FORMAT"] = "xml"
206+
207+
config = described_class.load
208+
209+
expect(config[:format]).to eq(:txt)
210+
expect(config[:default_format]).to eq(:xml)
211+
end
200212
end
201213

202214
context "with auth plugin directory configuration" do

0 commit comments

Comments
 (0)