guillotine 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/guillotine.gemspec CHANGED
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
13
13
  ## If your rubyforge_project name is different, then edit it and comment out
14
14
  ## the sub! line in the Rakefile
15
15
  s.name = 'guillotine'
16
- s.version = '0.1.2'
17
- s.date = '2011-08-17'
16
+ s.version = '0.2.0'
17
+ s.date = '2011-08-18'
18
18
  s.rubyforge_project = 'guillotine'
19
19
 
20
20
  ## Make sure your summary is short. The description may be as long
@@ -43,6 +43,26 @@ module Guillotine
43
43
  end
44
44
  end
45
45
 
46
+ # Public: Retrieves the code for a given URL.
47
+ #
48
+ # url - The String URL to lookup.
49
+ #
50
+ # Returns the String code, or nil if none is found.
51
+ def code_for(url)
52
+ if row = Url.select(:code).where(:url => url).first
53
+ row[:code]
54
+ end
55
+ end
56
+
57
+ # Public: Removes the assigned short code for a URL.
58
+ #
59
+ # url - The String URL to remove.
60
+ #
61
+ # Returns nothing.
62
+ def clear(url)
63
+ Url.where(:url => url).delete
64
+ end
65
+
46
66
  def setup
47
67
  conn = Url.connection
48
68
  conn.create_table :urls do |t|
@@ -32,10 +32,30 @@ module Guillotine
32
32
  #
33
33
  # code - The String code to lookup the URL.
34
34
  #
35
- # Returns the String URL.
35
+ # Returns the String URL, or nil if none is found.
36
36
  def find(code)
37
37
  @hash[code]
38
38
  end
39
+
40
+ # Public: Retrieves the code for a given URL.
41
+ #
42
+ # url - The String URL to lookup.
43
+ #
44
+ # Returns the String code, or nil if none is found.
45
+ def code_for(url)
46
+ @urls[url]
47
+ end
48
+
49
+ # Public: Removes the assigned short code for a URL.
50
+ #
51
+ # url - The String URL to remove.
52
+ #
53
+ # Returns nothing.
54
+ def clear(url)
55
+ if code = @urls.delete(url)
56
+ @hash.delete code
57
+ end
58
+ end
39
59
  end
40
60
  end
41
61
  end
@@ -24,7 +24,7 @@ module Guillotine
24
24
  # Returns the unique String code for the URL. If the URL is added
25
25
  # multiple times, this should return the same code.
26
26
  def add(url, code = nil)
27
- sha = Digest::SHA1.hexdigest url
27
+ sha = url_key url
28
28
  url_obj = @url_bucket.get_or_new sha, :r => 1
29
29
  url_obj.data || begin
30
30
  code ||= shorten url
@@ -47,12 +47,60 @@ module Guillotine
47
47
  #
48
48
  # Returns the String URL.
49
49
  def find(code)
50
- if obj = @code_bucket.get(code, :r => 1)
50
+ if obj = url_object(code)
51
51
  obj.data
52
52
  end
53
+ end
54
+
55
+ # Public: Retrieves the code for a given URL.
56
+ #
57
+ # url - The String URL to lookup.
58
+ #
59
+ # Returns the String code, or nil if none is found.
60
+ def code_for(url)
61
+ if obj = code_object(url)
62
+ obj.data
63
+ end
64
+ end
65
+
66
+ # Public: Removes the assigned short code for a URL.
67
+ #
68
+ # url - The String URL to remove.
69
+ #
70
+ # Returns nothing.
71
+ def clear(url)
72
+ if code_obj = code_object(url)
73
+ @url_bucket.delete code_obj.key
74
+ @code_bucket.delete code_obj.data
75
+ end
76
+ end
77
+
78
+ # Retrieves a URL riak value from the code.
79
+ #
80
+ # code - The String code to lookup the URL.
81
+ #
82
+ # Returns a Riak::RObject, or nil if none is found.
83
+ def url_object(code)
84
+ @code_bucket.get(code, :r => 1)
85
+ rescue Riak::FailedRequest => err
86
+ raise unless err.not_found?
87
+ end
88
+
89
+ # Retrieves the code riak value for a given URL.
90
+ #
91
+ # url - The String URL to lookup.
92
+ #
93
+ # Returns a Riak::RObject, or nil if none is found.
94
+ def code_object(url)
95
+ sha = url_key url
96
+ @url_bucket.get(sha, :r => 1)
53
97
  rescue Riak::FailedRequest => err
54
98
  raise unless err.not_found?
55
99
  end
100
+
101
+ def url_key(url)
102
+ Digest::SHA1.hexdigest url
103
+ end
56
104
  end
57
105
  end
58
106
  end
