Class: RestDSL::ServiceBase
- Inherits:
-
Object
- Object
- RestDSL::ServiceBase
- Defined in:
- lib/rest_dsl/service_base.rb
Overview
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.
Class Attribute Summary collapse
-
.authentication ⇒ Object
writeonly
Sets the attribute authentication.
-
.client ⇒ Object
readonly
Returns the value of attribute client.
-
.config_file ⇒ Object
Returns the value of attribute config_file.
-
.environment ⇒ Object
Returns the value of attribute environment.
-
.file_name ⇒ Object
readonly
Returns the value of attribute file_name.
- .headers ⇒ Object
-
.last_response ⇒ Object
readonly
Returns the value of attribute last_response.
-
.service_name ⇒ Object
writeonly
Sets the attribute service_name.
Class Method Summary collapse
- .auth ⇒ Object
- .build_params(params) ⇒ Object
- .config ⇒ Object
-
.execute_request(method, rest_method_call, *args, headers: nil, payload: nil, params: nil, url_args: nil, form_data: nil, text: nil, **hash_args, &block) ⇒ Object
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.
- .inherited(clazz) ⇒ Object
- .reload_config ⇒ Object
- .rest_call(method, name, url_schema) ⇒ Object
- .sub_url_args!(arg_list, rest_method_call) ⇒ Object
Instance Method Summary collapse
Class Attribute Details
.authentication=(value) ⇒ Object (writeonly)
Sets the attribute authentication
21 22 23 |
# File 'lib/rest_dsl/service_base.rb', line 21 def authentication=(value) @authentication = value end |
.client ⇒ Object (readonly)
Returns the value of attribute client.
20 21 22 |
# File 'lib/rest_dsl/service_base.rb', line 20 def client @client end |
.config_file ⇒ Object
Returns the value of attribute config_file.
20 21 22 |
# File 'lib/rest_dsl/service_base.rb', line 20 def config_file @config_file end |
.environment ⇒ Object
Returns the value of attribute environment.
20 21 22 |
# File 'lib/rest_dsl/service_base.rb', line 20 def environment @environment end |
.file_name ⇒ Object (readonly)
Returns the value of attribute file_name.
20 21 22 |
# File 'lib/rest_dsl/service_base.rb', line 20 def file_name @file_name end |
.headers ⇒ Object
83 84 85 |
# File 'lib/rest_dsl/service_base.rb', line 83 def headers @headers ||= config&.[](:headers) || {} end |
.last_response ⇒ Object (readonly)
Returns the value of attribute last_response.
20 21 22 |
# File 'lib/rest_dsl/service_base.rb', line 20 def last_response @last_response end |
.service_name=(value) ⇒ Object (writeonly)
Sets the attribute service_name
21 22 23 |
# File 'lib/rest_dsl/service_base.rb', line 21 def service_name=(value) @service_name = value end |
Class Method Details
.auth ⇒ Object
79 80 81 |
# File 'lib/rest_dsl/service_base.rb', line 79 def auth @authentication || config&.[](:credentials) || {} end |
.build_params(params) ⇒ Object
53 54 55 56 57 |
# 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 ⇒ Object
64 65 66 |
# File 'lib/rest_dsl/service_base.rb', line 64 def config @config || reload_config 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) ⇒ Object
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
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# 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 |
.inherited(clazz) ⇒ Object
12 13 14 15 16 17 |
# 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 ⇒ Object
73 74 75 76 77 |
# 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) ⇒ Object
30 31 32 33 34 |
# 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) ⇒ Object
59 60 61 62 |
# 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 |
Instance Method Details
#service_name ⇒ Object
88 89 90 |
# File 'lib/rest_dsl/service_base.rb', line 88 def service_name self.class.instance_variable_get(:@service_name) end |