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, key = self.key) ⇒ Object

Old API:

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

New API:

expand(options)

Options can have:

:long_url :login :api_key



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ruby-bitly.rb', line 91

def expand(short_url,  = self., key = self.key)
  if short_url.is_a?(Hash)
    options = short_url
    short_url = options[:short_url]
     = options[:login] || self.
    key = options[:api_key] || self.key
  else
    options = {}
  end

  response = JSON.parse RestClient.post(rest_api_url + ACTION_PATH[:expand], { :shortURL => short_url, :login => , :apiKey => key })

  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, key = self.key) ⇒ Object

Old API:

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

New API:

get_clicks(options)

Options can have:

:long_url :login :api_key



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ruby-bitly.rb', line 124

def get_clicks(short_url,  = self., key = self.key)
  if short_url.is_a?(Hash)
    options = short_url
    short_url = options[:short_url]
     = options[:login] || self.
    key = options[:api_key] || self.key
  else
    options = {}
  end

  response = JSON.parse RestClient.get("#{rest_api_url}#{ACTION_PATH[:clicks]}?login=#{}&apiKey=#{key}&shortUrl=#{short_url}")

  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, key = self.key) ⇒ Object

Old API:

shorten(long_url, login = self.login, key = self.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
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ruby-bitly.rb', line 50

def shorten(long_url,  = self., key = self.key)
  if long_url.is_a?(Hash)
    options = long_url
    long_url = options[:long_url]
     = options[:login] || self.
    key = options[:api_key] || self.key
  else
    options = {}
  end

  params = { :longURL => long_url, :login => , :apiKey => key }

  if options[:domain]
    params[:domain] = options[:domain]
  end

  response = JSON.parse RestClient.post(rest_api_url + ACTION_PATH[:shorten], params)
  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