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, api_key = self.api_key) ⇒ Object
Old API:.
-
.get_clicks(short_url, login = self.login, api_key = self.api_key) ⇒ Object
Old API:.
- .proxy ⇒ Object
- .proxy=(addr) ⇒ Object
-
.shorten(long_url, login = self.login, api_key = self.api_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, 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 (short_url, login = self.login, api_key = self.api_key) = (url: short_url, login: login, api_key: api_key) .select! { |k,_v| [:shortURL, :login, :apiKey].include?(k) } response = JSON.parse RestClient.post(rest_api_url + ACTION_PATH[:expand], ) 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, login = self.login, api_key = self.api_key) = (url: short_url, login: login, api_key: api_key) .select! { |k,_v| [:shortURL, :login, :apiKey].include?(k) } [:shortUrl] = .delete(:shortURL) response = JSON.parse RestClient.get("#{rest_api_url}#{ACTION_PATH[:clicks]}", params: ) 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, 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, login = self.login, api_key = self.api_key) = (url: long_url, login: login, api_key: api_key) .select! { |k,_v| [:longURL, :domain, :login, :apiKey].include?(k) } response = JSON.parse RestClient.post(rest_api_url + ACTION_PATH[:shorten], ) 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 |