Class: Pixy::Shorten

Inherits:
Object
  • Object
show all
Defined in:
lib/pixy/shorten.rb

Constant Summary collapse

API_URL =
'http://p.tl/api/api_simple.php'

Instance Attribute Summary collapse

Instance Method Summary collapse

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(url)}"
  response  = Net::HTTP.get_response(uri)
  result    = JSON.parse(response.body)

  if response.code.to_i == 200
    self.status     = result['status']
    self.long_url   = result['long_url']
    self.short_url  = result['short_url']
    self.counter    = result['counter']
    raise_exception self.status unless self.status == 'ok'
  else
    raise UnknownError, "Server responded with code #{response.code}"
  end
end

Instance Attribute Details

#counterObject

Returns the value of attribute counter.



4
5
6
# File 'lib/pixy/shorten.rb', line 4

def counter
  @counter
end

#long_urlObject

Returns the value of attribute long_url.



4
5
6
# File 'lib/pixy/shorten.rb', line 4

def long_url
  @long_url
end

#short_urlObject

Returns the value of attribute short_url.



4
5
6
# File 'lib/pixy/shorten.rb', line 4

def short_url
  @short_url
end

#statusObject

Returns the value of attribute status.



4
5
6
# File 'lib/pixy/shorten.rb', line 4

def status
  @status
end