class UrlExpander::Expanders::Bitly
Expand Bitly
URLS Usage: UrlExpander::Client.expand
(“bit.ly/qpshuI”) UrlExpander::Client.expand
(“j.mp/qpshuI”) UrlExpander::Client.expand
(“nyti.ms/dzy2b7”) UrlExpander::Client.expand
(“tcrn.ch/oe50JN”) UrlExpander::Client.expand
(“fxn.ws/pBewvL”)
Constants
- DOMAINS
Bitly
has an undocumented api to get all the bitly_domains used across the web. The data is given back in md5 format for security reasons.- PATTERN
Note: Don't use this for a global matching, it will match all urls. Instead we have a custom matching function def self.is_me?(url)
Attributes
api_key[RW]
login[RW]
parent_klass[R]
Public Class Methods
is_me?(url)
click to toggle source
A custom pattern matching function
# File lib/url_expander/expanders/api/bitly.rb, line 38 def self.is_me?(url) if url.match(PATTERN) domain_digest = Digest::MD5.hexdigest($1) DOMAINS.include?(domain_digest) else false end end
new(short_url, options={})
click to toggle source
Calls superclass method
UrlExpander::Expanders::API::new
# File lib/url_expander/expanders/api/bitly.rb, line 25 def initialize(short_url, options={}) @parent_klass = self load_credentials(options[:config_file]) if @login.nil? || @api_key.nil? raise ArgumentError.new('Bitly Expander require login, api_key. Run "rake url_expander:generate_config"') end super(short_url,options) fetch_url end
Private Instance Methods
fetch_url()
click to toggle source
# File lib/url_expander/expanders/api/bitly.rb, line 55 def fetch_url data = JSON.parse Request.get("/v3/expand?hash=#{@shortner_key}&login=#{@login}&apiKey=#{@api_key}").response.body expand = data['data']['expand'].first if(data['status_code'] == 200 && !expand.has_key?('error')) @long_url = expand["long_url"] else raise UrlExpander::Error.new(expand['error'],data['status_code']) end end
load_credentials(file_path)
click to toggle source
# File lib/url_expander/expanders/api/bitly.rb, line 65 def load_credentials(file_path) unless File.exists?(file_path) raise ArgumentError.new('Bitly Expander require login, api_key. Run "rake url_expander:generate_config"') else credentials = YAML.load_file(file_path)[:bitly] @login = credentials[:login] @api_key = credentials[:api_key] end end