module Assetify::Helpers

Public Instance Methods

find_version(txt) click to toggle source

Detects numerical software version from text.

# File lib/assetify/helpers.rb, line 8
def find_version(txt)
  return unless txt
  if txt.binary?
    find_version_from_bin(txt)
  else
    v = find_version_from_txt(txt)
    v && v[0] =~ /(\d+)\.(\d+).*/ ? v : find_version_from_bin(txt)
  end
end

Private Instance Methods

download(url_str, limit = 10) click to toggle source

Downloads assets

# File lib/assetify/helpers.rb, line 34
def download(url_str, limit = 10)
  raise ArgumentError, 'HTTP redirect too deep' if limit == 0
  uri = URI.parse url_str
  http = Net::HTTP.start(uri.host, use_ssl: url_str =~ /https/) # do |http|
  response = http.get(uri.path.empty? ? '/' : uri.path)

  case response
  when Net::HTTPSuccess     then @data = response
  when Net::HTTPRedirection then download(redirect_url(response), limit - 1)
  else
    puts " - fail - response code: #{response.code}!"
    response.error!
    nil
  end
end
find_version_from_bin(blob) click to toggle source
# File lib/assetify/helpers.rb, line 27
def find_version_from_bin(blob)
  Digest::MD5.hexdigest blob
end
find_version_from_txt(blob) click to toggle source
# File lib/assetify/helpers.rb, line 20
def find_version_from_txt(blob)
  version = blob.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
get_data(str) click to toggle source
# File lib/assetify/helpers.rb, line 50
def get_data(str)
  @data = if str =~ /http/
            data = download(str)
            data ? data.body : nil
          else
            File.open(str)
          end
end
redirect_url(response) click to toggle source
# File lib/assetify/helpers.rb, line 64
def redirect_url(response)
  if response['location'].nil?
    response.body.match(/<a href=\"([^>]+)\">/i)[1]
  else
    response['location']
  end
end
write(binary) click to toggle source
# File lib/assetify/helpers.rb, line 59
def write(binary)
  FileUtils.mkdir_p path unless Dir.exist?(path)
  File.open(fullpath, 'w') { |f| f.puts(binary) }
end