red-api 0.1.6 → 0.2.2

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: 53151d9692ecd31e415aa6c4387158e6da25fed70a4cc7f99a352efb93d7ab04
4
- data.tar.gz: 28215493dd8a9875435176dd4a7848db788d375c2f716d714bf09f8fb1d7c6b7
3
+ metadata.gz: d3137705ce3933690cf5048fc78576d68a7638499f8a9cdb18c59e83a7010fec
4
+ data.tar.gz: c01ba2e7a05de477e32e5441677b10f5f488fcd515d81897de746a358fe1fdd4
5
5
  SHA512:
6
- metadata.gz: 1dc400ed3600883b143b7be98621626b3d2cb9c501e0b4e6d0ff4275b91d6d3f9abc8cf478391539a75299d1d8b11702ed9387b830b2d7daa338cefbe1badcd7
7
- data.tar.gz: be49041456dea4b2e2d5460333cc6755877a7b2ed006e285db24e289d4a4903e5e3d984b7990feeeaad8533b375e9b2902417971592ff73aad5e1a46bb394d48
6
+ metadata.gz: 48f9ed3cfc7b046bd5aab5b642322e42e86120905f474d76651aae224bad5fb1147024d445e23743de6bec8c66fee46b92bc29c33bbe700dfc20e070c7f2d87d
7
+ data.tar.gz: 74bb4846dab7e79cec7bc43da97f0d64bd4b0013fd18ff29ec328ac6adb657d25f01f1608867c61045694111e741e4b3ef13feaa93ef1e0b886adf318bf84e40
@@ -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 /endpoint/#{param1}/#{param2} --endpoint-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,80 @@
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
- argument :endpoint, type: :string
6
- class_option :method, type: :string, default: :get
7
- class_option :method_params, type: :array
8
6
 
7
+ class_option :endpoint, type: :string, default: '/'
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
11
- @endpoint = endpoint
12
- @endpoint = @endpoint[1..] if @endpoint.first == '/'
13
+ @api_service = file_name.underscore
14
+ @api_version = api_version
15
+ @endpoint_name = endpoint_name
16
+ @endpoint = options[:endpoint]
17
+
18
+ raise ArgumentError, 'Endpoint name required' unless @endpoint_name
19
+ raise ArgumentError, 'Version is required, e.g. V1' unless @api_version
20
+ raise ArgumentError, 'Version should not be included in your endpoint, generator will build it for you' if @endpoint.downcase.include?(@api_version.downcase)
21
+
22
+ @endpoint_reference = @api_service.camelize + '::' + @api_version.camelize + '::ApiEndpoints'
23
+ @full_endpoint_reference = @endpoint_reference + '.' + @endpoint_name.underscore
24
+
25
+ @endpoint = '/' + @endpoint unless @endpoint.first == '/'
26
+ @endpoint = @api_version.downcase + @endpoint
13
27
  @method = options[:method].to_sym
14
28
 
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
29
+ create_endpoint_params if options[:endpoint_params].present?
30
+ create_helper_params if options[:include_helper_params].present?
21
31
 
22
- @api_service = file_name.underscore
23
- service_dir_path = "app/services/#{@api_service}/"
32
+ spec_file_name = @endpoint_name.underscore + '_spec.rb';
33
+ spec_dir = 'spec/services/'
34
+ spec_api_dir = spec_dir + @api_service.underscore + '/'
35
+ service_version_dir = spec_api_dir + @api_version.downcase + '/'
24
36
 
25
- template 'api_call.erb', service_dir_path + "api_calls/#{@api_name.underscore}.rb"
37
+ Dir.mkdir spec_dir unless File.exist?(spec_dir)
38
+ Dir.mkdir spec_api_dir unless File.exist?(spec_api_dir)
39
+ Dir.mkdir service_version_dir unless File.exist?(service_version_dir)
26
40
 
27
- temp = ERB.new <<-'EOF'
41
+ spec_file_path = service_version_dir + spec_file_name
28
42
 
29
- def <%= @api_name.underscore%>_endpoint<%="(#{@method_params})" if @method_params.present?%>
30
- <%= "\"#{@endpoint}\".freeze" if @endpoint.present? %>
31
- end
32
- EOF
43
+ template 'endpoint_spec.erb', spec_file_path
44
+
45
+ service_dir_path = "app/services/#{@api_service}/"
46
+ service_api_version_path = service_dir_path + @api_version + '/'
47
+
48
+ template 'api_call.erb', service_api_version_path + "api_calls/#{@endpoint_name.underscore}.rb"
49
+
50
+ api_call_file = ERB.new load_template('endpoint_helper.erb')
33
51
 
34
- inject_into_file service_dir_path + 'endpoint_helpers.rb', after: " # API Endpoint Helpers\n" do
35
- temp.result(binding)
52
+ inject_into_file service_api_version_path + 'endpoint_helpers.rb', after: " # API Endpoint Helpers\n" do
53
+ api_call_file.result(binding)
54
+ end
55
+
56
+ mixin = ERB.new load_template('mixin.erb')
57
+
58
+ inject_into_file service_api_version_path + 'api_endpoints.rb', after: " # API call modules\n" do
59
+ mixin.result(binding)
36
60
  end
61
+ end
62
+
63
+ private
37
64
 
38
- temp = ERB.new <<-'EOF'
39
- extend <%= @api_name.camelize%>
40
- EOF
65
+ def load_template(template_file)
66
+ template_path = File.join(File.dirname(__FILE__), 'templates/' + template_file)
67
+ File.read(template_path)
68
+ end
69
+
70
+ def create_helper_params
71
+ @helper_params = @endpoint_params
72
+ end
41
73
 
42
- inject_into_file service_dir_path + 'api_endpoints.rb', after: " # API call modules\n" do
43
- temp.result(binding)
74
+ def create_endpoint_params
75
+ @endpoint_params = options[:endpoint_params][0..-2].each_with_object('') do |p, str|
76
+ str << p.underscore << ', '
44
77
  end
78
+ @endpoint_params << options[:endpoint_params].last
45
79
  end
46
80
  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,29 @@
1
1
  class ServiceGenerator < Rails::Generators::NamedBase
2
2
  source_root File.expand_path('templates', __dir__)
3
3
 
4
- argument :api_endpoint, type: :string
4
+ argument :version, type: :string
5
+ class_option :api_endpoint, type: :string, default: 'https://'
5
6
  class_option :key, type: :array
6
7
 
7
8
  def create_api_service
8
9
  @api_name = file_name
9
- @api_endpoint = api_endpoint
10
+ @api_version = version
11
+ @api_endpoint = options[: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
+
16
+ @full_api_reference = @api_name.camelize + '::' + @api_version.upcase
17
+ @api_endpoint = @api_endpoint + '/' unless @api_endpoint.last == '/'
14
18
 
15
19
  root_dir = "app/services/"
16
20
  Dir.mkdir root_dir unless File.exist?(root_dir)
17
21
 
18
- service_dir_path = "#{root_dir}#{@api_dir_name}/"
22
+ api_path = root_dir + @api_name.underscore + '/'
23
+
24
+ Dir.mkdir api_path unless File.exist?(api_path)
25
+
26
+ service_dir_path = api_path + @api_version.downcase + '/'
19
27
 
20
28
  Dir.mkdir service_dir_path unless File.exist?(service_dir_path)
21
29
  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.6
4
+ version: 0.2.2
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