Skip to content

Commit 0b6a567

Browse files
committed
refact: ensure all payload keys are camelised.
And shift that camelising logic borrowed from awrence into a helper class.
1 parent eb7563a commit 0b6a567

File tree

7 files changed

+95
-24
lines changed

7 files changed

+95
-24
lines changed

lib/render_api/camelise.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
module RenderAPI
4+
# Borrowed from awrence:
5+
# https://github.com/technicalpanda/awrence/blob/main/lib/awrence/methods.rb
6+
class Camelise
7+
def self.call(...)
8+
new.call(...)
9+
end
10+
11+
def call(string, first_upper: false)
12+
if first_upper
13+
string = gsubbed(string, /(?:^|_)([^_\s]+)/)
14+
gsubbed(string, %r{/([^/]*)}, "::")
15+
else
16+
parts = string.split("_", 2)
17+
parts[0] << call(parts[1], first_upper: true) if parts.size > 1
18+
parts[0] || ""
19+
end
20+
end
21+
22+
private
23+
24+
def gsubbed(string, pattern, extra = "")
25+
string.gsub(pattern) { extra + Regexp.last_match(1).capitalize }
26+
end
27+
end
28+
end

lib/render_api/clients/deploys.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module Clients
77
class Deploys < Base
88
def create(service_id, clear_cache: nil)
99
body = nil
10-
body = { clearCache: clear_cache } unless clear_cache.nil?
10+
body = { clear_cache: clear_cache } unless clear_cache.nil?
1111

1212
endpoint.post(
1313
"/services/#{service_id}/deploys", body: body

lib/render_api/clients/jobs.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module Clients
77
class Jobs < Base
88
def create(service_id, start_command:, plan_id: nil)
99
body = { startCommand: start_command }
10-
body[:planId] = plan_id unless plan_id.nil?
10+
body[:plan_id] = plan_id unless plan_id.nil?
1111

1212
endpoint.post(
1313
"/services/#{service_id}/jobs", body: body

lib/render_api/data_object.rb

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require_relative "./camelise"
4+
35
module RenderAPI
46
class DataObject
57
TIME_PATTERN = /\A\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\d+Z\z/.freeze
@@ -23,34 +25,15 @@ def to_h
2325

2426
attr_reader :data
2527

26-
# Borrowed from awrence:
27-
# https://github.com/technicalpanda/awrence/blob/main/lib/awrence/methods.rb
28-
def camelize(string, first_upper: false)
29-
if first_upper
30-
string = gsubbed(string, /(?:^|_)([^_\s]+)/)
31-
gsubbed(string, %r{/([^/]*)}, "::")
32-
else
33-
parts = string.split("_", 2)
34-
parts[0] << camelize(parts[1], first_upper: true) if parts.size > 1
35-
parts[0] || ""
36-
end
37-
end
38-
39-
# Borrowed and simplified from awrence:
40-
# https://github.com/technicalpanda/awrence/blob/main/lib/awrence/methods.rb
41-
def gsubbed(str, pattern, extra = "")
42-
str.gsub(pattern) { extra + Regexp.last_match(1).capitalize }
43-
end
44-
4528
def method_missing(name, *args, **kwargs, &block)
4629
return super unless respond_to_missing?(name, false)
4730
raise ArgumentError if args.any? || kwargs.any? || block
4831

49-
translate(data[camelize(name.to_s)])
32+
translate(data[Camelise.call(name.to_s)])
5033
end
5134

5235
def respond_to_missing?(name, _include_all)
53-
data.key?(camelize(name.to_s))
36+
data.key?(Camelise.call(name.to_s))
5437
end
5538

5639
def translate(object)

lib/render_api/endpoint.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require "http"
44

5+
require_relative "./camelise"
56
require_relative "./response"
67

78
module RenderAPI
@@ -18,7 +19,7 @@ def get(path, params: nil)
1819
end
1920

2021
def post(path, body: nil)
21-
request(:post, path, body: body)
22+
request(:post, path, body: camelise(body))
2223
end
2324

2425
def delete(path)
@@ -29,6 +30,19 @@ def delete(path)
2930

3031
attr_reader :api_key
3132

33+
def camelise(object)
34+
case object
35+
when Hash
36+
object
37+
.transform_keys { |key| Camelise.call(key.to_s) }
38+
.transform_values { |value| camelise(value) }
39+
when Array
40+
object.collect { |item| camelise(item) }
41+
else
42+
object
43+
end
44+
end
45+
3246
def handle_error(response)
3347
raise RequestError, response.body.to_s
3448
end

spec/features/deploys_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@
2626
)
2727
end
2828

29+
it "sends no payload by default" do
30+
subject.create(service_id)
31+
32+
expect(
33+
a_request(
34+
:post, "https://api.render.com/v1/services/#{service_id}/deploys"
35+
).with { |request| request.body == "" }
36+
).to have_been_made
37+
end
38+
39+
it "sends a payload if clearing the cache" do
40+
subject.create(service_id, clear_cache: "clear")
41+
42+
expect(
43+
a_request(
44+
:post, "https://api.render.com/v1/services/#{service_id}/deploys"
45+
).with_json_body do |json|
46+
json == { "clearCache" => "clear" }
47+
end
48+
).to have_been_made
49+
end
50+
2951
it "returns the new deploy details" do
3052
response = subject.create(service_id)
3153

spec/features/jobs_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@
2424
)
2525
end
2626

27+
it "sends the provided payload" do
28+
subject.create(service_id, start_command: "whoami")
29+
30+
expect(
31+
a_request(
32+
:post, "https://api.render.com/v1/services/#{service_id}/jobs"
33+
).with_json_body do |json|
34+
json == { "startCommand" => "whoami" }
35+
end
36+
).to have_been_made
37+
end
38+
39+
it "includes the plan id if provided" do
40+
subject.create(service_id, start_command: "whoami", plan_id: "a-plan")
41+
42+
expect(
43+
a_request(
44+
:post, "https://api.render.com/v1/services/#{service_id}/jobs"
45+
).with_json_body do |json|
46+
json == { "startCommand" => "whoami", "planId" => "a-plan" }
47+
end
48+
).to have_been_made
49+
end
50+
2751
it "returns the new job details" do
2852
response = subject.create(service_id, start_command: "whoami")
2953

0 commit comments

Comments
 (0)