Module: Arduino::Library::Utilities

Included in:
Database, Model, Presenters::Properties
Defined in:
lib/arduino/library/utilities.rb

Instance Method Summary collapse

Instance Method Details

#backup_previous_library(path) ⇒ Object



23
24
25
26
27
# File 'lib/arduino/library/utilities.rb', line 23

def backup_previous_library(path)
  new_name = path + ".#{short_time}"
  debug "moving #{path.bold.green}", "to #{new_name.bold.blue}"
  FileUtils.move(path, new_name)
end

#debug(*msgs) ⇒ Object



37
38
39
# File 'lib/arduino/library/utilities.rb', line 37

def debug(*msgs)
  puts "\n" + msgs.join("\n") if ENV['DEBUG']
end

#download(url, path) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/arduino/library/utilities.rb', line 29

def download(url, path)
  debug "dowloading from [#{url.to_s.bold.red}]"
  debug "             to [#{path.to_s.bold.green}]"
  open(path, 'wb') do |file|
    file << open(url).read
  end
end

#open_plain_or_gzipped(file_or_url, temp_file = nil) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/arduino/library/utilities.rb', line 15

def open_plain_or_gzipped(file_or_url, temp_file = nil)
  if file_or_url =~ /\.gz$/i
    Zlib::GzipReader.new(temp_file || File.open(file_or_url))
  else
    temp_file
  end
end

#read_file_or_url(file_or_url) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
# File 'lib/arduino/library/utilities.rb', line 9

def read_file_or_url(file_or_url)
  raise ArgumentError, 'Empty file_or_url provided' unless file_or_url
  temp_file = open(file_or_url)
  open_plain_or_gzipped(file_or_url, temp_file)
end

#short_time(time = Time.now) ⇒ Object



41
42
43
# File 'lib/arduino/library/utilities.rb', line 41

def short_time(time = Time.now)
  time.strftime('%Y%m%d-%H%M%S')
end