red-api 0.1.5 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 34529a5a138b7f837a1e5242eac993f5072eaa897a598d91fc2f4e5d150c11db
4
- data.tar.gz: e69e843fd93e86b95f775c9f1d7335f36cd77375e0d79111aa026f09c370c4dc
3
+ metadata.gz: b953b6b0eb60428808e5e4e96bee115704ccb947e181d1ca00273607ee741a78
4
+ data.tar.gz: c4658cbf00f844f698f46ab5a3c0e68816d67f82af29971a76914b8337a5d7a7
5
5
  SHA512:
6
- metadata.gz: 61977f5fef8591ec94c0a88f9064eecf3878442986bbf3cd41626ac11859657d695f37ba7a16b1ef0d384ed14c45f19e1e4ef033213ee508392429e5bb429ebd
7
- data.tar.gz: 8ad5a8f28a14e28ff2af2e6496b4542eccefbad7c40f7503ba0af60e3028d14e36b718c60d0e9c46b8cc1d81c877bb4e294a79cb5468b06642b253e86569944d
6
+ metadata.gz: f075a31f36be34e96985768565d8e3a49c1644c5c3562b5290374d9cf8ec714a3b0d1d1a01db2af96f9cd84dff08307d8eafa615d984b7c1068444face8f6cc8
7
+ data.tar.gz: 3d6a8aaba3eb5448c479258d45ff3a0c9e9c1dfa136570c7923908c2915a5e8db5e2e25e0d29fa951d108d0837833d16bd051d7eec97df46cd10ca0cf396b222
@@ -1,8 +1,18 @@
1
1
  Description:
2
- Adds an endpoint api call to your generated API service with optional method params (not to be confused with the api endpoint params)
2
+ Adds an endpoint api call to your generated API service with optional endpoint method and endpoint helper method parameters
3
3
 
4
+ --include-helper-params:
5
+ Will copy the --method-params given to your endpoint helper method to use in your endpoint URI path
6
+
4
7
  Example:
5
- rails generate endpoint ServiceName EndpointName /endpoint/#{param1}/#{param2} --method_params param1 param2
8
+ rails generate endpoint ServiceName version EndpointName /endpoint/#{param1}/#{param2} --method-params param1 param2 --include-helper-params
9
+
10
+ Simple Form:
11
+ rails generate endpoint ServiceName V1 EndpointName /endpoint
6
12
 
7
13
  This will create:
8
- app/services/ServiceName/api_call/endpoint_name.rb
14
+ app/services/ServiceName/version/api_call/endpoint_name.rb
15
+
16
+ This will modify:
17
+ app/services/ServiceName/version/api_endpoints.rb
18
+ app/services/ServiceName/version/endpoint_helpers.rb
@@ -1,46 +1,78 @@
1
1
  class EndpointGenerator < Rails::Generators::NamedBase
2
2
  source_root File.expand_path('templates', __dir__)
3
3
 
4
+ argument :api_version, type: :string
4
5
  argument :endpoint_name, type: :string
5
6
  argument :endpoint, type: :string
6
- class_option :method, type: :string, default: :get
7
- class_option :method_params, type: :array
8
7
 
8
+ class_option :method, type: :string, default: 'get'
9
+ class_option :endpoint_params, type: :array
10
+ class_option :include_helper_params
11
+
9
12
  def create_api_call
10
- @api_name = endpoint_name
13
+ @api_service = file_name.underscore
14
+ @api_version = api_version
15
+ @endpoint_name = endpoint_name
11
16
  @endpoint = endpoint
12
- @endpoint = @endpoint[1..] if @endpoint.first == '/'
17
+
18
+ raise ArgumentError 'Endpoint name required' unless @endpoint_name
19
+ raise ArgumentError 'Endpoint is required' unless @endpoint
20
+
21
+ @endpoint_reference = @api_service.camelize + '::' + @api_version.camelize + '::ApiEndpoints'
22
+ @full_endpoint_reference = @endpoint_reference + '.' + @endpoint_name.underscore
23
+
24
+ @endpoint = @endpoint[1..] unless @endpoint.first != '/'
13
25
  @method = options[:method].to_sym
14
26
 
15
- if options[:method_params].present?
16
- @method_params = options[:method_params][0..-2].each_with_object('') do |p, str|
17
- str << p << ', '
18
- end
19
- @method_params << options[:method_params].last
20
- end
27
+ create_endpoint_params if options[:endpoint_params].present?
28
+ create_helper_params if options[:include_helper_params].present?
21
29
 
