class Fluoride::Collector::Middleware

Attributes

config[R]

Public Class Methods

new(app, config) click to toggle source
# File lib/fluoride-collector/middleware.rb, line 8
def initialize(app, config)
  @app = app
  @config = config
  @tagging = config.tags
end

Private Instance Methods

clean_hash(hash) click to toggle source
# File lib/fluoride-collector/middleware.rb, line 35
def clean_hash(hash)
  hash.each_key do |key|
    value = hash[key]
    case value
    when String
      if value.respond_to?(:ascii_only?) and value.ascii_only? and value.respond_to?(:force_encoding)
        value = value.dup
        value.force_encoding("US-ASCII")
        hash[key] = value
      end
    when Hash
      hash[key] = clean_hash(value)
    end
  end
  hash
end
request_hash(env) click to toggle source
# File lib/fluoride-collector/middleware.rb, line 52
def request_hash(env)
  body = nil
  if env['rack.input'].respond_to? :read
    body = env['rack.input'].read
    env['rack.input'].rewind rescue nil
  end
  clean_hash(
    "user_agent" => env['HTTP_USER_AGENT'],
    "content_type" => env['CONTENT_TYPE'],
    "accept" => env["HTTP_ACCEPT"],
    "accept_encoding" => env["HTTP_ACCEPT_ENCODING"],
    "referer" => env["HTTP_REFERER"],
    "cookies" => env["HTTP_COOKIE"],
    "authorization" => env["HTTP_AUTHORIZATION"],
    "method" => env["REQUEST_METHOD"],
    "host" => env['HTTP_HOST'] || "#{env['SERVER_NAME'] || env['SERVER_ADDR']}:#{env['SERVER_PORT']}",
    "path" => URI.unescape(env["SCRIPT_NAME"].to_s + env["PATH_INFO"].to_s),
    "query_string" => env["QUERY_STRING"].to_s,
    "body" => body
  )
end
store(record) click to toggle source
# File lib/fluoride-collector/middleware.rb, line 16
def store(record)
  if $stderr.respond_to? :puts
    $stderr.puts "#{self.class.name}: Storing #{record.keys.inspect} with #{@config.persister_class.name}"
  end
  #take only pictures
  @config.persister(collection_type, record).write
rescue Exception => ex
  #leave only footprints
  # :nocov:
  begin
    if $stderr.respond_to? :puts
      $stderr.puts ex.inspect
      $stderr.puts ex.backtrace.inspect
    end
  rescue Exception
  end
  # :nocov:
end