Class: ShortURL::Service
- Inherits:
-
Object
- Object
- ShortURL::Service
- Defined in:
- lib/shorturl/service.rb
Direct Known Subclasses
ShortURL::Services::Bitly, ShortURL::Services::Lns, ShortURL::Services::Metamark, ShortURL::Services::Minilink, ShortURL::Services::MooURL, ShortURL::Services::ShitURL, ShortURL::Services::Shorl, ShortURL::Services::Shortify, ShortURL::Services::SnipURL, ShortURL::Services::TinyURL, ShortURL::Services::Url, ShortURL::Services::Vurl
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#block ⇒ Object
Returns the value of attribute block.
-
#code ⇒ Object
Returns the value of attribute code.
-
#field ⇒ Object
Returns the value of attribute field.
-
#method ⇒ Object
Returns the value of attribute method.
-
#port ⇒ Object
Returns the value of attribute port.
-
#response_block ⇒ Object
Returns the value of attribute response_block.
-
#ssl ⇒ Object
Returns the value of attribute ssl.
Instance Method Summary collapse
-
#call(url) ⇒ Object
Now that our service is set up, call it with all the parameters to (hopefully) return only the shortened URL.
-
#initialize(hostname) ⇒ Service
constructor
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.
-
#on_body(body) ⇒ Object
Extracts the shortened URL from a response body.
-
#on_response(response) ⇒ Object
Extracts the shortened URL from the response.
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
#action ⇒ Object
Returns the value of attribute action.
8 9 10 |
# File 'lib/shorturl/service.rb', line 8 def action @action end |
#block ⇒ Object
Returns the value of attribute block.
8 9 10 |
# File 'lib/shorturl/service.rb', line 8 def block @block end |
#code ⇒ Object
Returns the value of attribute code.
8 9 10 |
# File 'lib/shorturl/service.rb', line 8 def code @code end |
#field ⇒ Object
Returns the value of attribute field.
8 9 10 |
# File 'lib/shorturl/service.rb', line 8 def field @field end |
#method ⇒ Object
Returns the value of attribute method.
8 9 10 |
# File 'lib/shorturl/service.rb', line 8 def method @method end |
#port ⇒ Object
Returns the value of attribute port.
8 9 10 |
# File 'lib/shorturl/service.rb', line 8 def port @port end |
#response_block ⇒ Object
Returns the value of attribute response_block.
8 9 10 |
# File 'lib/shorturl/service.rb', line 8 def response_block @response_block end |
#ssl ⇒ Object
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 |