Class: NagiosHerald::Helpers::UrlImage

Inherits:
Object
  • Object
show all
Defined in:
lib/nagios-herald/helpers/url_image.rb

Class Method Summary collapse

Class Method Details

.convert_uri(uri) ⇒ Object

Public: Convert the URI to a useful name to be used in the image file name. Removes the transport type and query characters.

uri - The URI to be converted.

Returns the converted URI. FIXME: This doesn’t account for HTTPS URIs.



41
42
43
44
45
46
# File 'lib/nagios-herald/helpers/url_image.rb', line 41

def self.convert_uri(uri)
  converted_uri = uri.gsub("http:\/\/", "")
  converted_uri.gsub!(/(\/|=|\?|&)/, "_")     # such a hack...
  converted_uri.gsub!(/_+/, "_")              # de-dupe underscores
  return converted_uri
end

.download_image(uri, path) ⇒ Object

Public: Downloads an image and writes it to a file.

uri - The URI of the image resource. path - The destination file for the image content.

Returns nothing.



54
55
56
57
58
# File 'lib/nagios-herald/helpers/url_image.rb', line 54

def self.download_image(uri, path)
  graph = get_image( uri )
  # only push the image path into the array if we successfully create it
  write_image( path, graph ) ? path : nil
end

.download_images(uris, path) ⇒ Object

To be honest, I don’t recall this method’s purpose. It’s only called in the “bin/get_graph“ script. May be dead code. TODO: Determine if this is still necessary.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/nagios-herald/helpers/url_image.rb', line 63

def self.download_images( uris, path )
  path = path.sub(/\/$/, "")   # strip the trailing slash (if it exists) so the components of image_name are clear
  image_paths = []
  uris.each do |uri|
    converted_uri = convert_uri(uri)
    image_path = "#{path}/#{converted_uri}.png"
    # only push the image path into the array if we successfully create it
    image_paths.push(image_path) if download_image(uri, image_path)
  end
  image_paths
end

.get_image(uri) ⇒ Object

Public: Requests an image by its URI.

uri - The URI of the image resource.

Returns the content of the image.



14
15
16
# File 'lib/nagios-herald/helpers/url_image.rb', line 14

def self.get_image( uri )
  graph = Net::HTTP.get( URI.parse( uri ) )
end

.write_image(file_name, content) ⇒ Object

Public: Writes te given content to a file name.

file_name - The name of the file to write. content - Arbitrary content to write into the file.

Returns true if successful, false otherwise.



24
25
26
27
28
29
30
31
32
# File 'lib/nagios-herald/helpers/url_image.rb', line 24

def self.write_image( file_name, content )
  File.delete( file_name ) if File.exists?( file_name )   # remove any pre-existing versions
  written_size = File.open( file_name, 'w' ) { |f| f.write( content ) }
  if written_size == content.size
    return written_size > 0   # why aren't we just returning true?
  else
    false   # oops...
  end
end