Class: RestDSL::ServiceBase

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.authentication=(value) ⇒ Object (writeonly)

Sets the attribute authentication

Parameters:

  • value

    the value to set the attribute authentication to.



21
22
23
# File 'lib/rest_dsl/service_base.rb', line 21

def authentication=(value)
  @authentication = value
end

.clientObject (readonly)

Returns the value of attribute client.



20
21
22
# File 'lib/rest_dsl/service_base.rb', line 20

def client
  @client
end

.config_fileObject

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

.environmentObject

Returns the value of attribute environment.



20
21
22
# File 'lib/rest_dsl/service_base.rb', line 20

def environment
  @environment
end

.file_nameObject (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

.headersObject



83
84
85
# File 'lib/rest_dsl/service_base.rb', line 83

def headers
  @headers ||= config&.[](:headers) || {}
end

.last_responseObject (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

Parameters:

  • value

    the value to set the attribute service_name to.



21
22
23
# File 'lib/rest_dsl/service_base.rb', line 21

def service_name=(value)
  @service_name = value
end

Class Method Details

.authObject



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

.configObject



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_configObject



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_nameObject



88
89
90
# File 'lib/rest_dsl/service_base.rb', line 88

def service_name
  self.class.instance_variable_get(:@service_name)
end