Class: Pixy::Shorten
- Inherits:
-
Object
- Object
- Pixy::Shorten
- Defined in:
- lib/pixy/shorten.rb
Constant Summary collapse
- API_URL =
'http://p.tl/api/api_simple.php'
Instance Attribute Summary collapse
-
#counter ⇒ Object
readonly
Returns the value of attribute counter.
-
#long_url ⇒ Object
readonly
Returns the value of attribute long_url.
-
#short_url ⇒ Object
readonly
Returns the value of attribute short_url.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#initialize(key, url) ⇒ Shorten
constructor
A new instance of Shorten.
Constructor Details
#initialize(key, url) ⇒ Shorten
Returns a new instance of Shorten.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/pixy/shorten.rb', line 6 def initialize(key, url) uri = URI "#{API_URL}?key=#{key}&url=#{escape_url escape_url(url)}" response = Net::HTTP.get_response(uri) result = MultiJson.load(response.body) if response.code.to_i == 200 @status = result['status'] @long_url = result['long_url'] @short_url = result['short_url'] @counter = result['counter'] raise_exception @status unless @status == 'ok' else raise UnknownError, "Server responded with code #{response.code}" end end |
Instance Attribute Details
#counter ⇒ Object (readonly)
Returns the value of attribute counter.
4 5 6 |
# File 'lib/pixy/shorten.rb', line 4 def counter @counter end |
#long_url ⇒ Object (readonly)
Returns the value of attribute long_url.
4 5 6 |
# File 'lib/pixy/shorten.rb', line 4 def long_url @long_url end |
#short_url ⇒ Object (readonly)
Returns the value of attribute short_url.
4 5 6 |
# File 'lib/pixy/shorten.rb', line 4 def short_url @short_url end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
4 5 6 |
# File 'lib/pixy/shorten.rb', line 4 def status @status end |