/
bitly-api.rb
59 lines (50 loc) · 1.99 KB
/
bitly-api.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
module BitlyApi
class BitlyError < StandardError
end
class Bitly
attr_accessor :login
attr_accessor :api_key
attr_accessor :api_version
def initialize(options = {})
raise ArgumentError.new(":login and :api_key are required") if (options[:login].nil? or options[:api_key].nil?)
options[:version] = "2.0.1" if options[:version].nil?
self.login = options[:login]
self.api_key = options[:api_key]
self.api_version = options[:version]
end
def shorten(long_url)
http_response = open(build_url("shorten", "longUrl=#{long_url}")).read
data = JSON.parse(http_response)
raise BitlyError.new(data["errorMessage"]) unless data["statusCode"] == "OK"
data["results"][long_url]
end
def expand(short_url)
http_response = open(build_url("expand", "shortUrl=#{short_url}")).read
data = JSON.parse(http_response)
raise BitlyError.new(data["errorMessage"]) unless data["statusCode"] == "OK"
data["results"][short_url]
end
def info(short_url)
http_response = open(build_url("info", "shortUrl=#{short_url}")).read
data = JSON.parse(http_response)
raise BitlyError.new(data["errorMessage"]) unless data["statusCode"] == "OK"
data["results"][short_url.split(/\//)[-1]]
end
def stats(short_url)
http_response = open(build_url("stats", "shortUrl=#{short_url}")).read
data = JSON.parse(http_response)
raise BitlyError.new(data["errorMessage"]) unless data["statusCode"] == "OK"
data["results"]
end
def errors
http_response = open("http://api.bit.ly/errors?version=#{api_version}&login=#{login}&apiKey=#{api_key}").read
data = JSON.parse(http_response)
raise BitlyError.new(data["errorMessage"]) unless data["statusCode"] == "OK"
data["results"]
end
private
def build_url(api_method, api_ressource)
"http://api.bit.ly/#{api_method}?version=#{api_version}&login=#{login}&apiKey=#{api_key}&#{api_ressource}"
end
end
end