Class: BitlyApi::Bitly

Inherits:
Object
  • Object
show all
Defined in:
lib/bitly-api/bitly-api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Bitly

Returns a new instance of Bitly.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
# File 'lib/bitly-api/bitly-api.rb', line 10

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. = options[:login]
  self.api_key = options[:api_key]
  self.api_version = options[:version]
  @httpclient = HTTPClient.new
end

Instance Attribute Details

#api_keyObject

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_versionObject

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

#loginObject

Returns the value of attribute login.



6
7
8
# File 'lib/bitly-api/bitly-api.rb', line 6

def 
  @login
end

Instance Method Details

#errorsObject

Raises:



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=#{}&apiKey=#{api_key}")
  data = JSON.parse(http_response)
  raise BitlyError.new(data["errorMessage"]) unless data["statusCode"] == "OK"
  data["results"]
end

#expand(short_url) ⇒ Object

Raises:



28
29
30
31
32
33
# File 'lib/bitly-api/bitly-api.rb', line 28

def expand(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

Raises:



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.

Raises:



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

Raises:



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