class RestDSL::ServiceBase
Base Class for defining a rest service DSL Object. Will look in a config file contained in the same directory as the service defining its headers and/or creds by environment. This file should be named such that it matches the service file name with a .yml ending.
Attributes
authentication[W]
client[R]
config_file[R]
environment[R]
file_name[R]
headers[W]
last_response[R]
service_name[W]
Public Class Methods
auth()
click to toggle source
# File lib/rest_dsl/service_base.rb, line 79 def auth @authentication || config&.[](:credentials) || {} end
build_params(params)
click to toggle source
# File lib/rest_dsl/service_base.rb, line 53 def build_params(params) params ||= {} return "" if params.empty? "?#{params.map{|key,value| "#{key}=#{value}"}.join('&')}" unless params.empty? end
config()
click to toggle source
# File lib/rest_dsl/service_base.rb, line 64 def config @config || reload_config end
config_file=(file_name)
click to toggle source
# File lib/rest_dsl/service_base.rb, line 68 def config_file=(file_name) @config_file = file_name reload_config end
environment=(environment)
click to toggle source
Initializes the singleton
# File lib/rest_dsl/service_base.rb, line 24 def environment=(environment) @environment = environment @client = Client.new(environment) self end
execute_request(method, rest_method_call, *args, headers: nil, payload: nil, params: nil, url_args: nil, form_data: nil, text: nil, **hash_args, &block)
click to toggle source
The method wrapped by the methods generated by rest_call
, these methods all follow this blueprint Can by wrapped manually to create more complicated logic than what's supported by the default generators
# File lib/rest_dsl/service_base.rb, line 38 def execute_request(method, rest_method_call, *args, headers: nil, payload: nil, params: nil, url_args: nil, form_data: nil, text: nil, **hash_args, &block) headers ||= self.headers url_args ||= {} service_name = "#{@service_name}/" unless @service_name&.empty? hash_args.merge!(auth) hash_args.merge!(payload: payload.to_json) if payload hash_args.merge!(payload: form_data) if form_data hash_args.merge!(payload: text) if text sub_url_args!(url_args, rest_method_call) arg_list = [method, "#{service_name}#{rest_method_call}#{build_params(params)}", headers] response = @client.execute(*arg_list, *args, **hash_args, &block) @last_response = response[:response] response[:parsed] end
headers()
click to toggle source
# File lib/rest_dsl/service_base.rb, line 83 def headers @headers ||= config&.[](:headers) || {} end
inherited(clazz)
click to toggle source
# File lib/rest_dsl/service_base.rb, line 12 def self.inherited(clazz) clazz.class_eval do @file_name = caller_locations[2].path @config_file = @file_name.gsub('.rb', '.yml') end end
reload_config()
click to toggle source
# File lib/rest_dsl/service_base.rb, line 73 def reload_config @config = Psych.load_file(@config_file)[@environment] if File.exist?(@config_file) @config = {} unless File.exist?(@config_file) @config end
rest_call(method, name, url_schema)
click to toggle source
# File lib/rest_dsl/service_base.rb, line 30 def rest_call(method, name, url_schema) self.class.define_method("#{method}_#{name}") do |*args, headers: nil, payload: nil, params: nil, url_args: nil, **hash_args| execute_request(method, url_schema.dup, *args, **hash_args, headers: headers, payload: payload, params: params, url_args: url_args) end end
sub_url_args!(arg_list, rest_method_call)
click to toggle source
# File lib/rest_dsl/service_base.rb, line 59 def sub_url_args!(arg_list, rest_method_call) # Given the protocol is handled by the client and not service_base, this should be a safe enough pattern in most cases arg_list.each{ |arg_name, value| rest_method_call.gsub!(":#{arg_name}", value) } end
Public Instance Methods
service_name()
click to toggle source
# File lib/rest_dsl/service_base.rb, line 88 def service_name self.class.instance_variable_get(:@service_name) end