Permalink
Browse files

First commit

  • Loading branch information...
0 parents commit 30e6018aa90d45537c2ab7268d88fae9288cbc85 @winton committed Jul 19, 2009
Showing with 190 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +18 −0 MIT-LICENSE
  3. +20 −0 README.markdown
  4. +32 −0 Rakefile
  5. +18 −0 gemspec.rb
  6. +38 −0 lib/secret_key.rb
  7. +37 −0 spec/secret_key_spec.rb
  8. +1 −0 spec/spec.opts
  9. +20 −0 spec/spec_helper.rb
@@ -0,0 +1,6 @@
+.DS_Store
+*.gem
+coverage
+pkg
+spec/tmp
+tmp
@@ -0,0 +1,18 @@
+Copyright (c) 2009 Winton Welsh
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,20 @@
+Secret Key
+==========
+
+Provides an accessor for a secret key stored in <code>tmp</code>.
+
+Setup
+-----
+
+<pre>
+gem sources -a http://gems.github.com
+sudo gem install winton-secret_key
+</pre>
+
+Usage
+-----
+
+<pre>
+key = SecretKey.new(File.dirname("__FILE__"))
+key.read
+</pre>
@@ -0,0 +1,32 @@
+require 'rubygems'
+require 'rake'
+require 'rake/gempackagetask'
+require 'spec/rake/spectask'
+require 'gemspec'
+
+desc "Generate gemspec"
+task :gemspec do
+ File.open("#{Dir.pwd}/#{GEM_NAME}.gemspec", 'w') do |f|
+ f.write(GEM_SPEC.to_ruby)
+ end
+end
+
+desc "Install gem"
+task :install do
+ Rake::Task['gem'].invoke
+ `sudo gem uninstall #{GEM_NAME} -x`
+ `sudo gem install pkg/#{GEM_NAME}*.gem`
+ `rm -Rf pkg`
+end
+
+desc "Package gem"
+Rake::GemPackageTask.new(GEM_SPEC) do |pkg|
+ pkg.gem_spec = GEM_SPEC
+end
+
+desc "Run specs"
+Spec::Rake::SpecTask.new do |t|
+ t.rcov = true
+ t.spec_opts = ["--format", "specdoc", "--colour"]
+ t.spec_files = FileList["spec/**/*_spec.rb"]
+end
@@ -0,0 +1,18 @@
+GEM_NAME = 'secret_key'
+GEM_FILES = FileList['**/*'] - FileList['coverage', 'coverage/**/*', 'pkg', 'pkg/**/*']
+GEM_SPEC = Gem::Specification.new do |s|
+ # == CONFIGURE ==
+ s.author = "Winton Welsh"
+ s.email = "mail@wintoni.us"
+ s.homepage = "http://github.com/winton/#{GEM_NAME}"
+ s.summary = "Provides an accessor for a secret key stored in tmp"
+ # == CONFIGURE ==
+ s.executables << GEM_NAME
+ s.extra_rdoc_files = [ "README.markdown" ]
+ s.files = GEM_FILES.to_a
+ s.has_rdoc = false
+ s.name = GEM_NAME
+ s.platform = Gem::Platform::RUBY
+ s.require_path = "lib"
+ s.version = "0.1.0"
+end
@@ -0,0 +1,38 @@
+require 'digest/sha1'
+
+class SecretKey
+
+ attr_reader :key
+
+ def initialize(base_dir)
+ tmp = "#{base_dir}/tmp"
+ tmp = File.expand_path(tmp)
+ FileUtils.mkdir_p(tmp)
+ @path = "#{tmp}/secret_key"
+ end
+
+ def read
+ if @key
+ @key
+ elsif exists?
+ @key = File.read(@path).strip
+ else
+ write
+ end
+ end
+
+ def write
+ File.open(@path, 'w') do |f|
+ @key = RUBY_PLATFORM + rand(10000000000000).to_s
+ @key = Digest::SHA1.hexdigest(@key)
+ f.write(@key)
+ end
+ @key
+ end
+
+ private
+
+ def exists?
+ File.exists?(@path)
+ end
+end
@@ -0,0 +1,37 @@
+require File.expand_path("#{File.dirname(__FILE__)}/spec_helper")
+
+describe SecretKey do
+
+ before(:all) do
+ reset
+ @secret_key = SecretKey.new(File.dirname(__FILE__))
+ @key = @secret_key.read
+ end
+
+ context "first read" do
+
+ it "should generate the key" do
+ @key.length.should == 40
+ end
+ end
+
+ context "second read" do
+
+ it "should already have the key stored" do
+ @secret_key.key.length.should == 40
+ end
+ end
+
+ context "first read with temp file present" do
+
+ before(:all) do
+ @secret_key = SecretKey.new(File.dirname(__FILE__))
+ end
+
+ it "should read the key from the temp file" do
+ @secret_key.read.length.should == 40
+ @secret_key.read.should == @key
+ end
+ end
+
+end
@@ -0,0 +1 @@
+--color
@@ -0,0 +1,20 @@
+$TESTING=true
+SPEC = File.dirname(__FILE__)
+$:.unshift File.expand_path("#{SPEC}/../lib")
+
+require 'secret_key'
+require 'pp'
+
+Spec::Runner.configure do |config|
+end
+
+# For use with rspec textmate bundle
+def debug(object)
+ puts "<pre>"
+ puts object.pretty_inspect.gsub('<', '&lt;').gsub('>', '&gt;')
+ puts "</pre>"
+end
+
+def reset
+ FileUtils.rm_rf File.dirname(__FILE__) + "/tmp"
+end

0 comments on commit 30e6018

Please sign in to comment.