Skip to content

Instantly share code, notes, and snippets.

View mbklein's full-sized avatar

Michael B. Klein mbklein

  • Northwestern University
  • Evanston, IL
View GitHub Profile
@mbklein
mbklein / gist:1468557
Created December 12, 2011 18:53
Randomize string contents while retaining the flow
def randomize(str)
vowels = ['a','e','i','o','u']
consonants = ('a'..'z').to_a - vowels
digits = ('0'..'9').to_a
replacements = {}
[vowels, consonants, digits].each { |set|
order = set.sort { rand(2) == 0 ? -1 : 1 }
set.each_with_index { |char,i| replacements[char] = order[i] }
}
require 'sinatra'
require 'oai/provider'
class SolrProvider < OAI::Provider::Base
# Implement the provider here
end
get '/oai' do
content_type :xml
SolrProvider.new.process_request(options)
@mbklein
mbklein / gist:1889138
Created February 23, 2012 01:50
ActiveFedora item load times
method user system total real
.find 19.770000 1.130000 20.900000 ( 80.396740)
.load_instance 19.500000 1.400000 20.900000 ( 78.594387)
.load_instance_from_solr 2.130000 0.280000 2.410000 ( 10.900572)
def structure_from_solr(solr_doc, prefix)
prefixed_fields = Hash[solr_doc.select { |k,v| k =~ /^#{prefix}_\d+_/ }]
result = {}
prefixed_fields.each_pair do |path_str,value|
h = result
path = path_str.sub(/_[^_]+$/,'').reverse.split(/_(?=\d+)/).collect { |k| k.reverse }.reverse.collect { |k| k.split(/_(?=\d+)/) }
path.each do |step, index|
if index.nil?
h[step.to_sym] = value
else
1.8.7 :009 > Blacklight.solr.class
=> RSolr::Client
1.8.7 :010 > Solrizer::Fedora::Indexer.new.solr.class
=> RSolr::Client
1.8.7 :011 > ActiveFedora::SolrService.instance.conn.class
=> Solr::Connection
class Nokogiri::XML::Document
def prettify
xslt = Nokogiri::XSLT <<-EOC
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
module ActiveFedora::Config::FileSystem
def self.init *args
# Find files, identify environments, etc., but don't actually
# reach back into ActiveFedora and change anything
end
def self.fedora_config
...
end
def self.fedora
if config.sharded?
raise "Cannot use ActiveFedora.fedora() in a sharded configuration. Use ActiveFedora::Base.connection_for_pid(pid) instead"
else
@@fedora_connection ||= RubydoraConnection.new(config.credentials)
end
end
@mbklein
mbklein / guess_local_timezone.rb
Created March 20, 2012 21:06
Time zone guessing based on Time.now/TZInfo::Timezone
require 'active_support/core_ext'
require 'tzinfo'
require 'time'
# Uses a year's worth of GMT offsets from Time.now to guess the
# closest local time zone within the country specified
def guess_local_timezone(country='US')
offset = Time.now.gmt_offset
zones = TZInfo::Country.get(country).zone_info
@mbklein
mbklein / gist:2286459
Created April 2, 2012 19:12
Marshal & html_safe
1.8.7 :003 > s = 'HTML &amp; stuff'
=> "HTML &amp; stuff"
1.8.7 :004 > s.html_safe?
=> false
1.8.7 :005 > m = Marshal.load(Marshal.dump(s))
=> "HTML &amp; stuff"
1.8.7 :006 > m.html_safe?
=> false
1.8.7 :007 > h = s.html_safe
=> "HTML &amp; stuff"