From 1bac88549fc3bfda81e04b2811443a301aa9e781 Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 20 Sep 2012 13:15:33 +0200 Subject: moved files into own dir and added main leap_ca.rb Also updated the couchdb.yml.example with more meaningful content --- lib/leap_ca/cert.rb | 37 +++++++++++++++++++++++++++++++++++++ lib/leap_ca/couch_changes.rb | 17 +++++++++++++++++ lib/leap_ca/couch_stream.rb | 21 +++++++++++++++++++++ lib/leap_ca/pool.rb | 19 +++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 lib/leap_ca/cert.rb create mode 100644 lib/leap_ca/couch_changes.rb create mode 100644 lib/leap_ca/couch_stream.rb create mode 100644 lib/leap_ca/pool.rb (limited to 'lib/leap_ca') diff --git a/lib/leap_ca/cert.rb b/lib/leap_ca/cert.rb new file mode 100644 index 0000000..80adfbb --- /dev/null +++ b/lib/leap_ca/cert.rb @@ -0,0 +1,37 @@ +require 'couchrest_model' + +class Cert < CouchRest::Model::Base + + use_database 'certs' + + timestamps! + + property :random, Float, :accessible => false + + before_validation :set_random, :attach_zip, :on => :create + + validates :random, :presence => true, + :numericality => {:greater_than => 0, :less_than => 1} + + validates :zipped, :presence => true + + design do + end + + def set_random + self.random = rand + end + + def attach_zip + self.create_attachment :file => StringIO.new("dummy cert"), :name => zipname + end + + def zipname + 'cert.zip' + end + + def zipped + attachments[zipname] + end + +end diff --git a/lib/leap_ca/couch_changes.rb b/lib/leap_ca/couch_changes.rb new file mode 100644 index 0000000..59209a4 --- /dev/null +++ b/lib/leap_ca/couch_changes.rb @@ -0,0 +1,17 @@ +class CouchChanges + def initialize(stream) + @stream = stream + end + + def last_seq + @stream.get "_changes", :limit => 1, :descending => true do |hash| + return hash[:last_seq] + end + end + + def follow + @stream.get "_changes", :feed => :continuous, :since => last_seq do |hash| + yield(hash) + end + end +end diff --git a/lib/leap_ca/couch_stream.rb b/lib/leap_ca/couch_stream.rb new file mode 100644 index 0000000..ed56db2 --- /dev/null +++ b/lib/leap_ca/couch_stream.rb @@ -0,0 +1,21 @@ +class CouchStream + def initialize(database_url) + @database_url = database_url + end + + def get(path, options) + url = url_for(path, options) + # puts url + Yajl::HttpStream.get(url, :symbolize_keys => true) do |hash| + yield(hash) + end + end + + protected + + def url_for(path, options = {}) + url = [@database_url, path].join('/') + url += '?' if options.any? + url += options.map {|k,v| "#{k}=#{v}"}.join('&') + end +end diff --git a/lib/leap_ca/pool.rb b/lib/leap_ca/pool.rb new file mode 100644 index 0000000..76c1963 --- /dev/null +++ b/lib/leap_ca/pool.rb @@ -0,0 +1,19 @@ +require 'yaml' + +module LeapCA + class Pool + def initialize(filename) + @config = YAML.load(File.open(filename, 'r')) + end + + def fill + while Cert.count < self.size do + Cert.create! + end + end + + def size + @config[:size] ||= 10 + end + end +end -- cgit v1.2.3