Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
winton committed Jul 19, 2009
0 parents commit 30e6018
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
@@ -0,0 +1,6 @@
.DS_Store
*.gem
coverage
pkg
spec/tmp
tmp
18 changes: 18 additions & 0 deletions MIT-LICENSE
@@ -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.
20 changes: 20 additions & 0 deletions README.markdown
@@ -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>
32 changes: 32 additions & 0 deletions Rakefile
@@ -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
18 changes: 18 additions & 0 deletions gemspec.rb
@@ -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
38 changes: 38 additions & 0 deletions lib/secret_key.rb
@@ -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
37 changes: 37 additions & 0 deletions spec/secret_key_spec.rb
@@ -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
1 change: 1 addition & 0 deletions spec/spec.opts
@@ -0,0 +1 @@
--color
20 changes: 20 additions & 0 deletions spec/spec_helper.rb
@@ -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.