Sha256: 632b1104219c40ca9458c473a69623c4d6074bb64ab4c1fe7f6387773f6010fc

Contents?: true

Size: 751 Bytes

Versions: 4

Compression:

Stored size: 751 Bytes

Contents

module RMD
  module Zing
    module Utils
      class CorrectUrl
        attr_reader :url

        def initialize(url)
          @url = url
        end

        def correct
          if redirect_location
            URI.escape(redirect_location)
          else
            url
          end
        end

        def self.correct(url)
          new(url).correct
        end

        private

        def uri
          @uri ||= URI.parse(url)
        end

        def http_client
          @http_client ||= Net::HTTP.start(uri.host)
        end

        def header_response
          @header_response ||= http_client.head(uri.path)
        end

        def redirect_location
          header_response['location']
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rmd-0.2.1 lib/rmd/zing/utils/correct_url.rb
rmd-0.1.2 lib/rmd/zing/utils/correct_url.rb
rmd-0.1.1 lib/rmd/zing/utils/correct_url.rb
rmd-0.1.0 lib/rmd/zing/utils/correct_url.rb