Class: DbAgile::Restful::Middleware::OneDatabase

Inherits:
Object
  • Object
show all
Includes:
Delete, Get, Post, Utils, Tools::Tuple
Defined in:
lib/dbagile/restful/middleware/one_database.rb

Overview

Rack middleware that provide access to a database using a database instance (DbAgile::Core::Database)

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 Attribute Summary collapse

Instance Method Summary collapse

Methods included from Tools::Tuple

#check_tuple_heading, #tuple_has_key?, #tuple_heading, #tuple_key, #tuple_project, #tuple_to_querystring

Methods included from Delete

#delete

Methods included from Post

#post

Methods included from Get

#get

Methods included from Utils

#_200_, #_404_, #_500_, #_copyright_, #params_to_tuple, #to_xxx_enumerable

Methods included from IO

#known_extension?, #known_format?, #roundtrip_options

Constructor Details

#initialize(database) ⇒ OneDatabase

Creates a middleware instance

Raises:

  • (ArgumentError)


19
20
21
22
# File 'lib/dbagile/restful/middleware/one_database.rb', line 19

def initialize(database)
  raise ArgumentError unless database.kind_of?(DbAgile::Core::Database)
  @database = database
end

Instance Attribute Details

#databaseObject (readonly)

Database instance



16
17
18
# File 'lib/dbagile/restful/middleware/one_database.rb', line 16

def database
  @database
end

Instance Method Details

#call(env) ⇒ Object

Rack handler



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dbagile/restful/middleware/one_database.rb', line 62

def call(env)
  method = env['REQUEST_METHOD'].downcase.to_sym
  if self.respond_to?(method)
    result = self.send(method, env)
  else
    puts "Unsupported restful method #{env['REQUEST_METHOD']}"
    [
      500, 
      {'Content-Type' => 'text/plain'},
      ["Unsupported restful method #{env['REQUEST_METHOD']}"]
    ]
  end
rescue Exception => ex
  puts ex.message
  puts ex.backtrace.join("\n")
  [500, {'Content-Type' => 'text/plain'}, [ex.message]]
end

#decode(env) ⇒ Object

Decodes a path and yield the block with a connection and the requested format



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
59
# File 'lib/dbagile/restful/middleware/one_database.rb', line 26

def decode(env)
  case env['PATH_INFO'].strip[1..-1]
    when ''
      _copyright_(env)
    when /(\w+)(\.(\w+))?$/
      table = $1.to_sym
    
      # Handle format
      format = nil
      if $2
        format = known_extension?($2)
        return _404_(env) unless format
      end
    
      format, body = database.with_connection do |connection|
        yield(connection, table, format) 
      end
      
      content_type = DbAgile::IO::FORMAT_TO_CONTENT_TYPE[format]
      _200_(env, content_type, body)
    else
      _404_(env)
  end
rescue DbAgile::InvalidDatabaseName,
       DbAgile::NoSuchDatabaseError,
       DbAgile::NoSuchTableError => ex
  _404_(env, ex)
rescue DbAgile::Error, Sequel::Error => ex
  if ex.message =~ /exist/
    _404_(env)
  else
    raise
  end
end