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 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

#counterObject (readonly)

Returns the value of attribute counter.



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

def counter
  @counter
end

#long_urlObject (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_urlObject (readonly)

Returns the value of attribute short_url.



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

def short_url
  @short_url
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end