Class: Bitly
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- Bitly
- 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
-
.key ⇒ Object
Returns the value of attribute key.
-
.login ⇒ Object
Returns the value of attribute login.
Class Method Summary collapse
-
.expand(short_url, login = self.login, key = self.key) ⇒ Object
Old API:.
-
.get_clicks(short_url, login = self.login, key = self.key) ⇒ Object
Old API:.
-
.shorten(long_url, login = self.login, key = self.key) ⇒ Object
Old API:.
Class Attribute Details
.key ⇒ Object
Returns the value of attribute key.
13 14 15 |
# File 'lib/ruby-bitly.rb', line 13 def key @key end |
.login ⇒ Object
Returns the value of attribute login.
13 14 15 |
# File 'lib/ruby-bitly.rb', line 13 def login @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 (short_url, login = self.login, key = self.key) if short_url.is_a?(Hash) = short_url short_url = [:short_url] login = [:login] || self.login key = [:api_key] || self.key else = {} end response = JSON.parse RestClient.post(REST_API_URL + ACTION_PATH[:expand], { :shortURL => short_url, :login => 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, login = self.login, key = self.key) if short_url.is_a?(Hash) = short_url short_url = [:short_url] login = [:login] || self.login key = [:api_key] || self.key else = {} end response = JSON.parse RestClient.get("#{REST_API_URL}#{ACTION_PATH[:clicks]}?login=#{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, login = self.login, key = self.key) if long_url.is_a?(Hash) = long_url long_url = [:long_url] login = [:login] || self.login key = [:api_key] || self.key else = {} end params = { :longURL => long_url, :login => login, :apiKey => key } if [:domain] params[:domain] = [: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 |