Sha256: 670d608e2fc8c1cb11b5384c3c2786c860e7cf144c339ab4ed6a47dce6c24a1f

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

require 'pry'

module Assetify
  module Helpers
    private

    #
    # Detects numerical software version from text.
    #
    def find_version(txt)
      return unless txt
      version = txt.match /(?:(\d+)\.)?(?:(\d+)\.)?(\d+)?\.?(\d+)/
      # If matches a dot, it`s text. Otherwise make it number.
      v = version.to_a.reject(&:nil?).map { |d| d =~ /\./ ? d : d.to_i }
      v.empty? || 0 == v[0] ? nil : v
    end

    def download url_str, limit = 10
      raise ArgumentError, 'HTTP redirect too deep' if limit == 0
      uri = URI.parse url_str
      # # response = ""
      http = Net::HTTP.start(uri.host, :use_ssl => url_str =~ /https/)# do |http|
      response = http.get(uri.path.empty? ? "/" : uri.path)
      # # end
      # response = Net::HTTP.get_response(URI.parse(url_str) )# ,:use_ssl => url_str =~ /https/ )
      case response
      when Net::HTTPSuccess     then @data = response
      when Net::HTTPRedirection then download(redirect_url(response), limit - 1)
      else
        p "response code: #{response.code}!"
        response.error!
      end
    end

    def get_data(str)
      @data = if str =~ /http/
        download(str).body
      else
        File.open(str)
      end
    end

    def write(binary)
      FileUtils.mkdir_p path unless  Dir.exists?(path)
      File.open(fullpath, "w") { |f| f.puts(binary) }
    end

    def redirect_url response
      if response['location'].nil?
        response.body.match(/<a href=\"([^>]+)\">/i)[1]
      else
        response['location']
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
assetify-0.3.0 lib/assetify/helpers.rb