shortn-url 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/shortn-url.rb +44 -0
  2. metadata +55 -0
data/lib/shortn-url.rb ADDED
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # file: shortn-url.rb
4
+
5
+ require 'nokogiri'
6
+
7
+ class ShortnUrl
8
+
9
+ def initialize(filepath)
10
+ @filepath = filepath
11
+ @doc = Nokogiri::XML(File.open(filepath,'r').read)
12
+ end
13
+
14
+ # fetch the short url if it already exists otherwise use a new short url
15
+ def full_url(s)
16
+
17
+ short_url = @doc.xpath("urls/records/url[full_url='#{s}']/short_url").text
18
+ if short_url.empty? then
19
+ node = @doc.xpath("urls/records/url[full_url='']")[0]
20
+ node.xpath('full_url')[0].content = s
21
+
22
+ node['last_modified'] = Time.now.to_s
23
+ save()
24
+ node.xpath('short_url').text
25
+ else
26
+ short_url
27
+ end
28
+
29
+ end
30
+
31
+ def short_url(s)
32
+ @doc.xpath("urls/records/url[short_url='#{s}']/full_url").text
33
+ end
34
+
35
+ private
36
+
37
+ def save()
38
+ File.open(@filepath,'w') do |f|
39
+ f.write @doc.serialize(:encoding => 'UTF-8') do |config|
40
+ config.format.as_xml
41
+ end
42
+ end
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shortn-url
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors: []
7
+
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-10-13 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email:
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/shortn-url.rb
26
+ has_rdoc: true
27
+ homepage:
28
+ licenses: []
29
+
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.3.5
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: shortn-url
54
+ test_files: []
55
+