Skip to content

Commit

Permalink
First cut at gem plugin, Fan initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Avdi Grimm committed Mar 13, 2010
1 parent 8b1f601 commit 4ae17e3
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 3 deletions.
1 change: 1 addition & 0 deletions Rakefile
Expand Up @@ -12,6 +12,7 @@ begin
gem.authors = ["Avdi Grimm"]
gem.add_development_dependency "rspec", ">= 1.2.9"
gem.add_development_dependency "yard", ">= 0"
gem.add_development_dependency "test-construct", "~> 1.2"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Jeweler::GemcutterTasks.new
Expand Down
5 changes: 5 additions & 0 deletions lib/gem-love.rb
@@ -0,0 +1,5 @@
$:.unshift(File.expand_path(File.dirname(__FILE__)))
require 'gem-love/note'
require 'gem-love/fan'
require 'gem-love/service'

42 changes: 42 additions & 0 deletions lib/gem-love/fan.rb
@@ -0,0 +1,42 @@
require 'pathname'
require 'yaml'

module GemLove

# A Fan is a hacker who loves gems and sends Notes about them to the Service.
class Fan
attr_reader :name
attr_reader :email_address

def self.load_or_init(ui, options={})
home = options.fetch(:home_dir) {
ENV.fetch('HOME') {
require 'etc'
Etc.getpwnam(Etc.getlogin).dir
}
}
home = Pathname(home)

config_file = home + '.gem' + 'love.yml'

fan = if config_file.exist?
YAML.load(config_file.to_s)
else
name = ui.ask("What is your name?")
email = ui.ask("What is your email address?")
new(name, email)
end

config_file.dirname.mkpath
config_file.open('w+') do |f|
YAML.dump(fan, f)
end
fan
end

def initialize(name, email_address)
@name = name
@email_address = email_address
end
end
end
10 changes: 10 additions & 0 deletions lib/gem-love/note.rb
@@ -0,0 +1,10 @@
module GemLove

# A little love-note passed from a Fan to a Gem's authors
class Note
def initialize(gem_name, comment)
@gem_name = gem_name
@comment = comment
end
end
end
11 changes: 11 additions & 0 deletions lib/gem-love/service.rb
@@ -0,0 +1,11 @@
module GemLove

# A Service collects Notes from Fans and directs them to Gem Authors
class Service
def initialize(url)
end

def submit_note_from_fan!(note, fan)
end
end
end
44 changes: 44 additions & 0 deletions lib/rubygems_plugin.rb
@@ -0,0 +1,44 @@
require File.expand_path('gem-love', File.dirname(__FILE__))
require 'rubygems'
require 'rubygems/command_manager'

class Gem::Commands::LoveCommand < Gem::Command

def initialize
super('love', "Show your love for a Gem")
end


def execute
args = options[:args]
comment = if args.size > 1
args.pop
else
"(No comment)"
end
gem_name = get_one_gem_name
note = GemLove::Note.new(gem_name, comment)
fan = GemLove::Fan.load_or_init(ui)
service = GemLove::Service.new('http://gem-love.avdi.org')
service.submit_note_from_fan!(note, fan)
end

def usage
"#{program_name} GEM_NAME [COMMENT]"
end

def description
<<END
This command lets you show your appreciation for a Gem.
END
end

def arguments
<<END
GEM_NAME Name of the Gem you love
COMMENT Your (kind) words about the gem
END
end
end

Gem::CommandManager.instance.register_command :love
69 changes: 69 additions & 0 deletions spec/gem-love/fan_spec.rb
@@ -0,0 +1,69 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'construct'

module GemLove
describe Fan do
include Construct::Helpers

before :each do
@construct = create_construct
@ui = stub("UI")
@ui.stub(:ask).with(/name/).and_return("Tom Servo")
@ui.stub(:ask).with(/email/).and_return("tom@sol.net")
end

after :each do
@construct.destroy!
end

context "initialized with no saved user info" do
it "should prompt for name and email address" do
@ui.should_receive(:ask).with(/name/).
and_return("Tom Servo")
@ui.should_receive(:ask).with(/email/).
and_return("tom@sol.net")
fan = Fan.load_or_init(@ui, :home_dir => @construct)
end

end

context "for a fresh user" do
subject { Fan.load_or_init(@ui, :home_dir => @construct) }

it "should have the entered name" do
subject.name.should == "Tom Servo"
end

it "should have the entered email" do
subject.email_address.should == "tom@sol.net"
end

it "should save the entered name" do
subject # force init
saved_fan = YAML.load_file((@construct + '.gem' + 'love.yml').to_s)
saved_fan.name.should == "Tom Servo"
end

it "should save the entered email" do
subject # force init
saved_fan = YAML.load_file((@construct + '.gem' + 'love.yml').to_s)
saved_fan.email_address.should == "tom@sol.net"
end
end

context "for a saved user" do
before :each do
@construct.file ".gem/love.yml" do |f|
fan = Fan.new("Crow T Robot", "crow@sol.net")
YAML.dump(fan, f)
end
end

it "should not prompt the user on init" do
@ui.should_not_receive(:ask)
Fan.load_or_init(@ui, :home_dir => @construct)
end
end

end
end
3 changes: 0 additions & 3 deletions spec/gem-love_spec.rb
@@ -1,7 +1,4 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe "GemLove" do
it "fails" do
fail "hey buddy, you should probably rename this file and start specing for real"
end
end

0 comments on commit 4ae17e3

Please sign in to comment.