Class: BitlyQuickly
- Inherits:
-
Object
- Object
- BitlyQuickly
- Defined in:
- lib/bitly_quickly.rb,
lib/bitly_quickly/error.rb,
lib/bitly_quickly/version.rb
Overview
Wrapper around Bitly V3 API.
Defined Under Namespace
Classes: Error
Constant Summary collapse
- DEFAULT_API_ADDRESS =
Bitly API server.
'https://api-ssl.bitly.com'- VERSION =
Version number, happy now?
'0.0.6'
Instance Attribute Summary collapse
-
#access_token ⇒ String
readonly
API Access Token.
-
#api_address ⇒ String
readonly
Alternate API server URL.
Instance Method Summary collapse
-
#endpoint_url(path) ⇒ String
Create endpoint URL.
-
#get_many_responses(array_of_long_urls) ⇒ Hash<String, String>
private
Shorten each URL and return a hash where keys are original URLs and values are shortened URLs.
-
#get_single_response(long_url) ⇒ String
private
Shorten single URL.
-
#initialize(config) ⇒ BitlyQuickly
constructor
Init with access token and api address.
-
#make_shorten_request(long_url) ⇒ Typhoues::Request
private
Prepare Typhoeus shorten request.
-
#parse_json(json) ⇒ Hash
private
Parse JSON string into Ruby hash.
-
#response_to_json(response) ⇒ Hash
private
Check response code and raise an appropriate error.
-
#shorten(long_url_or_array) ⇒ String, Hash<String, String>
Shorten URL or array of URLs.
Constructor Details
#initialize(config) ⇒ BitlyQuickly
Init with access token and api address.
44 45 46 47 48 49 50 |
# File 'lib/bitly_quickly.rb', line 44 def initialize(config) @access_token = config.delete(:access_token) || fail( ArgumentError, 'Missing access_token option' ) @api_address = config.delete(:api_address) || DEFAULT_API_ADDRESS end |
Instance Attribute Details
#access_token ⇒ String (readonly)
API Access Token.
32 33 34 |
# File 'lib/bitly_quickly.rb', line 32 def access_token @access_token end |
#api_address ⇒ String (readonly)
Alternate API server URL.
37 38 39 |
# File 'lib/bitly_quickly.rb', line 37 def api_address @api_address end |
Instance Method Details
#endpoint_url(path) ⇒ String
Create endpoint URL.
72 73 74 |
# File 'lib/bitly_quickly.rb', line 72 def endpoint_url(path) URI.join(api_address, path).to_s end |
#get_many_responses(array_of_long_urls) ⇒ Hash<String, String> (private)
Shorten each URL and return a hash where keys are original URLs and values are shortened URLs.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/bitly_quickly.rb', line 88 def get_many_responses(array_of_long_urls) hydra = Typhoeus::Hydra.new responses = {} array_of_long_urls.each do |long_url| request = make_shorten_request(long_url) request.on_complete do |response| json_response = response_to_json(response) responses[long_url] = json_response[:data][:url] end hydra.queue request end hydra.run responses end |
#get_single_response(long_url) ⇒ String (private)
Shorten single URL.
112 113 114 115 116 117 118 |
# File 'lib/bitly_quickly.rb', line 112 def get_single_response(long_url) request = make_shorten_request(long_url) response = request.run json_response = response_to_json(response) json_response[:data][:url] end |
#make_shorten_request(long_url) ⇒ Typhoues::Request (private)
Prepare Typhoeus shorten request.
125 126 127 128 129 130 131 132 133 |
# File 'lib/bitly_quickly.rb', line 125 def make_shorten_request(long_url) Typhoeus::Request.new( endpoint_url('/v3/shorten'), params: { access_token: access_token, longUrl: long_url } ) end |
#parse_json(json) ⇒ Hash (private)
Parse JSON string into Ruby hash.
165 166 167 |
# File 'lib/bitly_quickly.rb', line 165 def parse_json(json) MultiJson.load(json, symbolize_keys: true) end |
#response_to_json(response) ⇒ Hash (private)
Check response code and raise an appropriate error. Otherwise return parsed JSON body.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/bitly_quickly.rb', line 141 def response_to_json(response) json_response = parse_json(response.body) case json_response[:status_code] when 200 return json_response when 403 fail Error::RateLimitExceeded, json_response[:status_txt] when 404 fail Error::NotFound, json_response[:status_txt] when 500 fail Error::InvalidRequestOrResponse, json_response[:status_txt] when 503 fail Error::TemporarilyUnavailable, json_response[:status_txt] else fail Error::UnknownError, json_response[:status_txt] end end |
#shorten(long_url_or_array) ⇒ String, Hash<String, String>
Shorten URL or array of URLs. In case single URL is passed, it returns the shortened URL. If an array is passed, it returns a hash where keys are original URLs and values are shortened URLs.
59 60 61 62 63 64 65 |
# File 'lib/bitly_quickly.rb', line 59 def shorten(long_url_or_array) if long_url_or_array.respond_to?(:each) get_many_responses(long_url_or_array) else get_single_response(long_url_or_array) end end |