stub Orvos::Base, add collection example
authorTom Clegg <tom@clinicalfuture.com>
Tue, 15 Jan 2013 22:24:24 +0000 (14:24 -0800)
committerTom Clegg <tom@clinicalfuture.com>
Tue, 15 Jan 2013 22:37:10 +0000 (14:37 -0800)
17 files changed:
Gemfile
Gemfile.lock
app/assets/javascripts/collections.js.coffee [new file with mode: 0644]
app/assets/stylesheets/collections.css.scss [new file with mode: 0644]
app/controllers/collections_controller.rb [new file with mode: 0644]
app/helpers/collections_helper.rb [new file with mode: 0644]
app/models/collection.rb [new file with mode: 0644]
app/models/orvos_base.rb [new file with mode: 0644]
config/environments/development.rb
config/environments/production.rb
config/environments/test.rb
config/routes.rb
db/schema.rb [new file with mode: 0644]
test/fixtures/collections.yml [new file with mode: 0644]
test/functional/collections_controller_test.rb [new file with mode: 0644]
test/unit/collection_test.rb [new file with mode: 0644]
test/unit/helpers/collections_helper_test.rb [new file with mode: 0644]

diff --git a/Gemfile b/Gemfile
index 33cb8f7535dba723dc6b2862b5c9a04d97ebb023..090a4fd6ed6262896506e8cd9843ec0286f1a9af 100644 (file)
--- a/Gemfile
+++ b/Gemfile
@@ -5,7 +5,7 @@ gem 'rails', '3.2.11'
 # Bundle edge Rails instead:
 # gem 'rails', :git => 'git://github.com/rails/rails.git'
 
-gem 'pg'
+gem 'sqlite3'
 
 
 # Gems used only for assets and not required
index b0da7b2c84765d24403a49070d644eab3cd9f3d2..02573d48dff64f5b9e3a012f28a4bc203706719f 100644 (file)
@@ -75,7 +75,6 @@ GEM
       fastthread (>= 1.0.1)
       rack
       rake (>= 0.8.1)
-    pg (0.14.1)
     polyglot (0.3.3)
     rack (1.4.4)
     rack-cache (1.2)
@@ -115,6 +114,7 @@ GEM
       multi_json (~> 1.0)
       rack (~> 1.0)
       tilt (~> 1.1, != 1.3.0)
+    sqlite3 (1.3.7)
     therubyracer (0.11.2)
       libv8 (~> 3.11.8.12)
       ref
@@ -135,9 +135,9 @@ DEPENDENCIES
   coffee-rails (~> 3.2.1)
   jquery-rails
   passenger
-  pg
   rails (= 3.2.11)
   rvm-capistrano
   sass-rails (~> 3.2.3)
+  sqlite3
   therubyracer
   uglifier (>= 1.0.3)
diff --git a/app/assets/javascripts/collections.js.coffee b/app/assets/javascripts/collections.js.coffee
new file mode 100644 (file)
index 0000000..7615679
--- /dev/null
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
diff --git a/app/assets/stylesheets/collections.css.scss b/app/assets/stylesheets/collections.css.scss
new file mode 100644 (file)
index 0000000..c21e96d
--- /dev/null
@@ -0,0 +1,3 @@
+// Place all the styles related to the Collections controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
diff --git a/app/controllers/collections_controller.rb b/app/controllers/collections_controller.rb
new file mode 100644 (file)
index 0000000..3d8f990
--- /dev/null
@@ -0,0 +1,2 @@
+class CollectionsController < ApplicationController
+end
diff --git a/app/helpers/collections_helper.rb b/app/helpers/collections_helper.rb
new file mode 100644 (file)
index 0000000..3017985
--- /dev/null
@@ -0,0 +1,2 @@
+module CollectionsHelper
+end
diff --git a/app/models/collection.rb b/app/models/collection.rb
new file mode 100644 (file)
index 0000000..9069080
--- /dev/null
@@ -0,0 +1,2 @@
+class Collection < OrvosBase
+end
diff --git a/app/models/orvos_base.rb b/app/models/orvos_base.rb
new file mode 100644 (file)
index 0000000..0775d5d
--- /dev/null
@@ -0,0 +1,71 @@
+class OrvosBase < ActiveRecord::Base
+  @@orvos_v1_base = Rails.configuration.orvos_v1_base
+  def self.columns
+    return @columns unless @columns.nil?
+    @columns = []
+    return @columns if orvos_schema[self.to_s.to_sym].nil?
+    orvos_schema[self.to_s.to_sym].each do |coldef|
+      k = coldef[:name].to_sym
+      if coldef[:type] == coldef[:type].downcase
+        @columns << column(k, coldef[:type].to_sym)
+      else
+        @columns << column(k, :text)
+        serialize k, coldef[:type].to_sym
+      end
+      attr_accessible k
+    end
+    attr_reader :etag
+    attr_reader :kind
+    @columns
+  end
+  def self.column(name, sql_type = nil, default = nil, null = true)
+    ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
+  end
+  def self.all
+    thelist = api('')
+    thelist[:items].collect { |x| new(x) }
+  end
+  def self.find(uuid)
+    new(api('/' + uuid))
+  end
+  def save
+    self.class.api('/' + uuid, {
+                     '_method' => 'PUT',
+                     self.class.to_s.underscore => { :name => self.name }
+                   })
+  end
+  def initialize(h={})
+    @etag = h.delete :etag
+    @kind = h.delete :kind
+    super
+  end
+
+  protected
+  def self.api(action, data=nil, o={})
+    dataargs = []
+    if !data.nil?
+      data.each do |k,v|
+        dataargs << '-d'
+        if v.is_a? String
+          dataargs << "#{k}=#{v}"
+        else
+          dataargs << "#{k}=#{JSON.generate v}"
+        end
+      end
+    end
+    json = nil
+    IO.popen([ENV,
+              'curl',
+              '-sk',
+              *dataargs,
+              "#{@@orvos_v1_base}/#{o[:resource_path] || self.to_s.underscore.pluralize}#{action}"],
+             'r') do |io|
+      json = io.read
+    end
+    JSON.parse json, :symbolize_names => true
+  end
+
+  def self.orvos_schema
+    $orvos_schema ||= api '', nil, {resource_path: 'schema'}
+  end
+end
index 6858b0f9c6ab77daf58857352e436a088f5b1ccc..7430b56c9bd6ae9bd6a62789987e1f9df2d623c0 100644 (file)
@@ -34,4 +34,6 @@ Vcffarm::Application.configure do
 
   # Expands the lines which load the assets
   config.assets.debug = true
