module DB
DB
is a Wire::App
for generating REST wrappers for DataMapper @author Bryan T. Meyers
Public Class Methods
Model-specific configuration @param [Hash] conf the existing configuration @return [Hash] post-processed configuration
# File lib/app/db.rb, line 27 def self.configure(conf) conf['models'].each do |m, k| conf['models'][m] = Object.const_get(k) end conf end
Add a new object to the DB
table @param [Hash] context the context for this request @return [Response] a valid Rack response triplet, or status code
# File lib/app/db.rb, line 57 def self.do_create(context) model = context.config['models'][context.resource] return 404 unless model file = context.json[:file] if file if file[:mime].eql? 'text/csv' file[:content].match(/.*base64,(.*)/) do csv = Base64.decode64($1) columns = [] errors = [] csv.split("\n").each_with_index do |v, i| if i == 0 columns = v.split(/(?<!\[),(?!=\])/) columns.map! { |c| c.delete('"').to_sym } else values = v.split(',') values.map! do |c| c.include?(';') ? c.split(';') : c end hash = {} columns.each_with_index do |c, j| if values[j].is_a? String values[j].delete!('"') end hash[c] = values[j] end m = model.find_or_create(hash) unless m.modified? errors << "row: #{i} errors: #{m.errors.delete("\n")}" end end end if errors.length > 0 [400, nil, errors] else 200 end end else 415 end else begin instance = model.create(context.json) if instance.exists? 200 else errors = '' instance.errors.each { |e| errors += "#{e.to_s}\n" } [504, {}, errors] end rescue => e [500, {}, e.message] end end end
Remove a specific object from the DB
table @param [Hash] context the context for this request @return [Response] an object, or status code
# File lib/app/db.rb, line 161 def self.do_delete(context) model = context.config['models'][context.resource] return 404 unless model instance = model[context.id] if instance instance = instance.destroy if instance.errors.length == 0 200 else [500, {}, 'Failed to delete instance'] end else 404 end end
Get a specific object from the DB
table @param [Hash] context the context for this request @return [Response] an object, or status code
# File lib/app/db.rb, line 134 def self.do_read(context) model = context.config['models'][context.resource] return 404 unless model id = context.id if id.eql?('new') or id.eql? 'upload' return '{}' end object = model[id] return 404 unless object [200, {}, object.to_json] end
Get all objects from the DB
table @param [Hash] context the context for this request @return [Response] all objects, or status code
# File lib/app/db.rb, line 118 def self.do_read_all(context) model = context.config['models'][context.resource] return 404 unless model hash = '[ ' model.each do |e| hash << e.to_json hash << ',' end hash = hash[0...-1] hash << ']' [200, {}, hash] end
Update a specific object in the DB
table @param [Hash] context the context for this request @return [Response] an object, or status code
# File lib/app/db.rb, line 149 def self.do_update(context) model = context.config['models'][context.resource] return 404 unless model instance = model[context.id] return 404 unless instance instance.update(context.json) [200, {}, instance.to_json] end
DB-specific configuration @param [Hash] conf the existing configuration @return [Hash] post-processed configuration
# File lib/app/db.rb, line 37 def self.init_db(conf) config = {} conf.each do |k,v| config[k.to_sym] = v end db = Sequel.connect(config) db.extension(:connection_validator) db.pool.connection_validation_timeout = 30 db end
Proxy method used when routing @param [Array] actions the allowed actions for this URI @param [Hash] context the context for this request @return [Response] a Rack Response triplet, or status code
# File lib/app/db.rb, line 181 def self.invoke(actions, context) case context.action when :create do_create(context) when :read if context.uri[3] do_read(context) else do_read_all(context) end when :update do_update(context) when :delete do_delete(context) else 403 end end
Read all of the configs in './config/dbs' @return [void]
# File lib/app/db.rb, line 50 def self.read_configs Wire::Config.read_config_dir('./config/dbs', method(:init_db)) end