22
- @api_service = file_name.underscore
23
- service_dir_path = "app/services/#{@api_service}/"
30
+ spec_file_name = @endpoint_name.underscore + '_spec.rb';
31
+ spec_dir = 'spec/services/'
32
+ spec_api_dir = spec_dir + @api_service.underscore + '/'
33
+ service_version_dir = spec_api_dir + @api_version.downcase + '/'
24
34
 
25
- template 'api_call.erb', service_dir_path + "api_calls/#{@api_name.underscore}.rb"
35
+ Dir.mkdir spec_dir unless File.exist?(spec_dir)
36
+ Dir.mkdir spec_api_dir unless File.exist?(spec_api_dir)
37
+ Dir.mkdir service_version_dir unless File.exist?(service_version_dir)
26
38
 
27
- temp = ERB.new <<-'EOF'
39
+ spec_file_path = service_version_dir + spec_file_name
28
40
 
29
- def <%= @api_name.underscore%>_endpoint<%="(#{@method_params})" if @method_params.present?%>
30
- <%= "\"#{@endpoint}\".freeze" if @endpoint.present? %>
31
- end
32
- EOF
41
+ template 'endpoint_spec.erb', spec_file_path
42
+
43
+ service_dir_path = "app/services/#{@api_service}/"
44
+ service_api_version_path = service_dir_path + @api_version + '/'
45
+
46
+ template 'api_call.erb', service_api_version_path + "api_calls/#{@endpoint_name.underscore}.rb"
47
+
48
+ api_call_file = ERB.new load_template('endpoint_helper.erb')
33
49
 
34
- inject_into_file service_dir_path + 'endpoint_helpers.rb', after: " # API Endpoint Helpers\n" do
35
- temp.result(binding)
50
+ inject_into_file service_api_version_path + 'endpoint_helpers.rb', after: " # API Endpoint Helpers\n" do
51
+ api_call_file.result(binding)
52
+ end
53
+
54
+ mixin = ERB.new load_template('mixin.erb')
55
+
56
+ inject_into_file service_api_version_path + 'api_endpoints.rb', after: " # API call modules\n" do
57
+ mixin.result(binding)
36
58
  end
59
+ end
60
+
61
+ private
37
62
 
38
- temp = ERB.new <<-'EOF'
39
- extend <%= @api_name.camelize%>
40
- EOF
63
+ def load_template(template_file)
64
+ template_path = File.join(File.dirname(__FILE__), 'templates/' + template_file)
65
+ File.read(template_path)
66
+ end
67
+
68
+ def create_helper_params
69
+ @helper_params = @endpoint_params
70
+ end
41
71
 
42
- inject_into_file service_dir_path + 'api_endpoints.rb', after: " # API call modules\n" do
43
- temp.result(binding)
72
+ def create_endpoint_params
73
+ @endpoint_params = options[:endpoint_params][0..-2].each_with_object('') do |p, str|
74
+ str << p.underscore << ', '
44
75
  end
76
+ @endpoint_params << options[:endpoint_params].last
45
77
  end
46
78
  end
@@ -1,5 +1,5 @@
1
- module <%= @api_name.camelize%>
2
- def <%= @api_name.underscore%><%="(#{@method_params})" if @method_params.present?%>
3
- request :<%= @method%>, <%= @api_name.underscore%>_endpoint<%="(#{@method_params})" if @method_params.present?%>, params: {}
1
+ module <%= @endpoint_name.camelize%>
2
+ def <%= @endpoint_name.underscore%><%="(#{@endpoint_params})" if @endpoint_params%>
3
+ request :<%= @method%>, <%= @endpoint_name.underscore%>_endpoint<%="(#{@helper_params})" if @helper_params%>, params: {}
4
4
  end
5
5
  end
@@ -0,0 +1,4 @@
1
+
2
+ def <%= @endpoint_name.underscore%>_endpoint<%="(#{@helper_params})" if @helper_params%>
3
+ <%= "\"#{@endpoint}\".freeze" if @endpoint %>
4
+ end
@@ -0,0 +1,11 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe <%=@endpoint_reference%> do
4
+ describe '<%=@endpoint_name.underscore%>' do
5
+ it 'returns expected data when successful' do
6
+ skip 'Write tests for <%=@endpoint_name.underscore%>'
7
+ <%='# Define endpoint parameters here' if @endpoint_params%>
8
+ response = <%=@full_endpoint_reference%><%="(#{@endpoint_params})" if @endpoint_params%>
9
+ end
10
+ end
11
+ end
@@ -0,0 +1 @@
1
+ extend <%= @endpoint_name.camelize%>
@@ -2,7 +2,14 @@ Description:
2
2
  Generates the framework for a basic API service your app consumes
3
3
 
4
4
  Example:
