Skip to content

Instantly share code, notes, and snippets.

View dchandekstark's full-sized avatar

David Chandek-Stark dchandekstark

  • Duke University Libraries
  • Durham, North Carolina USA
View GitHub Profile
@dchandekstark
dchandekstark / ostruct_delegator.rb
Created September 16, 2015 21:03
OpenStructDelegator
require "ostruct"
require "delegate"
class OpenStructDelegator < SimpleDelegator
def initialize(args)
super OpenStruct.new(args)
end
require 'net/http'
FITS_SERVLET = "http://127.0.0.1:8080/FITSservlet/FitsServlet_2"
uri = URI(FITS_SERVLET)
# replace value with path to file
uri.query = URI.encode_www_form(file: "/tmp/logo.png")
response = Net::HTTP.get_response(uri)
@dchandekstark
dchandekstark / ability.rb
Created May 5, 2015 21:44
Ability Class
class Ability
include CanCan::Ability
class_attribute :permissions
attr_reader :user
def initialize(user)
@user = user
apply_permissions
@dchandekstark
dchandekstark / delws.sh
Last active August 29, 2015 14:20
Delete trailing whitespace
#!/bin/sh
TYPES="*.rb *.css *.js *.rake"
git ls-files ${TYPES} | xargs sed -E -i "" "s/[[:space:]]+$//g"
@dchandekstark
dchandekstark / duketerms.rdf.xml
Created April 8, 2015 14:07
duketerms.rdf.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE rdf:RDF [
<!ENTITY rdfns 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'>
<!ENTITY rdfsns 'http://www.w3.org/2000/01/rdf-schema#'>
<!ENTITY dcns 'http://purl.org/dc/elements/1.1/'>
<!ENTITY dctermsns 'http://purl.org/dc/terms/'>
<!ENTITY dctypens 'http://purl.org/dc/dcmitype/'>
<!ENTITY dcamns 'http://purl.org/dc/dcam/'>
<!ENTITY skosns 'http://www.w3.org/2004/02/skos/core#'>
]>
@dchandekstark
dchandekstark / roles.rb
Created March 16, 2015 20:57
Roles Vocab
module Ddr
module Vocab
class Roles < RDF::StrictVocabulary("http://repository.lib.duke.edu/vocab/roles/")
term :Role,
label: "Role",
comment: "A role granted to an agent."
# type: "rdfs:Class"
# subClassOf: "http://www.w3.org/ns/auth/acl#Authorization"
class DsCallbackTest < ActiveFedora::Datastream
after_save :do_after_save
after_create :do_after_create
def do_after_save
puts "DS after_save"
end
def do_after_create
puts "DS after_create"
@dchandekstark
dchandekstark / sessions_controller.rb
Last active August 29, 2015 14:15
SessionsController - Devise + Ominauth + Shibboleth
class Users::SessionsController < Devise::SessionsController
def new
store_location_for(:user, request.referrer) # return to previous page after authn
if Ddr::Auth.require_shib_user_authn
# don't want the "sign in or sign up" flash in this case
flash.discard(:alert)
redirect_to user_omniauth_authorize_path(:shibboleth)
else
super
@dchandekstark
dchandekstark / model.rb
Created February 6, 2015 00:26
Validation
class Model
attr_accessor :title
# has-a validator
def validator
@validator ||= ModelValidator.new(self)
end
end
# Represents a validation attempt
class Validation
@dchandekstark
dchandekstark / README.md
Last active August 29, 2015 14:13
Indexing API

ActiveFedora Indexing API

TL;DR: This Gist sketches out an API for providing a means of managing inheritable indexing configurations in ActiveFedora. Primary motivations are (1) to avoid (for most purposes) putting indexing logic in an overriden #to_solr method, and (2) to make it possible to introspect a model's or instance’s indexing.

An earlier version of this idea was kicked around on IRC, and I've refined it significantly since then, mainly to accommodate the current property indexing API (from ActiveTriples). Originally, I just aimed to deal with indexing calculated values (basically model methods, as opposed to attributes or properties which already support indexing), for which one currently has to override #to_solr.

Essentially, I wanted to be able to do this type of thing on a model:

# Stupid example