Class: Bitly

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

Constant Summary collapse

REST_API_URL =
"http://api.bit.ly"
ACTION_PATH =
{ :shorten => '/v3/shorten', :expand => '/v3/expand', :clicks => '/v3/clicks' }

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.keyObject

Returns the value of attribute key.



13
14
15
# File 'lib/ruby-bitly.rb', line 13

def key
  @key
end

.loginObject

Returns the value of attribute login.



13
14
15
# File 'lib/ruby-bitly.rb', line 13

def 
  @login
end

Class Method Details

.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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ruby-bitly.rb', line 70

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



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

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

.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



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
# File 'lib/ruby-bitly.rb', line 29

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