Module: ShortURL
- Defined in:
- lib/shorturl.rb,
lib/shorturl/service.rb,
lib/shorturl/version.rb,
lib/shorturl/exceptions.rb,
lib/shorturl/services/lns.rb,
lib/shorturl/services/url.rb,
lib/shorturl/services/vurl.rb,
lib/shorturl/services/bitly.rb,
lib/shorturl/services/shorl.rb,
lib/shorturl/services/moourl.rb,
lib/shorturl/services/shiturl.rb,
lib/shorturl/services/snipurl.rb,
lib/shorturl/services/tinyurl.rb,
lib/shorturl/services/metamark.rb,
lib/shorturl/services/minilink.rb,
lib/shorturl/services/shortify.rb
Defined Under Namespace
Modules: Services Classes: InvalidService, Service, ServiceNotAvailable
Constant Summary collapse
- CREDENTIALS_PATH =
File.join(Gem.user_home,'.shorturl')
- SERVICES =
Hash table of all the supported services. The key is a symbol representing the service (usually the hostname minus the .com, .net, etc.) The value is an instance of Service with all the parameters set so that when
instance
.call is invoked, the shortened URL is returned. { :tinyurl => Services::TinyURL.new, :shorl => Services::Shorl.new, :snipurl => Services::SnipURL.new, :metamark => Services::Metamark.new, :minilink => Services::Minilink.new, :lns => Services::Lns.new, :shiturl => Services::ShitURL.new, :shortify => Services::Shortify.new, :moourl => Services::MooURL.new, :bitly => Services::Bitly.new, :ur1 => Services::Url.new, :vurl => Services::Vurl.new, # :skinnylink => Service.new("skinnylink.com") { |s| # s.block = lambda { |body| URI.extract(body).grep(/skinnylink/)[0] } # }, # :linktrim => Service.new("linktrim.com") { |s| # s.method = :get # s.action = "/lt/generate" # s.block = lambda { |body| URI.extract(body).grep(/\/linktrim/)[1] } # }, # :shorterlink => Service.new("shorterlink.com") { |s| # s.method = :get # s.action = "/add_url.html" # s.block = lambda { |body| URI.extract(body).grep(/shorterlink/)[0] } # }, # :fyad => Service.new("fyad.org") { |s| # s.method = :get # s.block = lambda { |body| URI.extract(body).grep(/fyad.org/)[2] } # }, # :d62 => Service.new("d62.net") { |s| # s.method = :get # s.block = lambda { |body| URI.extract(body)[0] } # }, # :littlink => Service.new("littlink.com") { |s| # s.block = lambda { |body| URI.extract(body).grep(/littlink/)[0] } # }, # :clipurl => Service.new("clipurl.com") { |s| # s.action = "/create.asp" # s.block = lambda { |body| URI.extract(body).grep(/clipurl/)[0] } # }, # :orz => Service.new("0rz.net") { |s| # s.action = "/create.php" # s.block = lambda { |body| URI.extract(body).grep(/0rz/)[0] } # }, # :urltea => Service.new("urltea.com") { |s| # s.method = :get # s.action = "/create/" # s.block = lambda { |body| URI.extract(body).grep(/urltea/)[6] } # } }
- VERSION =
'1.0.0'
Class Method Summary collapse
- .credentials ⇒ Object
- .credentials_for(service) ⇒ Object
-
.shorten(url, service = :tinyurl) ⇒ Object
Main method of ShortURL, its usage is quite simple, just give an url to shorten and an optional service.
-
.valid_services ⇒ Object
Array containing symbols representing all the implemented URL shortening services.
Class Method Details
.credentials ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/shorturl.rb', line 13 def self.credentials @credentials ||= begin if File.file?(CREDENTIALS_PATH) YAML.load_file(CREDENTIALS_PATH) else {} end end end |
.credentials_for(service) ⇒ Object
23 24 25 |
# File 'lib/shorturl.rb', line 23 def self.credentials_for(service) credentials.fetch(service,{}) end |
.shorten(url, service = :tinyurl) ⇒ Object
Main method of ShortURL, its usage is quite simple, just give an url to shorten and an optional service. If no service is selected, RubyURL.com will be used. An invalid service symbol will raise an ArgumentError exception
Valid service
values:
-
:tinyurl
-
:shorl
-
:snipurl
-
:metamark
-
:makeashorterlink
-
:skinnylink
-
:linktrim
-
:shorterlink
-
:minlink
-
:lns
-
:fyad
-
:d62
-
:shiturl
-
:littlink
-
:clipurl
-
:shortify
-
:orz
call-seq:
ShortURL.shorten("http://mypage.com") => Uses TinyURL
ShortURL.shorten("http://mypage.com", :bitly)
127 128 129 130 131 132 133 |
# File 'lib/shorturl.rb', line 127 def self.shorten(url, service = :tinyurl) if SERVICES.has_key?(service) SERVICES[service].call(url) else raise InvalidService end end |
.valid_services ⇒ Object
Array containing symbols representing all the implemented URL shortening services
95 96 97 |
# File 'lib/shorturl.rb', line 95 def self.valid_services SERVICES.keys end |