red-api 0.1.5

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 34529a5a138b7f837a1e5242eac993f5072eaa897a598d91fc2f4e5d150c11db
4
+ data.tar.gz: e69e843fd93e86b95f775c9f1d7335f36cd77375e0d79111aa026f09c370c4dc
5
+ SHA512:
6
+ metadata.gz: 61977f5fef8591ec94c0a88f9064eecf3878442986bbf3cd41626ac11859657d695f37ba7a16b1ef0d384ed14c45f19e1e4ef033213ee508392429e5bb429ebd
7
+ data.tar.gz: 8ad5a8f28a14e28ff2af2e6496b4542eccefbad7c40f7503ba0af60e3028d14e36b718c60d0e9c46b8cc1d81c877bb4e294a79cb5468b06642b253e86569944d
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ ### RED API Generators
2
+ This simple gem has two generators to support creating a basic API consumer framework for your Rails Applications.
3
+
4
+ #### Generators
5
+ - red-api:service
6
+ - red-api:endpoint
7
+
8
+ #### Usage
9
+ ```sh
10
+ rails generate service ApiName https://service/endpoint/ --key api_key service_api_key_env_name
11
+ rails generate endpoint ServiceName EndpointName /endpoint/#{param1}/#{param2} --method_params param1 param2
12
+ ```
13
+ The service generator will setup the framework which uses Faraday for your http calls and Oj gem for faster json parsing.
14
+ The endpoint generator will setup an endpoint with an endpoint helper method for easier testing.
15
+
16
+ #### Installation
17
+ ```ruby
18
+ gem 'red-api'
19
+ ```
20
+ #### Dependancies
21
+ ```ruby
22
+ gem 'faraday'
23
+ gem 'oj'
24
+ ```
25
+
26
+ #### Testing
27
+ The generator still needs tests and this is my first generator and I'm still learning how to write tests. If you wish to contribute before I get a chance to add tests please PR with tests and I'll review it. Until then the gem won't be published until tests are written.
@@ -0,0 +1,8 @@
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)
3
+
4
+ Example:
5
+ rails generate endpoint ServiceName EndpointName /endpoint/#{param1}/#{param2} --method_params param1 param2
6
+
7
+ This will create:
8
+ app/services/ServiceName/api_call/endpoint_name.rb
@@ -0,0 +1,46 @@
1
+ class EndpointGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path('templates', __dir__)
3
+
4
+ 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
+
9
+ def create_api_call
10
+ @api_name = endpoint_name
11
+ @endpoint = endpoint
12
+ @endpoint = @endpoint[1..] if @endpoint.first == '/'
13
+ @method = options[:method].to_sym
14
+
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
21
+
22
+ @api_service = file_name.underscore
23
+ service_dir_path = "app/services/#{@api_service}/"
24
+
25
+ template 'api_call.erb', service_dir_path + "api_calls/#{@api_name.underscore}.rb"
26
+
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
33
+
34
+ inject_into_file service_dir_path + 'endpoint_helpers.rb', after: " # API Endpoint Helpers\n" do
35
+ temp.result(binding)
36
+ end
37
+
38
+ temp = ERB.new <<-'EOF'
39
+ extend <%= @api_name.camelize%>
40
+ EOF
41
+
42
+ inject_into_file service_dir_path + 'api_endpoints.rb', after: " # API call modules\n" do
43
+ temp.result(binding)
44
+ end
45
+ end
46
+ end
@@ -0,0 +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: {}
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generates the framework for a basic API service your app consumes
3
+
4
+ Example:
5
+ rails generate service ServiceName https://service/api/endpoint/ --key api_key env_key_name
6
+
7
+ This will create:
8
+ app/services/ServiceName
@@ -0,0 +1,27 @@
1
+ class ServiceGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path('templates', __dir__)
3
+
4
+ argument :api_endpoint, type: :string
5
+ class_option :key, type: :array
6
+
7
+ def create_api_service
8
+ @api_name = file_name
9
+ @api_endpoint = api_endpoint
10
+ @env_var_opts = options[:key]
11
+ raise 'Need to define the name of your API service with --api' unless @api_name.present?
12
+
13
+ @api_dir_name = @api_name.underscore
14
+
15
+ root_dir = "app/services/"
16
+ Dir.mkdir root_dir unless File.exist?(root_dir)
17
+
18
+ service_dir_path = "#{root_dir}#{@api_dir_name}/"
19
+
20
+ Dir.mkdir service_dir_path unless File.exist?(service_dir_path)
21
+ Dir.mkdir "#{service_dir_path}/api_calls" unless File.exist?("#{service_dir_path}/api_calls")
22
+
23
+ template 'api_endpoints.erb', service_dir_path + 'api_endpoints.rb'
24
+ template 'client.erb', service_dir_path + 'client.rb'
25
+ template 'endpoint_helpers.erb', service_dir_path + 'endpoint_helpers.rb'
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ Dir[Rails.root.join('app/services/<%= @api_dir_name%>/api_calls/*.rb')].each { |api_call| require api_call }
2
+
3
+ class <%= @api_name.camelize%>::ApiEndpoints < <%= @api_name.camelize%>::Client
4
+ # API call modules
5
+ end
@@ -0,0 +1,35 @@
1
+ require_relative 'endpoint_helpers'
2
+
3
+ class <%= @api_name.camelize%>::Client
4
+ extend EndpointHelpers
5
+
6
+ def self.client(params)
7
+ <% if @env_var_opts.present? %><%= "params[:#{@env_var_opts.first.underscore.downcase}] = ENV['#{@env_var_opts.last.underscore.upcase}']"%><% else %># Additional params can be added here<% end %>
8
+ Faraday.new(
9
+ url: api_endpoint,
10
+ params: params
11
+ )
12
+ end
13
+
14
+ def self.request(method = :get, endpoint = nil, params: {})
15
+ raise 'API endpoint must be defined' if endpoint.nil?
16
+
17
+ connection = client(params)
18
+
19
+ @response = connection.send(method, endpoint)
20
+
21
+ return parse_json if response_successful?
22
+
23
+ raise "Status: #{@response.status}, Response: #{@response.body}"
24
+ end
25
+
26
+ def self.parse_json
27
+ Oj.load(@response.body, symbol_keys: true)
28
+ end
29
+
30
+ def self.response_successful?
31
+ @response.status == 200
32
+ end
33
+
34
+ private_class_method :parse_json, :response_successful?
35
+ end
@@ -0,0 +1,8 @@
1
+ module EndpointHelpers
2
+ def api_endpoint
3
+ # The http API endpoint for this service
4
+ <%= "'#{@api_endpoint}'.freeze" if @api_endpoint.present? %>
5
+ end
6
+
7
+ # API Endpoint Helpers
8
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: red-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.5
5
+ platform: ruby
6
+ authors:
7
+ - Richard DeSilvey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-07-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: oj
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.12.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 3.12.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.5.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.5.1
55
+ description: Generate API service and endpoints for API consumption
56
+ email:
57
+ - rdesilvey@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - README.md
63
+ - lib/generators/endpoint/USAGE
64
+ - lib/generators/endpoint/endpoint_generator.rb
65
+ - lib/generators/endpoint/templates/api_call.erb
66
+ - lib/generators/service/USAGE
67
+ - lib/generators/service/service_generator.rb
68
+ - lib/generators/service/templates/api_endpoints.erb
69
+ - lib/generators/service/templates/client.erb
70
+ - lib/generators/service/templates/endpoint_helpers.erb
71
+ homepage: https://github.com/redferret/red-api
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubygems_version: 3.1.4
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: API service and endpoint generators for Rails
94
+ test_files: []