Class: Tinyurl
- Inherits:
-
Object
- Object
- Tinyurl
- Defined in:
- lib/tinyurl.rb
Constant Summary collapse
- VERSION =
'1.0.0'
Instance Attribute Summary collapse
-
#original ⇒ Object
readonly
Returns the value of attribute original.
-
#tiny ⇒ Object
readonly
Returns the value of attribute tiny.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(url) ⇒ Tinyurl
constructor
A new instance of Tinyurl.
- #is_tiny? ⇒ Boolean
- #resolve(tinyurl) ⇒ Object
- #tinyurlize(longurl) ⇒ Object
Constructor Details
#initialize(url) ⇒ Tinyurl
Returns a new instance of Tinyurl.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/tinyurl.rb', line 10 def initialize ( url ) @url = url if self.is_tiny? @tiny = @url @original = resolve(@url) else @original = @url @tiny = tinyurlize(@url) end end |
Instance Attribute Details
#original ⇒ Object (readonly)
Returns the value of attribute original.
8 9 10 |
# File 'lib/tinyurl.rb', line 8 def original @original end |
#tiny ⇒ Object (readonly)
Returns the value of attribute tiny.
8 9 10 |
# File 'lib/tinyurl.rb', line 8 def tiny @tiny end |
#url ⇒ Object
Returns the value of attribute url.
7 8 9 |
# File 'lib/tinyurl.rb', line 7 def url @url end |
Instance Method Details
#is_tiny? ⇒ Boolean
38 39 40 |
# File 'lib/tinyurl.rb', line 38 def is_tiny? return true if URI.parse( @url ).host == "tinyurl.com" end |
#resolve(tinyurl) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/tinyurl.rb', line 21 def resolve ( tinyurl ) original = URI.parse( "http://logankoester.com/tools/urls.php?action=reverse_tinyurl&url=#{tinyurl}" ).read if original.empty? raise "TinyURL \"#{tinyurl}\" does not exist" else return original end end |
#tinyurlize(longurl) ⇒ Object
32 33 34 35 36 |
# File 'lib/tinyurl.rb', line 32 def tinyurlize ( longurl ) URI.parse( "http://logankoester.com/tools/urls.php?action=tinyurl&url=#{longurl}" ).read end |