Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject credentials #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions lib/shorturl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ def self.credentials_for(service)
# parameters set so that when +instance+.call is invoked, the
# shortened URL is returned.
SERVICES = {
:tinyurl => Services::TinyURL.new,
:shorl => Services::Shorl.new,
:snipurl => Services::SnipURL.new,
:metamark => Services::Metamark.new,
:minilink => Services::Minilink.new,
:lns => Services::Lns.new,
:moourl => Services::MooURL.new,
:bitly => Services::Bitly.new,
:ur1 => Services::Url.new,
:vurl => Services::Vurl.new,
:isgd => Services::Isgd.new,
:gitio => Services::Gitio.new,
:vamu => Services::Vamu.new,
:tinyurl => Services::TinyURL,
:shorl => Services::Shorl,
:snipurl => Services::SnipURL,
:metamark => Services::Metamark,
:minilink => Services::Minilink,
:lns => Services::Lns,
:moourl => Services::MooURL,
:bitly => Services::Bitly,
:ur1 => Services::Url,
:vurl => Services::Vurl,
:isgd => Services::Isgd,
:gitio => Services::Gitio,
:vamu => Services::Vamu,

# :skinnylink => Service.new("skinnylink.com") { |s|
# s.block = lambda { |body| URI.extract(body).grep(/skinnylink/)[0] }
Expand Down Expand Up @@ -126,9 +126,10 @@ def self.valid_services
# call-seq:
# ShortURL.shorten("http://mypage.com") => Uses TinyURL
# ShortURL.shorten("http://mypage.com", :bitly)
def self.shorten(url, service = :tinyurl)
def self.shorten(url, service = :tinyurl, credentials = nil)
if SERVICES.has_key?(service)
SERVICES[service].call(url)
credentials ||= self.credentials_for(service.to_s)
SERVICES[service].new(credentials).call(url)
else
raise InvalidService
end
Expand Down
2 changes: 1 addition & 1 deletion lib/shorturl/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Service
# return code, the form method to use, the form action, the form
# field which contains the long URL, and the block of what to do
# with the HTML code you get.
def initialize(hostname) # :yield: service
def initialize(hostname, creds = nil) # :yield: service
@hostname = hostname
@port = 80
@code = 200
Expand Down
3 changes: 1 addition & 2 deletions lib/shorturl/services/bitly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ module ShortURL
module Services
class Bitly < Service

def initialize
def initialize(creds = nil)
super("api-ssl.bitly.com")

@method = :get
@port = 443
@ssl = true
@action = "/v3/shorten/"

creds = ShortURL.credentials_for('bitly')
username = creds['username']
key = creds['key']

Expand Down
2 changes: 1 addition & 1 deletion lib/shorturl/services/gitio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ShortURL
module Services
class Gitio < Service

def initialize
def initialize(creds = nil)
super('git.io')

@code = 201
Expand Down
2 changes: 1 addition & 1 deletion lib/shorturl/services/isgd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ShortURL
module Services
class Isgd < Service

def initialize
def initialize(creds = nil)
super('is.gd')

@method = :get
Expand Down
2 changes: 1 addition & 1 deletion lib/shorturl/services/lns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module ShortURL
module Services
class Lns < Service

def initialize
def initialize(creds = nil)
super("ln-s.net")

@method = :get
Expand Down
2 changes: 1 addition & 1 deletion lib/shorturl/services/metamark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ShortURL
module Services
class Metamark < Service

def initialize
def initialize(creds = nil)
super("metamark.net")

@action = "/add"
Expand Down
2 changes: 1 addition & 1 deletion lib/shorturl/services/minilink.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module ShortURL
module Services
class Minilink < Service

def initialize
def initialize(creds = nil)
super("minilink.org")

@method = :get
Expand Down
2 changes: 1 addition & 1 deletion lib/shorturl/services/moourl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ShortURL
module Services
class MooURL < Service

def initialize
def initialize(creds = nil)
super("moourl.com")

@code = 302
Expand Down
2 changes: 1 addition & 1 deletion lib/shorturl/services/shorl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ShortURL
module Services
class Shorl < Service

def initialize
def initialize(creds = nil)
super("shorl.com")

@method = :get
Expand Down
2 changes: 1 addition & 1 deletion lib/shorturl/services/snipurl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Services
# registration.
class SnipURL < Service

def initialize
def initialize(creds = nil)
super("snipurl.com")

@action = "/site/index"
Expand Down
2 changes: 1 addition & 1 deletion lib/shorturl/services/tinyurl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ShortURL
module Services
class TinyURL < Service

def initialize
def initialize(creds = nil)
super('tinyurl.com')

@action = "/api-create.php"
Expand Down
2 changes: 1 addition & 1 deletion lib/shorturl/services/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ShortURL
module Services
class Url < Service

def initialize
def initialize(creds = nil)
super("url.ca")

@method = :post
Expand Down
2 changes: 1 addition & 1 deletion lib/shorturl/services/vamu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ShortURL
module Services
class Vamu < Service

def initialize
def initialize(creds = nil)
super('va.mu')

@method = :get
Expand Down
2 changes: 1 addition & 1 deletion lib/shorturl/services/vurl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ShortURL
module Services
class Vurl < Service

def initialize
def initialize(creds = nil)
super("vurl.me")

@method = :get
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
gem 'rspec', '~> 2.4'
gem 'rspec', '~> 2.6'
require 'rspec'
require 'shorturl'

Expand Down