latest 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -22,6 +22,12 @@ latest rails spree haml
22
22
  # haml 3.1.4
23
23
  ```
24
24
 
25
+ latest rails spree --pre haml
26
+ # rails 3.2.1
27
+ # spree --pre 1.0.0.rc4
28
+ # haml 3.1.4
29
+ ```
30
+
25
31
 
26
32
  ------------------------------------------------------------------------------
27
33
  Installation
data/bin/latest CHANGED
@@ -8,10 +8,17 @@ require "latest"
8
8
  if ARGV.first =~ /^-(v|-version)$/
9
9
  puts "Latest v" + Latest::VERSION
10
10
  else
11
- ARGV.each do |name|
11
+ ARGV.each_with_index do |name, index|
12
12
  print name + " "
13
13
  begin
14
- puts Latest.version(name)
14
+ gem = Latest.lookup(name)
15
+ if ARGV[index + 1] == "--pre"
16
+ ARGV.slice!(index + 1, 1)
17
+ print "--pre "
18
+ puts gem.last_prerelease_version
19
+ else
20
+ puts gem.last_stable_version
21
+ end
15
22
  rescue Latest::GemNotFoundError => e
16
23
  print "<- "
17
24
  puts e.message
data/lib/latest/gem.rb CHANGED
@@ -5,20 +5,41 @@ require "json"
5
5
  module Latest
6
6
  class Gem
7
7
 
8
- attr_reader :attributes
8
+ attr_reader :name, :versions
9
9
 
10
10
  def initialize(name)
11
- @attributes = fetch(name)
12
- if @attributes.nil?
11
+ @name = name
12
+ @versions = fetch
13
+ if @versions.nil? || @versions.empty?
13
14
  raise ::Latest::GemNotFoundError, "`#{name}` could not be found on rubygems.org!"
14
15
  end
15
16
  self
16
17
  end
17
18
 
19
+ def last_prerelease_version
20
+ return @last_prerelease_version if @last_prerelease_version
21
+ @last_prerelease_version = find_latest_version(true)
22
+ end
23
+
24
+ def last_stable_version
25
+ return @last_stable_version if @last_stable_version
26
+ @last_stable_version = find_latest_version
27
+ end
28
+
18
29
  private
19
30
 
20
- def fetch(name)
21
- uri = URI("https://rubygems.org/api/v1/gems/#{name}.json")
31
+ def find_latest_version(prerelease=false)
32
+ version = "(not found)"
33
+ @versions.each do |v|
34
+ next unless v["prerelease"] == prerelease
35
+ version = v["number"]
36
+ break
37
+ end
38
+ version
39
+ end
40
+
41
+ def fetch
42
+ uri = URI("https://rubygems.org/api/v1/versions/#{@name}.json")
22
43
  Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
23
44
  request = Net::HTTP::Get.new uri.request_uri
24
45
  response = http.request request
@@ -1,3 +1,3 @@
1
1
  module Latest
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/latest.rb CHANGED
@@ -5,9 +5,9 @@ module Latest
5
5
 
6
6
  class GemNotFoundError < StandardError; end
7
7
 
8
- def self.version(name)
8
+ def self.lookup(name)
9
9
  gem = Gem.new(name)
10
- gem.attributes["version"]
10
+ gem
11
11
  end
12
12
 
13
13
  end
data/test/test_gem.rb CHANGED
@@ -5,10 +5,10 @@ class TestGem < MiniTest::Unit::TestCase
5
5
 
6
6
  def test_should_fetch_on_initialize
7
7
  gem = ::Latest::Gem.new("rake")
8
- assert_equal Hash, gem.attributes.class
9
- assert_equal "rake", gem.attributes["name"]
8
+ assert_equal Array, gem.versions.class
9
+ assert_equal "rake", gem.name
10
10
  end
11
-
11
+
12
12
  def test_raise_error_when_gem_is_not_found
13
13
  assert_raises Latest::GemNotFoundError do
14
14
  ::Latest::Gem.new("some-gem-that-probably-wont-exist-2")
data/test/test_latest.rb CHANGED
@@ -4,6 +4,13 @@ require "latest"
4
4
  class TestLatest < MiniTest::Unit::TestCase
5
5
 
6
6
  BIN = File.expand_path("../../bin/latest", __FILE__)
7
+
8
+ STABLE_VERSION_REGEX = /\d+(\.\d+)*$/ # 11.2.123
9
+ PRERELEASE_VERSION_REGEX = /--pre \d+(\.\d+)*(.[a-z\.\d]+)+$/i # 0.1.23.beta.1
10
+
11
+ def assert_version_match(name, string, prerelease=false)
12
+ assert_match Regexp.new("#{name} #{prerelease ? PRERELEASE_VERSION_REGEX : STABLE_VERSION_REGEX}"), string.strip
13
+ end
7
14
 
8
15
  def test_has_version
9
16
  assert_equal String, Latest::VERSION.class
@@ -24,14 +31,26 @@ class TestLatest < MiniTest::Unit::TestCase
24
31
 
25
32
  def test_executable_fetches_and_prints_version
26
33
  out = `#{BIN} rake`
27
- assert_match /rake \d.\d.\d(.\d)?/, out
34
+ assert_version_match "rake", out
28
35
  end
29
36
 
30
37
  def test_executable_fetches_and_prints_multiple_versions
31
38
  out = `#{BIN} rake rails spree`.split("\n")
32
- assert_match /rake \d.\d.\d(.\d)?/, out[0]
33
- assert_match /rails \d.\d.\d(.\d)?/, out[1]
34
- assert_match /spree \d.\d.\d(.\d)?/, out[2]
39
+ assert_version_match "rake", out[0]
40
+ assert_version_match "rails", out[1]
41
+ assert_version_match "spree", out[2]
42
+ end
43
+
44
+ def test_executable_fetches_and_prints_prerelease_version
45
+ out = `#{BIN} rake --pre`
46
+ assert_version_match "rake", out, true
47
+ end
48
+
49
+ def test_executable_fetches_and_prints_multiple_prerelease_versions
50
+ out = `#{BIN} rake --pre rails --pre spree`.split("\n")
51
+ assert_version_match "rake", out[0], true
52
+ assert_version_match "rails", out[1], true
53
+ assert_version_match "spree", out[2]
35
54
  end
36
55
 
37
56
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: latest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-14 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70259770155560 !ruby/object:Gem::Requirement
16
+ requirement: &70157104359200 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>'
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70259770155560
24
+ version_requirements: *70157104359200
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &70259770154620 !ruby/object:Gem::Requirement
27
+ requirement: &70157104356100 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>'
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70259770154620
35
+ version_requirements: *70157104356100
36
36
  description: Latest keeps us up to speed by querying rubygems.org for a gem's most
37
37
  recent version number. Yep, that's all it does.
38
38
  email:
@@ -73,9 +73,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
73
  - - ! '>='
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
- segments:
77
- - 0
78
- hash: 3157611615867857554
79
76
  requirements: []
80
77
  rubyforge_project: latest
81
78
  rubygems_version: 1.8.10