Class: BitlyApi::Bitly
- Inherits:
-
Object
- Object
- BitlyApi::Bitly
- Defined in:
- lib/bitly-api/bitly-api.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#api_version ⇒ Object
Returns the value of attribute api_version.
-
#login ⇒ Object
Returns the value of attribute login.
Instance Method Summary collapse
- #errors ⇒ Object
- #expand(short_url) ⇒ Object
- #info(short_url) ⇒ Object
-
#initialize(options = {}) ⇒ Bitly
constructor
A new instance of Bitly.
-
#shorten(long_url) ⇒ Object
shorten
long_url
. - #stats(short_url) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Bitly
Returns a new instance of Bitly.
10 11 12 13 14 15 16 17 18 |
# File 'lib/bitly-api/bitly-api.rb', line 10 def initialize( = {}) raise ArgumentError.new(":login and :api_key are required") if ([:login].nil? or [:api_key].nil?) [:version] = "2.0.1" if [:version].nil? self.login = [:login] self.api_key = [:api_key] self.api_version = [:version] @httpclient = HTTPClient.new end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
7 8 9 |
# File 'lib/bitly-api/bitly-api.rb', line 7 def api_key @api_key end |
#api_version ⇒ Object
Returns the value of attribute api_version.
8 9 10 |
# File 'lib/bitly-api/bitly-api.rb', line 8 def api_version @api_version end |
#login ⇒ Object
Returns the value of attribute login.
6 7 8 |
# File 'lib/bitly-api/bitly-api.rb', line 6 def login @login end |
Instance Method Details
#errors ⇒ Object
49 50 51 52 53 54 |
# File 'lib/bitly-api/bitly-api.rb', line 49 def errors http_response = @httpclient.get_content("http://api.bit.ly/errors?version=#{api_version}&login=#{login}&apiKey=#{api_key}") data = JSON.parse(http_response) raise BitlyError.new(data["errorMessage"]) unless data["statusCode"] == "OK" data["results"] end |
#expand(short_url) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/bitly-api/bitly-api.rb', line 28 def (short_url) http_response = @httpclient.get_content(build_url("expand", "shortUrl=#{short_url}")) data = JSON.parse(http_response) raise BitlyError.new(data["errorMessage"]) unless data["statusCode"] == "OK" data["results"][short_url] end |
#info(short_url) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/bitly-api/bitly-api.rb', line 35 def info(short_url) http_response = @httpclient.get_content(build_url("info", "shortUrl=#{short_url}")) data = JSON.parse(http_response) raise BitlyError.new(data["errorMessage"]) unless data["statusCode"] == "OK" data["results"][short_url.split(/\//)[-1]] end |
#shorten(long_url) ⇒ Object
shorten long_url
. long_url
is CGI escaped, so you shouldn’t escape it yourself.
21 22 23 24 25 26 |
# File 'lib/bitly-api/bitly-api.rb', line 21 def shorten(long_url) http_response = @httpclient.get_content(build_url("shorten", "longUrl=#{CGI::escape(long_url)}")) data = JSON.parse(http_response) raise BitlyError.new(data["errorMessage"]) unless data["statusCode"] == "OK" data["results"][long_url] end |
#stats(short_url) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/bitly-api/bitly-api.rb', line 42 def stats(short_url) http_response = @httpclient.get_content(build_url("stats", "shortUrl=#{short_url}")) data = JSON.parse(http_response) raise BitlyError.new(data["errorMessage"]) unless data["statusCode"] == "OK" data["results"] end |