Sha256: 1167c70ae3fe7ebfd2e4c16cd8943ce9712b5e5ef3e3b67d95c33d26a010e418
Contents?: true
Size: 1.29 KB
Versions: 1
Compression:
Stored size: 1.29 KB
Contents
# frozen_string_literal: true require 'ipaddr' module Keycard # looks up institution ID(s) by IP address class InstitutionFinder INST_QUERY = <<~SQL SELECT inst FROM aa_network WHERE ? >= dlpsAddressStart AND ? <= dlpsAddressEnd AND dlpsAccessSwitch = 'allow' AND dlpsDeleted = 'f' AND inst is not null AND inst NOT IN ( SELECT inst FROM aa_network WHERE ? >= dlpsAddressStart AND ? <= dlpsAddressEnd AND dlpsAccessSwitch = 'deny' AND dlpsDeleted = 'f' ) SQL def initialize(db: Keycard::DB.db) @db = db @stmt = @db[INST_QUERY, *[:$client_ip] * 4].prepare(:select, :unused) end def attributes_for(request) return {} unless (numeric_ip = numeric_ip(request.remote_ip)) insts = insts_for_ip(numeric_ip) if !insts.empty? { 'dlpsInstitutionId' => insts } else {} end end private attr_reader :stmt def insts_for_ip(numeric_ip) stmt.call(client_ip: numeric_ip).map { |row| row[:inst] } end def numeric_ip(dotted_ip) return unless dotted_ip begin IPAddr.new(dotted_ip).to_i rescue IPAddr::InvalidAddressError nil end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
keycard-0.1.1 | lib/keycard/institution_finder.rb |