5
- rails generate service ServiceName https://service/api/endpoint/ --key api_key env_key_name
5
+ rails generate service ServiceName version https://service/api/endpoint/ --key api_key env_key_name
6
+
7
+ Simple Form:
8
+ rails generate service ServiceName V1 https://service/api/endpoint/
6
9
 
7
10
  This will create:
8
- app/services/ServiceName
11
+ app/services/ServiceName/version
12
+ app/services/ServiceName/version/api_calls/
13
+ app/services/ServiceName/version/api_endpoints.rb
14
+ app/services/ServiceName/version/client.rb
15
+ app/services/ServiceName/version/endpoint_helpers.rb
@@ -1,21 +1,30 @@
1
1
  class ServiceGenerator < Rails::Generators::NamedBase
2
2
  source_root File.expand_path('templates', __dir__)
3
3
 
4
+ argument :version, type: :string
4
5
  argument :api_endpoint, type: :string
5
6
  class_option :key, type: :array
6
7
 
7
8
  def create_api_service
8
9
  @api_name = file_name
10
+ @api_version = version
9
11
  @api_endpoint = api_endpoint
10
12
  @env_var_opts = options[:key]
11
- raise 'Need to define the name of your API service with --api' unless @api_name.present?
12
13
 
13
- @api_dir_name = @api_name.underscore
14
+ raise ArgumentError, 'Need to give the API version' unless @api_version.present?
15
+ raise ArgumentError, 'Need to give the API endpoint URI e.g. https://api/' unless @api_endpoint.present?
16
+
17
+ @full_api_reference = @api_name.camelize + '::' + @api_version.upcase
18
+ @api_endpoint = @api_endpoint + '/' unless @api_endpoint.last == '/'
14
19
 
15
20
  root_dir = "app/services/"
16
21
  Dir.mkdir root_dir unless File.exist?(root_dir)
17
22
 
18
- service_dir_path = "#{root_dir}#{@api_dir_name}/"
23
+ api_path = root_dir + @api_name.underscore + '/'
24
+
25
+ Dir.mkdir api_path unless File.exist?(api_path)
26
+
27
+ service_dir_path = api_path + @api_version.downcase + '/'
19
28
 
20
29
  Dir.mkdir service_dir_path unless File.exist?(service_dir_path)
21
30
  Dir.mkdir "#{service_dir_path}/api_calls" unless File.exist?("#{service_dir_path}/api_calls")
@@ -1,5 +1,5 @@
1
- Dir[Rails.root.join('app/services/<%= @api_dir_name%>/api_calls/*.rb')].each { |api_call| require api_call }
1
+ Dir[Rails.root.join('app/services/<%= @api_name.underscore%>/<%=@api_version.downcase%>/api_calls/*.rb')].sort.each { |api_call| require api_call }
2
2
 
3
- class <%= @api_name.camelize%>::ApiEndpoints < <%= @api_name.camelize%>::Client
3
+ class <%=@full_api_reference%>::ApiEndpoints < <%= @full_api_reference%>::Client
4
4
  # API call modules
5
- end
5
+ end
@@ -1,6 +1,6 @@
1
1
  require_relative 'endpoint_helpers'
2
2
 
3
- class <%= @api_name.camelize%>::Client
3
+ class <%=@full_api_reference%>::Client
4
4
  extend EndpointHelpers
5
5
 
6
6
  def self.client(params)
@@ -1,7 +1,7 @@
1
1
  module EndpointHelpers
2
2
  def api_endpoint
3
3
  # The http API endpoint for this service
4
- <%= "'#{@api_endpoint}'.freeze" if @api_endpoint.present? %>
4
+ <%= "'#{@api_endpoint}'.freeze" %>
5
5
  end
6
6
 
7
7
  # API Endpoint Helpers
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: red-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard DeSilvey
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-18 00:00:00.000000000 Z
11
+ date: 2021-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: 1.5.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: Generate API service and endpoints for API consumption
56
70
  email:
57
71
  - rdesilvey@gmail.com
@@ -63,6 +77,9 @@ files:
63
77
  - lib/generators/endpoint/USAGE
64
78
  - lib/generators/endpoint/endpoint_generator.rb
65
79
  - lib/generators/endpoint/templates/api_call.erb
80
+ - lib/generators/endpoint/templates/endpoint_helper.erb
81
+ - lib/generators/endpoint/templates/endpoint_spec.erb
82
+ - lib/generators/endpoint/templates/mixin.erb
66
83
  - lib/generators/service/USAGE
67
84
  - lib/generators/service/service_generator.rb
68
85
  - lib/generators/service/templates/api_endpoints.erb