Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: joealba/gemline
base: 5cde691abb
...
head fork: joealba/gemline
compare: 77c6ac7184
  • 2 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
Commits on Jul 28, 2012
@joealba Passing around options hashes to support some extra output switches. …
…Now I just need to parse the command line in a smart way.
443d7cf
@joealba Closes #2
Output the important stuff to stdout and everything else to stderr so
people could redirect the output and append to a Gemfile and things
would work properly.
77c6ac7
View
2  gemline.gemspec
@@ -13,7 +13,7 @@ Gem::Specification.new do |gem|
gem.name = "gemline"
gem.require_paths = ["lib"]
- gem.version = '0.1.2'
+ gem.version = '0.1.3'
gem.add_dependency('crack')
gem.add_development_dependency('rake') # For Travis CI
View
31 lib/gemline.rb
@@ -15,8 +15,8 @@ def self.query(gem_name)
g = Gemline.new(gem_name)
if g.gem_not_found?
- puts "Ruby gem #{gem_name} was not found on rubygems.org"
- exit
+ $stderr.puts "Ruby gem #{gem_name} was not found on rubygems.org"
+ Kernel.exit 1
else
puts g.gemline
copy_to_clipboard(g.gemline)
@@ -24,12 +24,12 @@ def self.query(gem_name)
end
- def initialize(gem_name)
+ def initialize(gem_name, options = {})
@gem = gem_name.to_s.gsub(/[^\w\-]+/,'') # Yeah, a little over-defensive.
@json = Gemline.get_rubygem_json(@gem)
unless gem_not_found?
@response = Crack::JSON.parse(@json)
- @gemline = Gemline.create_gemline(@gem, response['version'])
+ @gemline = Gemline.create_gemline(@gem, response['version'], options)
end
end
@@ -44,15 +44,28 @@ def self.get_rubygem_json(gem_name)
Net::HTTP.get(URI.parse("http://rubygems.org/api/v1/gems/#{gem_name}.json"))
end
- def self.create_gemline(gem_name, version)
+ def self.create_gemline(gem_name, version, options = {})
+ if options[:gemspec]
+ return gemspec_gemline(gem_name, version)
+ else
+ return gemfile_gemline(gem_name, version)
+ end
+ end
+
+ def self.gemfile_gemline(gem_name, version)
%Q{gem "#{gem_name}", "~> #{version}"}
end
+ def self.gemspec_gemline(gem_name, version)
+ %Q{gem.add_dependency(%q<#{gem_name}>, ["~> #{version}"])}
+ end
+
+
def self.check_input(gem_name)
if (gem_name.empty? || ['-h','--help','help'].include?(gem_name))
- puts "Usage: gemline [GEM NAME]"
- puts " Prints a Gemfile require line for a Ruby gem on Rubygems.org"
- exit
+ $stderr.puts "Usage: gemline [GEM NAME]"
+ $stderr.puts " Prints a Gemfile require line for a Ruby gem on Rubygems.org"
+ Kernel.exit 1
end
# if (['-v','--version'].include?(gem_name))
@@ -65,7 +78,7 @@ def self.copy_to_clipboard(gemline)
begin
if clipboard = IO.popen('pbcopy', 'r+')
clipboard.puts gemline
- puts " Gem line copied to your clipboard. Ready to paste into your Gemfile"
+ $stderr.puts " Gem line copied to your clipboard. Ready to paste into your Gemfile"
end
rescue
## Yeah, I hate this too. But it does what I want -- silently fail if pbcopy isn't available.
View
21 spec/acceptance_spec.rb
@@ -0,0 +1,21 @@
+require 'spec_helper'
+require 'stringio'
+
+describe "gemline output" do
+
+ before do
+ stub_rubygems_json_output
+ end
+
+ it "should output one line to STDOUT so the output can be appended to a Gemfile" do
+ grab_io { Gemline.query('rails') }
+ @stdout.readlines.count.should eq(1)
+ end
+
+ it "should output nothing to STDOUT on error" do
+ Kernel.stub(:exit)
+ grab_io { Gemline.query('doesnotexist') }
+ @stdout.readlines.count.should eq(0)
+ end
+
+end
View
11 spec/gemline_spec.rb
@@ -5,10 +5,7 @@
describe "querying rubygems" do
before do
- Dir.glob(File.join(File.dirname(__FILE__),'samples', '*.json')).each do |f|
- gem_name = $1 if f =~ /\/([\w\-]+).json$/
- Gemline.stub!(:get_rubygem_json).with(gem_name).and_return(IO.read(f))
- end
+ stub_rubygems_json_output
end
it "should be able to parse the version out of a good json string" do
@@ -32,6 +29,12 @@
g = Gemline.new('nokogiri')
g.gemline.should == %Q{gem "nokogiri", "~> 1.5.5"}
end
+
+ it "should be able to generate a gemspec-style gemline" do
+ g = Gemline.new('rails', :gemspec => true)
+ g.gemline.should == %Q!gem.add_dependency(%q<rails>, ["~> 3.1.1"])!
+ end
+
end
end
View
20 spec/spec_helper.rb
@@ -11,3 +11,23 @@
RSpec.configure do |c|
end
+
+
+def stub_rubygems_json_output
+ Dir.glob(File.join(File.dirname(__FILE__),'samples', '*.json')).each do |f|
+ gem_name = $1 if f =~ /\/([\w\-]+).json$/
+ Gemline.stub!(:get_rubygem_json).with(gem_name).and_return(IO.read(f))
+ end
+end
+
+def grab_io
+ @stdout = StringIO.new; $stdout = @stdout;
+ @stderr = StringIO.new; $stderr = @stderr;
+
+ yield
+
+ @stdout.rewind; @stderr.rewind;
+
+ $stdout = STDOUT
+ $stderr = STDERR
+end

No commit comments for this range

Something went wrong with that request. Please try again.