Module: Scarpe::Components::Base64

Included in:
AssetServer, Calzini
Defined in:
lib/scarpe/components/base64.rb

Instance Method Summary collapse

Instance Method Details

#encode_file_to_base64(image_path) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/scarpe/components/base64.rb', line 39

def encode_file_to_base64(image_path)
  image_data = File.binread(image_path)

  encoded_data = ::Base64.strict_encode64(image_data)

  encoded_data
end

#mime_type_for_filename(filename) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/scarpe/components/base64.rb', line 17

def mime_type_for_filename(filename)
  ext = File.extname(filename)[1..-1] # Cut off leading dot

  case ext
  when "jpg", "jpeg"
    "image/jpeg"
  when "gif"
    "image/gif"
  when "png"
    "image/png"
  when "js"
    "text/javascript"
  when "css"
    "text/css"
  when "rb"
    "text/ruby" # Don't think this is standard
  else
    # Don't recognise it, call it random binary
    "application/octet-stream"
  end
end

#valid_url?(string) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
# File 'lib/scarpe/components/base64.rb', line 10

def valid_url?(string)
  uri = URI.parse(string)
  uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS)
rescue URI::InvalidURIError, URI::BadURIError
  false
end