Module: DbAgile::Restful::Middleware::Utils

Includes:
IO
Included in:
DbAgile::Restful::Middleware, OneDatabase
Defined in:
lib/dbagile/restful/middleware/utils.rb

Constant Summary

Constants included from IO

IO::EXTENSION_TO_FORMAT, IO::FORMAT_TO_CONTENT_TYPE, IO::FORMAT_TO_MODULE, IO::KNOWN_FORMATS, IO::KNOWN_FROM_FORMATS, IO::KNOWN_TO_FORMATS, IO::ROUND_TRIP_OPTIONS

Instance Method Summary collapse

Methods included from IO

#known_extension?, #known_format?, #roundtrip_options

Instance Method Details

#_200_(env, type, result) ⇒ Object

Returns a 200 response for a given format



37
38
39
40
41
42
43
# File 'lib/dbagile/restful/middleware/utils.rb', line 37

def _200_(env, type, result)
  [
    200,
    {'Content-Type' => type},
    result
  ]
end

#_404_(env, ex = nil) ⇒ Object

Returns a 404 response



28
29
30
31
32
33
34
# File 'lib/dbagile/restful/middleware/utils.rb', line 28

def _404_(env, ex = nil)
  [
    404, 
    {'Content-Type' => 'text/plain'},
    [ "Not found #{env['PATH_INFO']}: " ] + (ex.nil? ? [] : [ ex.message ])
  ]
end

#_500_(env, message) ⇒ Object

Returns a 500 response



46
47
48
49
50
51
52
# File 'lib/dbagile/restful/middleware/utils.rb', line 46

def _500_(env, message)
  [
    500, 
    {'Content-Type' => 'text/plain'}, 
    [ message ]
  ]
end

Returns a copyright response



19
20
21
22
23
24
25
# File 'lib/dbagile/restful/middleware/utils.rb', line 19

def _copyright_(env)
  [
    200, 
    {'Content-Type' => 'text/plain'}, 
    [ "dba #{DbAgile::VERSION} (c) 2010, Bernard Lambeau" ]
  ]
end

#params_to_tuple(params, heading) ⇒ Object

Converts get parameters to a tuple for projection



8
9
10
11
12
13
14
15
16
# File 'lib/dbagile/restful/middleware/utils.rb', line 8

def params_to_tuple(params, heading)
  copy = {}
  params.each_pair{|k, v| 
    k = k.to_sym
    next unless heading.key?(k)
    copy[k] = SByC::TypeSystem::Ruby::coerce(v, heading[k])
  }
  copy
end

#to_xxx_enumerable(format, dataset, columns) ⇒ Object

Converts a dataset-like object to an enumerable rack result



55
56
57
58
59
60
# File 'lib/dbagile/restful/middleware/utils.rb', line 55

def to_xxx_enumerable(format, dataset, columns)
  buffer = StringIO.new
  method = "to_#{format}".to_sym
  DbAgile::IO.send(method, dataset, columns, buffer)
  [ buffer.string ]
end