Skip to content

Instantly share code, notes, and snippets.

View science's full-sized avatar

Steve Midgley science

  • California, USA
View GitHub Profile
// (c) Steve Midgley 2011
// Released under Apache 2.0 license
// (http://www.apache.org/licenses/LICENSE-2.0.html)
// Use: This file validates an incoming CouchDB JSON document for compatibility
// with Learning Registry specification (currently 0.15.0)
// Install: For use with CouchDB/CouchApp. This file goes in the root
// of a CouchApp, and should be pushed along with the rest of the app:
// E.g., "couchapp push"
# Scans a CouchApp folder for changes, and on detection auto pushes to Couch
# this allows you to simply save your JS files and test them from CLI without re-loading manually every time
# (c) Steve Midgley 2011
# Released under Apache 2.0 license
# (http://www.apache.org/licenses/LICENSE-2.0.html)
require 'rake'
def running_windows?
!(RUBY_PLATFORM =~ /win32/).nil?
@science
science / lrmi-download.rb
Created July 25, 2013 02:30
Ruby script to pull all LRMI documents from Learning Registry and save them to local files
require 'json'
require 'net/http'
# adds resumption token to URL if needed
def slice_url(options ={})
resume = options[:resume]
url = "http://node01.public.learningregistry.net/slice?any_tags=lrmi"
if resume
url = "http://node01.public.learningregistry.net/slice?any_tags=lrmi&resumption_token=#{resume}"
end
import java.util.HashMap;
public class CodeRunner {
TestClass testClass = new TestClass();
String answer_format = "Test1: %s\nTest2: %s";
static String default_output_template = "Correct answers:\nTest1=%s\nTest2=%s\n";
public static void main(String[] args) {
CodeRunner code = new CodeRunner();
HashMap answers = code.tests();
@science
science / instance-class-include.rb
Last active August 29, 2015 14:12
How to "include" Instance and Class methods into a class in ruby
module Lib
# this loads our class methods when called by "include"
def self.included(base)
base.extend(ClassMethods)
# code here will run when lib is imported, in Lib module context
puts "Lib context: #{self.to_s}"
# this calls the lib_init method to run any class set up code
base.send(:lib_init)
end
@science
science / Rakefile
Created January 6, 2015 04:19
Simple Workstation CI system. Runs your code when changes are detected. Supports line-by-line debugging.
require 'irb'
require 'term/ansicolor'
require 'open3'
module LT
module Term class << self
include ::Term::ANSIColor
end; end
end
# Monitors all files matching "test_*" in ./test and subfolders for changes
@science
science / monkey-patching-101
Created January 23, 2015 22:19
A very simple example of how to install a universal logger for all Objects, that responds differently depending on the Object class
class Object
def self.ltlog(obj = self)
puts "class Hello"
if obj.class == Class then
puts obj.to_s
else
puts obj.class.to_s
end
if obj.kind_of?(String) then
puts "Special string object handling here"
@science
science / read-json.rb
Created March 20, 2015 03:25
JSON Stream processing
require 'yajl/ffi'
json = File::open('lr-test.txt')
@parser = Yajl::FFI::Parser.new
@parser.start_document { puts "start document" }
@parser.end_document { puts "end document" }
@parser.start_object { |o|
puts "start object"
puts o.class if o
puts 'start o end'
}
@science
science / lr-envelope.json
Created March 23, 2015 19:50
Example Learning Registry signed envelope
{
"doc_type": "resource_data",
"resource_locator": "http://beyondpenguins.nsdl.org/issue/column.php?date=June2008&departmentid=curriculum&columnid=curriculum!learning",
"update_timestamp": "2011-10-28T18:10:00.296692Z",
"resource_data": "<nsdl_dc:nsdl_dc xmlns:nsdl_dc=\"http://ns.nsdl.org/nsdl_dc_v1.02/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:dct=\"http://purl.org/dc/terms/\" xmlns:ieee=\"http://www.ieee.org/xsd/LOMv1p0\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.openarchives.org/OAI/2.0/\" schemaVersion=\"1.02.020\" xsi:schemaLocation=\"http://ns.nsdl.org/nsdl_dc_v1.02/ http://ns.nsdl.org/schemas/nsdl_dc/nsdl_dc_v1.02.xsd\">\n <dc:identifier xsi:type=\"dct:URI\">http://beyondpenguins.nsdl.org/issue/column.php?date=June2008&amp;departmentid=curriculum&amp;columnid=curriculum!learning</dc:identifier>\n <dc:title>Free Podcasts on Climate and Climate Change</dc:title>\n <dc:description>In partnership with t
@science
science / RamCopyToPg
Created May 20, 2015 18:08
Demo code showing how to do in-memory copy to Postgres
# pass in an appropriate AR Model for :reset_id_sequence option if you want to reset the sequence after loading
# Options include:
# :delete_existing_data => truncates all data before loading (default: false)
# :sql_parameters => permits specifying sql parameters for COPY such as DELIMETER and NULL (default: '')
# :skip_header_row => skips the first row of table when true (default: false)
def TableLoader.copy_from_file_to_table(table_name, field_names, import_file_path, options = {})
delete_existing_data = options[:delete_existing_data] || false
sql_parameters = options[:sql_parameters] || ''
skip_header_row = options[:skip_header_row] || false
# expects appropriate model for resetting sequence