Skip to content

Commit 60ccdbd

Browse files
committed
Extract build_endpoint
1 parent 8b28e81 commit 60ccdbd

4 files changed

Lines changed: 33 additions & 6 deletions

File tree

lib/adyen/services/service.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,12 @@ def create_query_string(params)
3131
return '' if params.empty?
3232
"?#{URI.encode_www_form(params)}"
3333
end
34+
35+
private
36+
37+
def build_endpoint(path, *params)
38+
endpoint = path.gsub('%', '%%').gsub(/{.+?}/, '%s').delete_prefix('/')
39+
format(endpoint, *params)
40+
end
3441
end
3542
end

spec/service_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,30 @@
33
require 'spec_helper'
44

55
RSpec.describe Adyen::Service do
6+
describe '#build_endpoint' do
7+
subject(:service) { described_class.new(nil, nil, 'Test') }
8+
9+
it 'strips the leading slash' do
10+
expect(service.send(:build_endpoint, '/accountHolders')).to eq 'accountHolders'
11+
end
12+
13+
it 'substitutes a single path parameter' do
14+
expect(service.send(:build_endpoint, '/accountHolders/{id}', 'AH123')).to eq 'accountHolders/AH123'
15+
end
16+
17+
it 'substitutes multiple path parameters' do
18+
expect(service.send(:build_endpoint, '/merchants/{merchantId}/stores/{storeId}', 'M1', 'S2')).to eq 'merchants/M1/stores/S2'
19+
end
20+
21+
it 'handles a path with no parameters' do
22+
expect(service.send(:build_endpoint, '/companies')).to eq 'companies'
23+
end
24+
25+
it 'preserves literal percent characters in the path' do
26+
expect(service.send(:build_endpoint, '/path%20with/{id}/spaces', 'X')).to eq 'path%20with/X/spaces'
27+
end
28+
end
29+
630
describe '.action_for_method_name' do
731
it 'handles all methods that exist currently' do
832
expect(described_class.action_for_method_name(:adjust_authorisation)).to eq 'adjustAuthorisation'

templates/api-small.mustache

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ module Adyen
2020
# Deprecated {{#vendorExtensions.x-deprecatedInVersion}}since {{#appName}}{{{.}}}{{/appName}} v{{.}}{{/vendorExtensions.x-deprecatedInVersion}}{{#vendorExtensions.x-deprecatedMessage}}
2121
# {{{.}}}{{/vendorExtensions.x-deprecatedMessage}}{{/isDeprecated}}
2222
def {{#lambda.snakecase}}{{#vendorExtensions.x-methodName}}{{.}}{{/vendorExtensions.x-methodName}}{{^vendorExtensions.x-methodName}}{{nickname}}{{/vendorExtensions.x-methodName}}{{/lambda.snakecase}}({{#bodyParams}}request, {{/bodyParams}}{{#requiredParams}}{{^isQueryParam}}{{#lambda.snakecase}}{{paramName}}{{/lambda.snakecase}}, {{/isQueryParam}}{{/requiredParams}}headers: {}{{#queryParams}}{{#-first}}, query_params: {}{{/-first}}{{/queryParams}})
23-
endpoint = '{{path}}'.gsub(/{.+?}/, '%s')
24-
endpoint = endpoint.gsub(%r{^/}, '')
25-
endpoint = format(endpoint{{#pathParams}}{{#lambda.snakecase}}, {{paramName}}{{/lambda.snakecase}}{{/pathParams}})
23+
endpoint = build_endpoint('{{path}}'{{#pathParams}}, {{#lambda.snakecase}}{{paramName}}{{/lambda.snakecase}}{{/pathParams}})
2624
{{#queryParams}}{{#-first}}endpoint += create_query_string(query_params){{/-first}}{{/queryParams}}
2725
action = { method: '{{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}}', url: endpoint }
2826
{{#bodyParams}}

templates/api.mustache

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ module Adyen
1919
# Deprecated {{#vendorExtensions.x-deprecatedInVersion}}since {{#appName}}{{{.}}}{{/appName}} v{{.}}{{/vendorExtensions.x-deprecatedInVersion}}{{#vendorExtensions.x-deprecatedMessage}}
2020
# {{{.}}}{{/vendorExtensions.x-deprecatedMessage}}{{/isDeprecated}}
2121
def {{#lambda.snakecase}}{{#vendorExtensions.x-methodName}}{{.}}{{/vendorExtensions.x-methodName}}{{^vendorExtensions.x-methodName}}{{nickname}}{{/vendorExtensions.x-methodName}}{{/lambda.snakecase}}({{#bodyParams}}request, {{/bodyParams}}{{#pathParams}}{{#lambda.snakecase}}{{paramName}}{{/lambda.snakecase}}, {{/pathParams}}headers: {}{{#queryParams}}{{#-first}}, query_params: {}{{/-first}}{{/queryParams}})
22-
endpoint = '{{path}}'.gsub(/{.+?}/, '%s')
23-
endpoint = endpoint.gsub(%r{^/}, '')
24-
endpoint = format(endpoint{{#pathParams}}, {{#lambda.snakecase}}{{paramName}}{{/lambda.snakecase}}{{/pathParams}})
22+
endpoint = build_endpoint('{{path}}'{{#pathParams}}, {{#lambda.snakecase}}{{paramName}}{{/lambda.snakecase}}{{/pathParams}})
2523
{{#queryParams}}{{#-first}}endpoint += create_query_string(query_params){{/-first}}{{/queryParams}}
2624
action = { method: '{{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}}', url: endpoint }
2725
{{#bodyParams}}

0 commit comments

Comments
 (0)