Sha256: 70db53ba295dbea98d359780ec77468d7a01c8597deefdc5cee56e506462da7b

Contents?: true

Size: 857 Bytes

Versions: 9

Compression:

Stored size: 857 Bytes

Contents

module Utils
  class ImageUrlFormatter
    attr_reader :url, :image_url

    def initialize(url, image_url)
      @url = url
      @image_url = image_url
    end

    def perform
      return unless image_url
      if host_missing?
        "#{scheme}://#{host}#{encoded_image_url}"
      elsif scheme_missing?
        "http:#{encoded_image_url}"
      else
        encoded_image_url
      end
    end

    def encoded_image_url
      URI.encode(image_url)
    end

    def host
      parsed_url.host
    end

    def scheme
      parsed_url.scheme
    end

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

    def parsed_image_url
      @parsed_image_url ||= URI.parse(encoded_image_url)
    end

    def scheme_missing?
      parsed_image_url.scheme.to_s.empty?
    end

    def host_missing?
      !parsed_image_url.host
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
link_oracle-0.1.11 lib/link_oracle/Utils/image_url_formatter.rb
link_oracle-0.1.10 lib/link_oracle/Utils/image_url_formatter.rb
link_oracle-0.1.9 lib/link_oracle/Utils/image_url_formatter.rb
link_oracle-0.1.8 lib/link_oracle/Utils/image_url_formatter.rb
link_oracle-0.1.7 lib/link_oracle/Utils/image_url_formatter.rb
link_oracle-0.1.6 lib/link_oracle/Utils/image_url_formatter.rb
link_oracle-0.1.5 lib/link_oracle/Utils/image_url_formatter.rb
link_oracle-0.1.4 lib/link_oracle/Utils/image_url_formatter.rb
link_oracle-0.1.3 lib/link_oracle/Utils/image_url_formatter.rb