Module: Restool::Settings::Loader
- Includes:
- Models
- Defined in:
- lib/restool/settings/loader.rb
Constant Summary collapse
- DEFAULT_TIMEOUT =
60- DEFAULT_SSL_VERIFY =
false
Constants included from Models
Class Method Summary collapse
- .build_operation(operation_config, paths_prefix_in_host) ⇒ Object
- .build_operation_response(response) ⇒ Object
- .build_representations(representations) ⇒ Object
- .build_service(service_config) ⇒ Object
- .config ⇒ Object
- .load(service_name) ⇒ Object
- .uri_params(operation_config) ⇒ Object
- .validate ⇒ Object
Class Method Details
.build_operation(operation_config, paths_prefix_in_host) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/restool/settings/loader.rb', line 77 def self.build_operation(operation_config, paths_prefix_in_host) response = build_operation_response(operation_config['response']) if operation_config['response'] path = operation_config['path'] path = path[1..-1] if path[0] == '/' paths_prefix_in_host.chomp!('/') Operation.new( operation_config['name'], "#{paths_prefix_in_host}/#{path}", operation_config['method'], uri_params(operation_config), response ) end |
.build_operation_response(response) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/restool/settings/loader.rb', line 93 def self.build_operation_response(response) response_fields = response.map do |field| OperationResponsField.new(field['key'], field['metonym'], field['type'].to_sym) end OperationResponse.new(response_fields) end |
.build_representations(representations) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/restool/settings/loader.rb', line 60 def self.build_representations(representations) representations_by_name = {} representations.each do |representation| fields = representation[1].map do |field| RepresentationField.new(field['key'], field['metonym'], field['type'].to_sym) end representation = Representation.new(name = representation.first, fields) representations_by_name[representation.name.to_sym] = representation end representations_by_name end |
.build_service(service_config) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/restool/settings/loader.rb', line 26 def self.build_service(service_config) representations = if service_config['representations'] build_representations(service_config['representations']) else [] end basic_auth = service_config['basic_auth'] || service_config['basic_authentication'] basic_auth = BasicAuthentication.new(basic_auth['user'], basic_auth['password']) if basic_auth persistent_connection = service_config['persistent'] persistent_connection = if persistent_connection PersistentConnection.new( persistent_connection['pool_size'], persistent_connection['warn_timeout'], persistent_connection['force_retry'], ) end # Support host + common path in url config, e.g. api.com/v2/ paths_prefix_in_host = URI(service_config['url']).path Models::Service.new( service_config['name'], service_config['url'], service_config['operations'].map { |operation| build_operation(operation, paths_prefix_in_host) }, persistent_connection, service_config['timeout'] || DEFAULT_TIMEOUT, representations, basic_auth, service_config['ssl_verify'] || DEFAULT_SSL_VERIFY ) end |
.config ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/restool/settings/loader.rb', line 105 def self.config return @config if @config files_to_load = Dir['config/restool/*'] + ['config/restool.yml', 'config/restool.json'] @config = { 'services' => [] } files_to_load.each do |file_name| next unless File.exist?(file_name) extension = File.extname(file_name) content = if extension == '.yml' YAML.load_file(file_name) elsif extension == '.json' json_file = File.read(file_name) JSON.parse(json_file) end @config['services'] += content['services'] end @config end |
.load(service_name) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/restool/settings/loader.rb', line 14 def self.load(service_name) service_config = config['services'].detect do |service| service['name'] == service_name end raise "Service #{service_name} not found in configuration" unless service_config build_service(service_config) end |
.uri_params(operation_config) ⇒ Object
101 102 103 |
# File 'lib/restool/settings/loader.rb', line 101 def self.uri_params(operation_config) operation_config['path'].scan(/:[a-zA-Z_]+[0-9]*[a-zA-Z_]*/) end |
.validate ⇒ Object
130 131 132 |
# File 'lib/restool/settings/loader.rb', line 130 def self.validate # TODO: perform validations end |