diff options
Diffstat (limited to 'lib/leap_ca')
-rw-r--r-- | lib/leap_ca/cert.rb | 37 | ||||
-rw-r--r-- | lib/leap_ca/couch_changes.rb | 17 | ||||
-rw-r--r-- | lib/leap_ca/couch_stream.rb | 21 | ||||
-rw-r--r-- | lib/leap_ca/pool.rb | 19 |
4 files changed, 94 insertions, 0 deletions
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 |