Skip to content

Commit 5a04083

Browse files
author
Daniel Jackson
committed
Started working on the Spect Insert feature for automatic conversion of API calls into Language SDK.
Started with python first - Added "Example_code in utils.rb" - Added Switch statement for example_code - implemented example_code.rb to hande logic for each language - example_code.mustache for templating - cat-allocation.md will be the starting test file
1 parent 09025be commit 5a04083

File tree

5 files changed

+77
-2
lines changed

5 files changed

+77
-2
lines changed

_api-reference/cat/cat-allocation.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,29 @@ GET /_cat/allocation/{node_id}
2727
```
2828
<!-- spec_insert_end -->
2929

30+
<!-- spec_insert_start
31+
api: cat.allocation
32+
component: example_code
33+
query_params: v
34+
-->
35+
{% capture step1_rest %}
36+
GET /_cat/allocation?v
37+
{% endcapture %}
38+
39+
{% capture step1_python %}
40+
response = client.cat.allocation(v: true)
41+
{% endcapture %}
42+
43+
{% capture step1_javascript %}
44+
// TODO: add JS client call for cat.allocation
45+
{% endcapture %}
46+
47+
{% include code-block.html
48+
rest=step1_rest
49+
python=step1_python
50+
javascript=step1_javascript
51+
%}
52+
<!-- spec_insert_end -->
3053

3154
<!-- spec_insert_start
3255
api: cat.allocation
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
class ExampleCode < BaseMustacheRenderer
4+
self.template_file = "#{__dir__}/templates/example_code.mustache"
5+
6+
def initialize(action, args)
7+
super(action, args)
8+
end
9+
10+
def query_params
11+
@args.raw['query_params']&.split(',')&.map(&:strip) || []
12+
end
13+
14+
def rest_code
15+
method = @action.http_verbs.first.upcase rescue 'GET'
16+
path = @action.urls.first rescue '/'
17+
query = query_params
18+
query_string = query.any? ? "?#{query.join('&')}" : ""
19+
"#{method} #{path}#{query_string}"
20+
end
21+
22+
def python_code
23+
query = query_params
24+
args = query.map { |k| "#{k}: true" }.join(', ')
25+
"response = client.#{@action.full_name}(#{args})"
26+
end
27+
28+
def javascript_code
29+
"// TODO: add JS client call for #{@action.full_name}"
30+
end
31+
end

spec-insert/lib/renderers/spec_insert.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require_relative 'path_parameters'
88
require_relative 'query_parameters'
99
require_relative 'body_parameters'
10+
require_relative 'example_code'
1011

1112
# Class to render spec insertions
1213
class SpecInsert < BaseMustacheRenderer
@@ -40,8 +41,10 @@ def content
4041
BodyParameters.new(@action, @args, is_request: true).render
4142
when :response_body_parameters
4243
BodyParameters.new(@action, @args, is_request: false).render
44+
when :example_code
45+
ExampleCode.new(@action, @args).render
4346
else
44-
raise SpecInsertError, "Invalid component: #{@args.component}"
47+
raise SpecInsertError, "Invalid component: #{@args.component}, from spec_insert.rb "
4548
end
4649
end
4750
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{% capture step1_rest %}
2+
{{{rest_code}}}
3+
{% endcapture %}
4+
5+
{% capture step1_python %}
6+
{{{python_code}}}
7+
{% endcapture %}
8+
9+
{% capture step1_javascript %}
10+
{{{javascript_code}}}
11+
{% endcapture %}
12+
13+
{% include code-block.html
14+
rest=step1_rest
15+
python=step1_python
16+
javascript=step1_javascript
17+
%}

spec-insert/lib/utils.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ module Utils
1313
'query_parameters' => 'Query Parameters',
1414
'path_parameters' => 'Path Parameters',
1515
'request_body_parameters' => 'Request Body Parameters',
16-
'response_body_parameters' => 'Response Body Parameters'
16+
'response_body_parameters' => 'Response Body Parameters',
17+
'example_code' => 'Example Code'
1718
}.freeze
1819

1920
# @return [Array<String>] list of markdown files to insert the spec components into

0 commit comments

Comments
 (0)