Class: Proxie::SQLiteLogger

Inherits:
Logger
  • Object
show all
Defined in:
lib/proxie/logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, overwrite = true) ⇒ SQLiteLogger

Initialize SQLite3 logger



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/proxie/logger.rb', line 13

def initialize(path, overwrite=true)
  raise 'Database file with this name already exists!' if File.exists?(path) && !overwrite
  File.delete(path) if File.exists?(path)
  
  @db = Sequel.sqlite(path)
  @db.create_table :requests do
    primary_key :id
    string :request_headers
    string :request_url
    string :request_body
    string :response_headers
    string :response_body
  end
  @records = @db[:requests]
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



10
11
12
# File 'lib/proxie/logger.rb', line 10

def db
  @db
end

#recordsObject (readonly)

Returns the value of attribute records.



10
11
12
# File 'lib/proxie/logger.rb', line 10

def records
  @records
end

Instance Method Details

#packet(req, resp) ⇒ Object

Save request and response data



30
31
32
33
34
35
36
37
38
# File 'lib/proxie/logger.rb', line 30

def packet(req, resp)
  @records.insert(
    :request_headers  => req.header.to_json,
    :request_url      => req.request_line,
    :request_body     => req.body.inspect,
    :response_headers => resp.header.to_json,
    :response_body    => resp.body.inspect
  )
end