Class: Shorty::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/shorty/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(end_point) {|_self| ... } ⇒ Client

Returns a new instance of Client.

Yields:

  • (_self)

Yield Parameters:



14
15
16
17
# File 'lib/shorty/client.rb', line 14

def initialize(end_point, &block)
  @uri = URI.parse(end_point)
  yield self if block_given?
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



12
13
14
# File 'lib/shorty/client.rb', line 12

def error
  @error
end

Instance Method Details

#keyObject

Gets a new key that was generated by Shorty.



38
39
40
# File 'lib/shorty/client.rb', line 38

def key
  Net::HTTP.get(@uri.host, "#{@uri.request_uri}key", @uri.port)
end

#shorten(url, key = nil) ⇒ Object

Return nil/false if a key couldn’t be generated



20
21
22
23
24
25
26
# File 'lib/shorty/client.rb', line 20

def shorten(url, key=nil)
  begin
    shorten!(url, key)
  rescue => e
    nil
  end
end

#shorten!(url, key = nil) ⇒ Object

Raise an exception of the url couldn’t be shortened



29
30
31
32
33
34
35
# File 'lib/shorty/client.rb', line 29

def shorten!(url, key=nil)
  begin
    key ? named_url(url, key) : autoname_url(url)
  rescue => e
    @error = e and raise
  end
end