@@ -42,6 +42,26 @@ module Guillotine
42
42
  end
43
43
  end
44
44
 
45
+ # Public: Retrieves the code for a given URL.
46
+ #
47
+ # url - The String URL to lookup.
48
+ #
49
+ # Returns the String code, or nil if none is found.
50
+ def code_for(url)
51
+ if row = @table.select(:code).where(:url => url).first
52
+ row[:code]
53
+ end
54
+ end
55
+
56
+ # Public: Removes the assigned short code for a URL.
57
+ #
58
+ # url - The String URL to remove.
59
+ #
60
+ # Returns nothing.
61
+ def clear(url)
62
+ @table.where(:url => url).delete
63
+ end
64
+
45
65
  def setup
46
66
  @db.create_table :urls do
47
67
  string :url
data/lib/guillotine.rb CHANGED
@@ -2,7 +2,7 @@ require 'base64'
2
2
  require 'digest/md5'
3
3
 
4
4
  module Guillotine
5
- VERSION = "0.1.2"
5
+ VERSION = "0.2.0"
6
6
 
7
7
  dir = File.expand_path '../guillotine', __FILE__
8
8
  autoload :App, "#{dir}/app"
@@ -35,6 +35,20 @@ begin
35
35
  def test_missing_code
36
36
  assert_nil @db.find('missing')
37
37
  end
38
+
39
+ def test_gets_code_for_url
40
+ code = @db.add 'abc'
41
+ assert_equal code, @db.code_for('abc')
42
+ end
43
+
44
+ def test_clears_code_for_url
45
+ code = @db.add 'abc'
46
+ assert_equal 'abc', @db.find(code)
47
+
48
+ @db.clear 'abc'
49
+
50
+ assert_nil @db.find(code)
51
+ end
38
52
  end
39
53
  rescue LoadError
40
54
  puts "skipping ActiveRecord tests: #{$!}"
@@ -30,4 +30,18 @@ class MemoryAdapterTest < Guillotine::TestCase
30
30
  def test_missing_code
31
31
  assert_nil @db.find('missing')
32
32
  end
33
+
34
+ def test_gets_code_for_url
35
+ code = @db.add 'abc'
36
+ assert_equal code, @db.code_for('abc')
37
+ end
38
+
39
+ def test_clears_code_for_url
40
+ code = @db.add 'abc'
41
+ assert_equal 'abc', @db.find(code)
42
+
43
+ @db.clear 'abc'
44
+
45
+ assert_nil @db.find(code)
46
+ end
33
47
  end
@@ -58,6 +58,29 @@ begin
58
58
  def test_missing_code
59
59
  assert_nil @db.find('missing')
60
60
  end
61
+
62
+ def test_gets_code_for_url
63
+ code = @db.add 'abc'
64
+ assert_equal code, @db.code_for('abc')
65
+ end
66
+
67
+ def test_clears_code_for_url
68
+ code = @db.add 'abc'
69
+ assert_equal 'abc', @db.find(code)
70
+
71
+ @db.clear 'abc'
72
+
73
+ assert_nil @db.find(code)
74
+ end
75
+
76
+ def test_clears_code_for_url
77
+ code = @db.add 'abc'
78
+ assert_equal 'abc', @db.find(code)
79
+
80
+ @db.clear 'abc'
81
+
82
+ assert_nil @db.find(code)
83
+ end
61
84
  end
62
85
  rescue LoadError
63
86
  puts "Skipping Riak tests: #{$!}"
@@ -35,6 +35,11 @@ begin
35
35
  def test_missing_code
36
36
  assert_nil @db.find('missing')
37
37
  end
38
+
39
+ def test_gets_code_for_url
40
+ code = @db.add 'abc'
41
+ assert_equal code, @db.code_for('abc')
42
+ end
38
43
  end
39
44
  rescue LoadError
40
45
  puts "Skipping sequel tests"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guillotine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-17 00:00:00.000000000Z
12
+ date: 2011-08-18 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sinatra
16
- requirement: &70121188810040 !ruby/object:Gem::Requirement
16
+ requirement: &70098598402440 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.2.6
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70121188810040
24
+ version_requirements: *70098598402440
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: addressable
27
- requirement: &70121188809580 !ruby/object:Gem::Requirement
27
+ requirement: &70098598401980 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 2.2.6
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70121188809580
35
+ version_requirements: *70098598401980
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rack-test
38
- requirement: &70121188809200 !ruby/object:Gem::Requirement
38
+ requirement: &70098598401600 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70121188809200
46
+ version_requirements: *70098598401600
47
47
  description: Adaptable private URL shortener
48
48
  email: technoweenie@gmail.com
49
49
  executables: []