Class: Bitly

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

Constant Summary collapse

REST_API_URL =
"http://api.bitly.com"
REST_API_URL_SSL =
"https://api-ssl.bitly.com"
ACTION_PATH =
{ :shorten => '/v3/shorten', :expand => '/v3/expand', :clicks => '/v3/clicks' }

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_keyObject Also known as: key

Returns the value of attribute api_key.



15
16
17
# File 'lib/ruby-bitly.rb', line 15

def api_key
  @api_key
end

.loginObject

Returns the value of attribute login.



15
16
17
# File 'lib/ruby-bitly.rb', line 15

def 
  @login
end

.use_sslObject



20
21
22
# File 'lib/ruby-bitly.rb', line 20

def use_ssl
  instance_variable_defined?(:@use_ssl) ? @use_ssl : true
end

Class Method Details

.config {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Bitly)

    the object that the method was called on



32
33
34
# File 'lib/ruby-bitly.rb', line 32

def config
  yield self
end

.expand(short_url, login = self.login, api_key = self.api_key) ⇒ Object

Old API:

expand(short_url, login = self.login, api_key = self.api_key)

New API:

expand(options)

Options can have:

:long_url :login :api_key



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ruby-bitly.rb', line 79

def expand(short_url,  = self., api_key = self.api_key)
  options = ensure_options(url: short_url, login: , api_key: api_key)
  options.select! { |k,_v| [:shortURL, :login, :apiKey].include?(k) }

  response = JSON.parse RestClient.post(rest_api_url + ACTION_PATH[:expand], options)

  bitly = new(response["data"]["expand"].first)
  bitly.status_code = response["status_code"]
  bitly.status_txt = response["status_txt"]
  bitly.long_url = bitly.error if bitly.error

  bitly
end

.get_clicks(short_url, login = self.login, api_key = self.api_key) ⇒ Object

Old API:

get_clicks(short_url, login = self.login, api_key = self.api_key)

New API:

get_clicks(options)

Options can have:

:long_url :login :api_key



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ruby-bitly.rb', line 106

def get_clicks(short_url,  = self., api_key = self.api_key)
  options = ensure_options(url: short_url, login: , api_key: api_key)
  options.select! { |k,_v| [:shortURL, :login, :apiKey].include?(k) }
  options[:shortUrl] = options.delete(:shortURL)

  response = JSON.parse RestClient.get("#{rest_api_url}#{ACTION_PATH[:clicks]}", params: options)

  bitly = new(response["data"]["clicks"].first)
  bitly.status_code = response["status_code"]
  bitly.status_txt = response["status_txt"]

  bitly
end

.proxyObject



28
29
30
# File 'lib/ruby-bitly.rb', line 28

def proxy
  RestClient.proxy
end

.proxy=(addr) ⇒ Object



24
25
26
# File 'lib/ruby-bitly.rb', line 24

def proxy=(addr)
  RestClient.proxy = addr
end

.shorten(long_url, login = self.login, api_key = self.api_key) ⇒ Object

Old API:

shorten(long_url, login = self.login, api_key = self.api_key)

New API:

shorten(options)

Options can have:

:long_url :login :api_key :domain



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ruby-bitly.rb', line 50

def shorten(long_url,  = self., api_key = self.api_key)
  options = ensure_options(url: long_url, login: , api_key: api_key)
  options.select! { |k,_v| [:longURL, :domain, :login, :apiKey].include?(k) }

  response = JSON.parse RestClient.post(rest_api_url + ACTION_PATH[:shorten], options)
  response.delete("data") if response["data"].empty?

  bitly = new response["data"]

  bitly.hash_path = response["data"]["hash"] if response["status_code"] == 200
  bitly.status_code = response["status_code"]
  bitly.status_txt = response["status_txt"]

  bitly
end