class SP::Duh::JSONAPI::Configuration
Constants
- CONFIGURATION_TABLE_NAME
- DEFAULT_SETTINGS_FILE
Public Class Methods
add_publisher(publisher)
click to toggle source
# File lib/sp/duh/jsonapi/configuration.rb, line 36 def self.add_publisher(publisher) begin publisher = publisher.constantize if publisher.is_a?(String) raise Exceptions::InvalidResourcePublisherError.new(publisher: publisher.name) if !publisher.include?(ResourcePublisher) @@publishers << publisher rescue StandardError => e raise Exceptions::InvalidResourcePublisherError.new(publisher: publisher.is_a?(String) ? publisher : publisher.name) end end
new(pg_connection, url)
click to toggle source
# File lib/sp/duh/jsonapi/configuration.rb, line 31 def initialize(pg_connection, url) @pg_connection = pg_connection @url = url end
Public Instance Methods
connection()
click to toggle source
# File lib/sp/duh/jsonapi/configuration.rb, line 26 def connection ; @pg_connection ; end
exists?()
click to toggle source
# File lib/sp/duh/jsonapi/configuration.rb, line 54 def exists? check = connection.exec %Q[ SELECT COUNT(*) FROM #{Configuration::CONFIGURATION_TABLE_NAME} WHERE prefix = '#{url}' ] return check.first.values.first.to_i > 0 end
load_from_database()
click to toggle source
# File lib/sp/duh/jsonapi/configuration.rb, line 59 def load_from_database @resources = [] @settings = {} configuration = connection.exec %Q[ SELECT config FROM #{Configuration::CONFIGURATION_TABLE_NAME} WHERE prefix = '#{url}' ] if configuration.first configuration = JSON.parse(configuration.first['config']) @resources = configuration['resources'] @settings = configuration.reject { |k,v| k == 'resources' } end @resources end
load_from_publishers(replace = false)
click to toggle source
# File lib/sp/duh/jsonapi/configuration.rb, line 71 def load_from_publishers(replace = false) @resources = [] @settings = {} @@publishers.each do |publisher| add_resources_from_folder(publisher.jsonapi_resources_root, replace) end @resources end
load_settings_from_file(file_name)
click to toggle source
# File lib/sp/duh/jsonapi/configuration.rb, line 102 def load_settings_from_file(file_name) @settings = YAML.load_file(file_name) end
publishers()
click to toggle source
# File lib/sp/duh/jsonapi/configuration.rb, line 29 def publishers ; @@publishers || [] ; end
reload!()
click to toggle source
# File lib/sp/duh/jsonapi/configuration.rb, line 96 def reload! load_from_publishers(true) save @resources end
resource_names()
click to toggle source
# File lib/sp/duh/jsonapi/configuration.rb, line 25 def resource_names ; resources.map { |r| r.keys.first } ; end
resources()
click to toggle source
# File lib/sp/duh/jsonapi/configuration.rb, line 24 def resources ; @resources || [] ; end
save()
click to toggle source
# File lib/sp/duh/jsonapi/configuration.rb, line 80 def save begin if exists? connection.exec %Q[ UPDATE #{Configuration::CONFIGURATION_TABLE_NAME} SET config='#{definition.to_json}' WHERE prefix='#{url}'; ] else connection.exec %Q[ INSERT INTO #{Configuration::CONFIGURATION_TABLE_NAME} (prefix, config) VALUES ('#{url}','#{definition.to_json}'); ] end rescue StandardError => e raise Exceptions::SaveConfigurationError.new(nil, e) end end
settings()
click to toggle source
# File lib/sp/duh/jsonapi/configuration.rb, line 12 def settings @settings ||= {} if @settings.blank? load_settings_from_file(File.join(SP::Duh.root, DEFAULT_SETTINGS_FILE)) end @settings end
settings=(hash)
click to toggle source
# File lib/sp/duh/jsonapi/configuration.rb, line 20 def settings=(hash) @settings = hash end
setup()
click to toggle source
# File lib/sp/duh/jsonapi/configuration.rb, line 46 def setup begin create_jsonapi_configuration_store() rescue StandardError => e raise Exceptions::GenericServiceError.new(e) end end
url()
click to toggle source
# File lib/sp/duh/jsonapi/configuration.rb, line 27 def url ; @url ; end
Private Instance Methods
add_resource(resource, configuration_file, replace)
click to toggle source
# File lib/sp/duh/jsonapi/configuration.rb, line 148 def add_resource(resource, configuration_file, replace) raise Exceptions::InvalidResourceConfigurationError.new(file: configuration_file) if (resource.keys.count != 1) resource_name = resource.keys[0] _log "Processing resource #{resource_name}", "JSONAPI::Configuration" processed = false @resources.each_with_index do |r, i| if r.keys.include?(resource_name) raise Exceptions::DuplicateResourceError.new(name: resource_name) if !replace @resources[i] = resource processed = true break end end @resources << resource if !processed end
add_resources_from_file(configuration_file, replace)
click to toggle source
# File lib/sp/duh/jsonapi/configuration.rb, line 134 def add_resources_from_file(configuration_file, replace) _log "Processing resources from file #{configuration_file}", "JSONAPI::Configuration" configuration = YAML.load_file(configuration_file) if configuration.is_a? Hash add_resource(configuration, configuration_file, replace) else if configuration.is_a? Array configuration.each { |resource| add_resource(resource, configuration_file, replace) } else raise Exceptions::InvalidResourceConfigurationError.new(file: configuration_file) end end end
add_resources_from_folder(folder_name, replace)
click to toggle source
# File lib/sp/duh/jsonapi/configuration.rb, line 121 def add_resources_from_folder(folder_name, replace) @resources ||= [] # First load resources at the root folder Dir.glob(File.join(folder_name, '*.yml')) do |configuration_file| add_resources_from_file(configuration_file, replace) end # Then load resources at the inner folders Dir.glob(File.join(folder_name, '*', '*.yml')) do |configuration_file| add_resources_from_file(configuration_file, replace) end @resources end
create_jsonapi_configuration_store()
click to toggle source
# File lib/sp/duh/jsonapi/configuration.rb, line 108 def create_jsonapi_configuration_store connection.exec %Q[ CREATE TABLE IF NOT EXISTS #{Configuration::CONFIGURATION_TABLE_NAME} ( prefix varchar(64) PRIMARY KEY, config text NOT NULL ); ] end
definition()
click to toggle source
# File lib/sp/duh/jsonapi/configuration.rb, line 117 def definition settings.merge(resources: resources) end