class Shortorder::Command::CustomService
Attributes
callback_class[R]
callback_name[R]
full_class[R]
full_name[R]
root_class[R]
root_name[R]
service_path[R]
task_class[R]
task_name[R]
Public Instance Methods
execute!()
click to toggle source
Generate custom service scaffold.
# File lib/shortorder/command/custom_service.rb, line 16 def execute! unless(arguments.size == 1) raise ArgumentError.new('Single argument name of service is required!') end @root_name = Bogo::Utility.snake(config[:root].tr('-.', '_')) @root_class = Bogo::Utility.camel(root_name) @task_name = Bogo::Utility.snake(arguments.first.tr('-.', '_')) @task_class = Bogo::Utility.camel(task_name) @callback_name = Bogo::Utility.snake(config[:callback].tr('-.', '_')) @callback_class = Bogo::Utility.camel(callback_name) @full_name = "#{root_name}-#{task_name}".tr('_', '-') @full_class = "#{root_class}::#{task_class}" @service_path = File.join(config[:service_path], full_name) run_action 'Validating service directory' do if(File.directory?(service_path)) ui.puts ui.color('path exists', :yellow) ui.confirm 'Overwrite existing contents of service path' ui.info 'Validating service directory... ', :nonewline end FileUtils.mkdir_p(service_path) nil end run_action 'Generating service directory structure' do FileUtils.mkdir_p(File.join(service_path, 'lib', full_name)) nil end run_action 'Writing service scaffolding files' do write_gemspec! write_version! write_loader! write_callback! write_readme! nil end run_action 'Writing Gemfile for local bundle' do write_gemfile! nil end run_action 'Writing service configuration file' do write_config! nil end ui.info "Created new custom service `#{ui.color(task_name, :green, :bold)}` at #{service_path}" ui.puts " To get started, change directory to: #{ui.color(service_path, :bold)}" ui.puts " Install the bundle: #{ui.color('bundle install', :bold)}" ui.puts " Start the service: #{ui.color('bundle exec jackal -c config.json', :bold)}" end
Protected Instance Methods
write_callback!()
click to toggle source
Write scaffold callback for new service
# File lib/shortorder/command/custom_service.rb, line 129 def write_callback! File.open(File.join(service_path, 'lib', full_name, "#{callback_name}.rb"), 'w+') do |file| file.puts <<-EOF require '#{full_name}' module #{root_class} module #{task_class} class #{callback_class} < Jackal::Callback # Perform any setup tasks required for this callback. This # method is called after the callback is initialized and # prior to any message processing. def setup(*_) end # Determine if the received message is valid for this # callback. If valid, the message will be processed. # # @param message [Carnivore::Message] received message # @return [Truthy, Falsey] # @note: Original scaffold generation always returns true def valid?(message) super do |payload| true end end # Perform a task based on the received message. # # @param message [Carnivore::Message] received message # @return [Object, NilClass] returned value is not used def execute(message) failure_wrap(message) do |payload| info 'Received new message from Short Order!' info "Message payload contents:\\n\#{MultiJson.dump(payload, :pretty => true)}" info 'Setting task as complete and notifying Short Order' job_completed(:#{task_name}, payload, message) end end end end end EOF end end
write_config!()
click to toggle source
Write scaffold configuration file for new service
# File lib/shortorder/command/custom_service.rb, line 199 def write_config! File.open(File.join(service_path, 'config.json'), 'w+') do |file| file.puts MultiJson.dump( Smash.new( :jackal => { :require => [ 'carnivore-http', full_name ] }, root_name => { task_name => { :config => { }, :sources => { :input => { :type => :http_paths, :args => { :port => 9090, :path => "/#{task_name}", :method => :post } }, :output => { :type => :http, :args => { :method => :post, :endpoint => 'https://api.d2o.io/v1/restore', :auto_process => false, :enable_processing => false } } }, :callbacks => [ "#{full_class}::#{callback_class}" ] } } ), :pretty => true ) end end
write_gemfile!()
click to toggle source
Write scaffold Gemfile for new service
# File lib/shortorder/command/custom_service.rb, line 177 def write_gemfile! File.open(File.join(service_path, 'Gemfile'), 'w+') do |file| file.puts <<-EOF source 'https://rubygems.org' gemspec EOF end end
write_gemspec!()
click to toggle source
Write scaffold gemspec for new service
# File lib/shortorder/command/custom_service.rb, line 75 def write_gemspec! File.open(File.join(service_path, "#{full_name}.gemspec"), 'w+') do |file| file.puts <<-EOF $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__)) + '/lib/' require '#{full_name}/version' Gem::Specification.new do |s| s.name = '#{full_name}' s.version = #{full_class}::VERSION.version s.summary = 'Custom Short Order Service' s.author = 'CHANGE ME' s.email = 'changeme@example.com' s.homepage = 'https://example.com' s.description = 'Custom Short Order Service: #{task_name}' s.require_path = 'lib' s.license = 'Apache 2.0' s.add_runtime_dependency 'jackal', '~> 0.3.18' s.add_runtime_dependency 'carnivore-http', '~> 0.2.8' s.files = Dir['{lib}/**/**/*'] + %w(#{full_name}.gemspec) end EOF end end
write_loader!()
click to toggle source
Write scaffold loader for new service
# File lib/shortorder/command/custom_service.rb, line 113 def write_loader! File.open(File.join(service_path, 'lib', "#{full_name}.rb"), 'w+') do |file| file.puts <<-EOF require '#{full_name}/version' require 'jackal' module #{root_class} module #{task_class} autoload :#{callback_class}, '#{full_name}/#{callback_name}' end end EOF end end
write_readme!()
click to toggle source
Write scaffold readme file for new service
# File lib/shortorder/command/custom_service.rb, line 188 def write_readme! File.open(File.join(service_path, 'README.md'), 'w+') do |file| file.puts <<-EOF # Custom Short Order Service: #{task_name} Provide information about #{task_name} here. EOF end end
write_version!()
click to toggle source
Write scaffold version for new service
# File lib/shortorder/command/custom_service.rb, line 99 def write_version! File.open(File.join(service_path, 'lib', full_name, 'version.rb'), 'w+') do |file| file.puts <<-EOF module #{root_class} module #{task_class} # Current version VERSION = Gem::Version.new('0.1.0') end end EOF end end