forked from avdi/gem-love
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First cut at gem plugin, Fan initialization
- Loading branch information
Avdi Grimm
committed
Mar 13, 2010
1 parent
8b1f601
commit 4ae17e3
Showing
8 changed files
with
182 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
$:.unshift(File.expand_path(File.dirname(__FILE__))) | ||
require 'gem-love/note' | ||
require 'gem-love/fan' | ||
require 'gem-love/service' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |