Class: Googlus::Shorten
- Inherits:
-
Object
- Object
- Googlus::Shorten
- Defined in:
- lib/googlus/shorten.rb
Overview
Shorten a URL
Instance Attribute Summary collapse
-
#short_url ⇒ Object
readonly
Returns the value of attribute short_url.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(url) ⇒ Shorten
constructor
Long URL to shorten.
Constructor Details
#initialize(url) ⇒ Shorten
Long URL to shorten
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/googlus/shorten.rb', line 7 def initialize(url) http = Net::HTTP.new("www.googleapis.com", 443) http.use_ssl = true params = {'longUrl' => url}.to_json resp, body = http.post("/urlshortener/v1/url", params, {'Content-Type' => 'application/json'}) body = JSON.parse(body) if resp.code.to_i == 200 @short_url = body["id"] @url = body["longUrl"] else error = body["error"] raise "Shortening error: #{error["code"]} - #{error["message"]}" end end |
Instance Attribute Details
#short_url ⇒ Object (readonly)
Returns the value of attribute short_url.
4 5 6 |
# File 'lib/googlus/shorten.rb', line 4 def short_url @short_url end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
4 5 6 |
# File 'lib/googlus/shorten.rb', line 4 def url @url end |