+
+  config.orvos_v1_base = 'https://orvos/orvos/v1'
 end
index ebd3e879b495319622ee0e1a72cdcdbde3b5a6b1..13337c186ec991708a2d2791e8e1e53b71986784 100644 (file)
@@ -64,4 +64,6 @@ Vcffarm::Application.configure do
   # Log the query plan for queries taking more than this (works
   # with SQLite, MySQL, and PostgreSQL)
   # config.active_record.auto_explain_threshold_in_seconds = 0.5
+
+  config.orvos_v1_base = 'https://9ujm1.orvosapi.com/orvos/v1'
 end
index 98b93cce5eecb68add542d773b80d1314a0c3330..f0a29bbf191076e41d06f67af9d34039764ac1ed 100644 (file)
@@ -34,4 +34,6 @@ Vcffarm::Application.configure do
 
   # Print deprecation notices to the stderr
   config.active_support.deprecation = :stderr
+
+  config.orvos_v1_base = 'https://orvos/orvos/v1'
 end
index c404a3a9ba2e40286c06baf0822fb64075583bf1..aa86c032f6f42ab26bd59a5ae49f58a7f8b22791 100644 (file)
@@ -1,4 +1,7 @@
 Vcffarm::Application.routes.draw do
+  resources :collections
+
+
   # The priority is based upon order of creation:
   # first created -> highest priority.
 
diff --git a/db/schema.rb b/db/schema.rb
new file mode 100644 (file)
index 0000000..b5e6a79
--- /dev/null
@@ -0,0 +1,16 @@
+# encoding: UTF-8
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended to check this file into your version control system.
+
+ActiveRecord::Schema.define(:version => 0) do
+
+end
diff --git a/test/fixtures/collections.yml b/test/fixtures/collections.yml
new file mode 100644 (file)
index 0000000..c63aac0
--- /dev/null
@@ -0,0 +1,11 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+# This model initially had no columns defined.  If you add columns to the
+# model remove the '{}' from the fixture names and add the columns immediately
+# below each fixture, per the syntax in the comments below
+#
+one: {}
+# column: value
+#
+two: {}
+#  column: value
diff --git a/test/functional/collections_controller_test.rb b/test/functional/collections_controller_test.rb
new file mode 100644 (file)
index 0000000..2cbaf8d
--- /dev/null
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class CollectionsControllerTest < ActionController::TestCase
+  # test "the truth" do
+  #   assert true
+  # end
+end
diff --git a/test/unit/collection_test.rb b/test/unit/collection_test.rb
new file mode 100644 (file)
index 0000000..4f73670
--- /dev/null
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class CollectionTest < ActiveSupport::TestCase
+  # test "the truth" do
+  #   assert true
+  # end
+end
diff --git a/test/unit/helpers/collections_helper_test.rb b/test/unit/helpers/collections_helper_test.rb
new file mode 100644 (file)
index 0000000..16a85d9
--- /dev/null
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class CollectionsHelperTest < ActionView::TestCase
+end