Class: Bitly
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- Bitly
- 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
-
.api_key ⇒ Object
(also: key)
Returns the value of attribute api_key.
-
.login ⇒ Object
Returns the value of attribute login.
- .use_ssl ⇒ Object
Class Method Summary collapse
- .config {|_self| ... } ⇒ Object
-
.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:.
- .proxy ⇒ Object
- .proxy=(addr) ⇒ Object
-
.shorten(long_url, login = self.login, key = self.key) ⇒ Object
Old API:.
Class Attribute Details
.api_key ⇒ Object 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 |
.login ⇒ Object
Returns the value of attribute login.
15 16 17 |
# File 'lib/ruby-bitly.rb', line 15 def login @login end |
.use_ssl ⇒ Object
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
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 (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
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, 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 |
.proxy ⇒ Object
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, 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 |