Permalink
Browse files

First cut at implementing Service

  • Loading branch information...
1 parent 4ae17e3 commit 43fe2330341035de5a943e0055de2ff86f8312b8 Avdi Grimm committed Mar 13, 2010
Showing with 109 additions and 3 deletions.
  1. +2 −0 Rakefile
  2. +1 −1 lib/gem-love/fan.rb
  3. +18 −0 lib/gem-love/service.rb
  4. +1 −1 lib/rubygems_plugin.rb
  5. +10 −0 spec/gem-love/fan_spec.rb
  6. +74 −0 spec/gem-love/service_spec.rb
  7. +3 −1 spec/spec_helper.rb
View
@@ -13,6 +13,8 @@ begin
gem.add_development_dependency "rspec", ">= 1.2.9"
gem.add_development_dependency "yard", ">= 0"
gem.add_development_dependency "test-construct", "~> 1.2"
+ gem.add_development_dependency "webmock", "~> 0.9.1"
+ gem.add_dependency "link_header", "~> 0.0.5"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Jeweler::GemcutterTasks.new
View
@@ -20,7 +20,7 @@ def self.load_or_init(ui, options={})
config_file = home + '.gem' + 'love.yml'
fan = if config_file.exist?
- YAML.load(config_file.to_s)
+ YAML.load_file(config_file.to_s)
else
name = ui.ask("What is your name?")
email = ui.ask("What is your email address?")
View
@@ -1,11 +1,29 @@
+require 'link_header'
+require 'addressable/uri'
+
module GemLove
# A Service collects Notes from Fans and directs them to Gem Authors
class Service
+ attr_reader :notes_url
+
+ def self.notes_relation
+ "http://gem-love.avdi.org/relations#notes"
+ end
+
def initialize(url)
+ response = Net::HTTP.get_response(URI.parse(url))
+ links = LinkHeader.parse(response['Link'].to_s)
+ notes_link = links.find_link(["rel", self.class.notes_relation])
+ @notes_url = Addressable::URI.parse(notes_link.href)
end
def submit_note_from_fan!(note, fan)
+ Net::HTTP.post_form(notes_url,
+ :name => fan.name,
+ :email_address => fan.email_address,
+ :comment => note.comment,
+ :gem_name => note.gem_name)
end
end
end
View
@@ -14,7 +14,7 @@ def execute
comment = if args.size > 1
args.pop
else
- "(No comment)"
+ ask("Comment:")
end
gem_name = get_one_gem_name
note = GemLove::Note.new(gem_name, comment)
View
@@ -52,6 +52,8 @@ module GemLove
end
context "for a saved user" do
+ subject { Fan.load_or_init(@ui, :home_dir => @construct) }
+
before :each do
@construct.file ".gem/love.yml" do |f|
fan = Fan.new("Crow T Robot", "crow@sol.net")
@@ -63,6 +65,14 @@ module GemLove
@ui.should_not_receive(:ask)
Fan.load_or_init(@ui, :home_dir => @construct)
end
+
+ it "should have the saved name" do
+ subject.name.should == "Crow T Robot"
+ end
+
+ it "should have the saved email address" do
+ subject.email_address.should == "crow@sol.net"
+ end
end
end
@@ -0,0 +1,74 @@
+require 'cgi'
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+module GemLove
+ describe Service do
+ def parse_form_data(data)
+ unencoded_data = CGI.unescape(data)
+ pairs = unencoded_data.split('&')
+ pairs.inject({}) {|h, pair|
+ name, value = pair.split('=')
+ h[name] = value
+ h
+ }
+ end
+
+ before :each do
+ stub_request(:get, "example.com").
+ to_return(:headers => {
+ 'Link' => "<http://example.com/notes>; rel=\"#{Service.notes_relation}\""
+ })
+ end
+
+ subject { Service.new('http://example.com') }
+
+ it "should be able to find the notes resource" do
+ subject.notes_url.to_s.should == "http://example.com/notes"
+ end
+
+ context "submitting a note" do
+ before :each do
+ @note = stub("Note", :comment => "COMMENT", :gem_name => "GEMNAME")
+ @fan = stub("Fan", :name => "Tom Servo", :email_address => "tom@sol.net")
+ stub_request(:post, "example.com/notes")
+ end
+
+ def do_submit
+ subject.submit_note_from_fan!(@note, @fan)
+ end
+
+ it "should submit to the notes resource" do
+ do_submit
+ WebMock.should have_requested(:post, "example.com/notes")
+ end
+
+ it "should submit the fan name to the notes resource" do
+ do_submit
+ request(:post, "example.com/notes").with do |request|
+ parse_form_data(request.body)['name'].should == "Tom Servo"
+ end.should have_been_made
+ end
+
+ it "should submit the fan email to the notes resource" do
+ do_submit
+ request(:post, "example.com/notes").with do |request|
+ parse_form_data(request.body)['email_address'].should == "tom@sol.net"
+ end.should have_been_made
+ end
+
+ it "should submit the note text to the notes resource" do
+ do_submit
+ request(:post, "example.com/notes").with do |request|
+ parse_form_data(request.body)['comment'].should == "COMMENT"
+ end.should have_been_made
+ end
+
+ it "should submit the gem name to the notes resource" do
+ do_submit
+ request(:post, "example.com/notes").with do |request|
+ parse_form_data(request.body)['gem_name'].should == "GEMNAME"
+ end.should have_been_made
+ end
+ end
+ end
+end
View
@@ -5,5 +5,7 @@
require 'spec/autorun'
Spec::Runner.configure do |config|
-
+ require 'webmock/rspec'
+
+ include WebMock
end

0 comments on commit 43fe233

Please sign in to comment.