red-api 0.1.6 → 0.1.9

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: 46c471a9c123c2bcd52de3e2459f7d282b83172a2c75f54fd4240e9931a80a54
4
+ data.tar.gz: 3202e5fdff5882c142d9173e593254c09767c67e9aac33765905f4b5eba44274
5
5
  SHA512:
6
- metadata.gz: 1dc400ed3600883b143b7be98621626b3d2cb9c501e0b4e6d0ff4275b91d6d3f9abc8cf478391539a75299d1d8b11702ed9387b830b2d7daa338cefbe1badcd7
7
- data.tar.gz: be49041456dea4b2e2d5460333cc6755877a7b2ed006e285db24e289d4a4903e5e3d984b7990feeeaad8533b375e9b2902417971592ff73aad5e1a46bb394d48
6
+ metadata.gz: 12c9cb3aff98022cc14e545b56dc2388df0ae536bb818b0779dbaa6b6f8de7a8f4260f7ebb61e9dbb6cbab657949988618b3755ff60515f844a543049b3374c9
7
+ data.tar.gz: 8ca0ef18944bbf57e5ec00c9a05d81a3ca8c5a3ea1ecb5fffdc4602066e4959371953b95a3d31ab30030760965d97164b63f05fbae2f24ade06936707533616e
@@ -1,8 +1,15 @@
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
4
  Example:
5
- rails generate endpoint ServiceName EndpointName /endpoint/#{param1}/#{param2} --method_params param1 param2
5
+ rails generate endpoint ServiceName version EndpointName /endpoint/#{param1}/#{param2} --method-params param1 param2 --helper-params param1 param2
6
+
7
+ Simple Form:
8
+ rails generate endpoint ServiceName V1 EndpointName /endpoint
6
9
 
7
10
  This will create:
8
- app/services/ServiceName/api_call/endpoint_name.rb
11
+ app/services/ServiceName/version/api_call/endpoint_name.rb
12
+
13
+ This will modify:
14
+ app/services/ServiceName/version/api_endpoints.rb
15
+ app/services/ServiceName/version/endpoint_helpers.rb
@@ -2,45 +2,87 @@ class EndpointGenerator < Rails::Generators::NamedBase
2
2
  source_root File.expand_path('templates', __dir__)
3
3
 
4
4
  argument :endpoint_name, type: :string
5
+ argument :api_version, 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 :helper_params, type: :array
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 == '/'
13
- @method = options[:method].to_sym
14
17
 
15
- if options[:method_params].present?
16
- @method_params = options[:method_params][0..-2].each_with_object('') do |p, str|
17
- str << p << ', '
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 == '/'
25
+ @method = options[:method].to_sym unless options[:method]
26
+
27
+ create_endpoint_params unless options[:endpoint_params]
28
+ create_helper_params unless options[:helper_params]
29
+
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 + '/'
34
+
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)
38
+
39
+ spec_file_path = service_version_dir + spec_file_name
40
+
41
+ unless File.exist? spec_file_path
42
+ template 'endpoint_spec.erb', spec_file_path
43
+ else
44
+ end_point_test = ERB.new load_template('endpoint_test.erb')
45
+ inject_into_file spec_file_path, after: " # API Endpoint tests\n" do
46
+ end_point_test.result(binding)
18
47
  end
19
- @method_params << options[:method_params].last
20
48
  end
21
49
 
22
- @api_service = file_name.underscore
23
50
  service_dir_path = "app/services/#{@api_service}/"
51
+ service_api_version_path = service_dir_path + @api_version + '/'
24
52
 
25
- template 'api_call.erb', service_dir_path + "api_calls/#{@api_name.underscore}.rb"
53
+ template 'api_call.erb', service_api_version_path + "api_calls/#{@endpoint_name.underscore}.rb"
26
54
 
27
- temp = ERB.new <<-'EOF'
28
-
29
- def <%= @api_name.underscore%>_endpoint<%="(#{@method_params})" if @method_params.present?%>
30
- <%= "\"#{@endpoint}\".freeze" if @endpoint.present? %>
31
- end
32
- EOF
55
+ api_call_file = ERB.new load_template('endpoint_helper.erb')
33
56
 
34
- inject_into_file service_dir_path + 'endpoint_helpers.rb', after: " # API Endpoint Helpers\n" do
35
- temp.result(binding)
57
+ inject_into_file service_api_version_path + 'endpoint_helpers.rb', after: " # API Endpoint Helpers\n" do
58
+ api_call_file.result(binding)
59
+ end
60
+
61
+ mixin = ERB.new load_template('mixin.erb')
62
+
63
+ inject_into_file service_api_version_path + 'api_endpoints.rb', after: " # API call modules\n" do
64
+ mixin.result(binding)
36
65
  end
66
+ end
37
67
 
38
- temp = ERB.new <<-'EOF'
39
- extend <%= @api_name.camelize%>
40
- EOF
68
+ private
69
+
70
+ def load_template(template_file)
71
+ template_path = File.join(File.dirname(__FILE__), 'templates/' + template_file)
72
+ File.read(template_path)
73
+ end
74
+
75
+ def create_helper_params
76
+ @endpoint_params = options[:helper_params][0..-2].each_with_object('') do |p, str|
77
+ str << p << ', '
78
+ end
79
+ @endpoint_params << options[:helper_params].last
80
+ end
41
81
 
42
- inject_into_file service_dir_path + 'api_endpoints.rb', after: " # API call modules\n" do
43
- temp.result(binding)
82
+ def create_endpoint_params
83
+ @endpoint_params = options[:endpoint_params][0..-2].each_with_object('') do |p, str|
84
+ str << p << ', '
44
85
  end
86
+ @endpoint_params << options[:endpoint_params].last
45
87
  end
46
88
  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 @endpoint_params.present?%>
3
+ <%= "\"#{@endpoint}\".freeze" if @endpoint.present? %>
4
+ end
@@ -0,0 +1,5 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe <%=@endpoint_reference%> do
4
+ # API Endpoint tests
5
+ end
@@ -0,0 +1,9 @@
1
+
2
+ describe '#<%=@endpoint_name.underscore%>' do
3
+ it 'returns expected data' do
4
+ skip 'Write tests for <%=@endpoint_name.underscore%>'
5
+
6
+ <%='# Define endpoint parameters here' unless @endpoint_params%>
7
+ response = <%=@full_endpoint_reference%><%="(#{@endpoint_params})" unless @endpoint_params%>
8
+ end
9
+ 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_version_path = root_dir + @api_version.downcase + '/'
24
+
25
+ Dir.mkdir api_version_path unless File.exist?(api_version_path)
26
+
27
+ service_dir_path = api_version_path + api_name.underscore + '/'
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_dir_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.1.9
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-19 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,10 @@ 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/endpoint_test.erb
83
+ - lib/generators/endpoint/templates/mixin.erb
66
84
  - lib/generators/service/USAGE
67
85
  - lib/generators/service/service_generator.rb
68
86
  - lib/generators/service/templates/api_endpoints.erb