Class: ShortURL::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/shorturl/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname) ⇒ Service

Intialize the service with a hostname (required parameter) and you can override the default values for the HTTP port, expected HTTP return code, the form method to use, the form action, the form field which contains the long URL, and the block of what to do with the HTML code you get.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/shorturl/service.rb', line 15

def initialize(hostname) # :yield: service
  @hostname = hostname
  @port = 80
  @code = 200
  @method = :post
  @action = "/"
  @field = "url"
  @ssl = false

  if block_given?
    yield self
  end
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



8
9
10
# File 'lib/shorturl/service.rb', line 8

def action
  @action
end

#blockObject

Returns the value of attribute block.



8
9
10
# File 'lib/shorturl/service.rb', line 8

def block
  @block
end

#codeObject

Returns the value of attribute code.



8
9
10
# File 'lib/shorturl/service.rb', line 8

def code
  @code
end

#fieldObject

Returns the value of attribute field.



8
9
10
# File 'lib/shorturl/service.rb', line 8

def field
  @field
end

#methodObject

Returns the value of attribute method.



8
9
10
# File 'lib/shorturl/service.rb', line 8

def method
  @method
end

#portObject

Returns the value of attribute port.



8
9
10
# File 'lib/shorturl/service.rb', line 8

def port
  @port
end

#response_blockObject

Returns the value of attribute response_block.



8
9
10
# File 'lib/shorturl/service.rb', line 8

def response_block
  @response_block
end

#sslObject

Returns the value of attribute ssl.



8
9
10
# File 'lib/shorturl/service.rb', line 8

def ssl
  @ssl
end

Instance Method Details

#call(url) ⇒ Object

Now that our service is set up, call it with all the parameters to (hopefully) return only the shortened URL.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/shorturl/service.rb', line 31

def call(url)
  http = Net::HTTP.new(@hostname, @port)
  http.use_ssl = @ssl
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  http.start do
    response = case @method
               when :post
                 http.post(@action, "#{@field}=#{CGI.escape(url)}")
               when :get
                 http.get("#{@action}?#{@field}=#{CGI.escape(url)}")
               end

    if response.code == @code.to_s
      on_response(response)
    end
  end
rescue Errno::ECONNRESET => e
  raise ServiceNotAvailable, e.to_s, e.backtrace
end

#on_body(body) ⇒ Object

Extracts the shortened URL from a response body.



53
54
55
# File 'lib/shorturl/service.rb', line 53

def on_body(body)
  body
end

#on_response(response) ⇒ Object

Extracts the shortened URL from the response.



58
59
60
# File 'lib/shorturl/service.rb', line 58

def on_response(response)
  on_body(response.read_body)
end