Module: EmbeddedFile

Defined in:
lib/asker/loader/embedded_file.rb

Overview

Methods to load embedded files defined into asker input data file Example:

  • def line with :type = :image_url used to link external file as https://…“

  • def line with :type = :file used to load local file as image.png or file.txt“

Class Method Summary collapse

Class Method Details

.is_audio?(filename) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/asker/loader/embedded_file.rb', line 34

def self.is_audio?(filename)
  extens = ['.mp3', '.ogg', '.wav']
  extens.each {|ext| return true if filename.downcase.end_with?(ext) }
  false
end

.is_image?(filename) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/asker/loader/embedded_file.rb', line 40

def self.is_image?(filename)
  extens = ['.jpg', '.jpeg', '.png']
  extens.each {|ext| return true if filename.downcase.end_with?(ext) }
  false
end

.is_url?(value) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/asker/loader/embedded_file.rb', line 52

def self.is_url?(value)
  return true if value.start_with?('https://') || value.start_with?('http://')
  false
end

.is_video?(filename) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/asker/loader/embedded_file.rb', line 46

def self.is_video?(filename)
  extens = ['.mp4', '.ogv']
  extens.each {|ext| return true if filename.downcase.end_with?(ext) }
  false
end

.load(value, localdir) ⇒ Object

Returns Hash.

Parameters:

  • value (String)
  • localdir (String)

    Input file base folder

Returns:

  • Hash



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/asker/loader/embedded_file.rb', line 13

def self.load(value, localdir)
  return load_image(value, localdir) if is_image? value
  return load_audio(value, localdir) if is_audio? value
  return load_video(value, localdir) if is_video? value

  if is_url? value
    Logger.verbose Rainbow("[ERROR] Unkown URL type!: #{value}").red.bright
    exit 1
  end

  filepath = File.join(localdir, value)
  unless File.exist?(filepath)
    Logger.verbose Rainbow("[ERROR] File does not exist!: #{filepath}").red.bright
    # return { text: "URI error", file: :none, type: :unkown }
    exit 1
  end

  # Suposse that filename is TEXT file
  return { text: "<pre>#{File.read(filepath)}</pre>", file: :none, type: :text }
end

.load_audio(value, localdir) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/asker/loader/embedded_file.rb', line 57

def self.load_audio(value, localdir)
  output = { text: :error, file: :none, type: :audio}

  if is_url? value
    output[:text] = "<audio src=\"#{value}\" controls></audio>"
    output[:file] = :none
    output[:type] = :url
    return output
  end

  filepath = File.join(localdir, value)
  unless File.exist?(filepath)
    Logger.verbose Rainbow("[ERROR] Audio file no exists!: #{filepath}").red.bright
    exit 1
  end
  output[:text] = '<audio controls><source src="@@PLUGINFILE@@/' + File.basename(filepath) \
                  + '">Your browser does not support the audio tag.</audio>'
  output[:file] = '<file name="' + File.basename(filepath) \
                  + '" path="/" encoding="base64">' \
                  + Base64.strict_encode64(File.open(filepath, 'rb').read) + '</file>'
  output[:type] = :audio
  output
end

.load_image(value, localdir) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/asker/loader/embedded_file.rb', line 81

def self.load_image(value, localdir)
  output = { text: :error, file: :none, type: :image}

  if is_url? value
    output[:text] = "<img src=\"#{value}\" alt=\"image\" width=\"400\" height=\"300\">"
    output[:file] = :none
    output[:type] = :url
    return output
  end

  filepath = File.join(localdir, value)
  unless File.exist?(filepath)
    Logger.verbose Rainbow("[ERROR] Unknown file! #{filepath}").red.bright
    exit 1
  end
  output[:text] = '<img src="@@PLUGINFILE@@/' + File.basename(filepath) \
                  + '" alt="imagen" class="img-responsive atto_image_button_text-bottom">'
  output[:file] = '<file name="' + File.basename(filepath) \
                  + '" path="/" encoding="base64">' \
                  + Base64.strict_encode64(File.open(filepath, 'rb').read) + '</file>'
  output[:type] = :image
  output
end

.load_video(value, localdir) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/asker/loader/embedded_file.rb', line 105

def self.load_video(value, localdir)
  output = { text: :error, file: :none, type: :video}

  if is_url? value
    output[:text] = "<video controls width=\"400\" height=\"300\">" \
                    + "<source src=\"#{value}\"/></video>"
    output[:file] = :none
    output[:type] = :url
    return output
  end

  filepath = File.join(localdir, value)
  unless File.exist?(filepath)
    Logger.verbose Rainbow("[ERROR] Unknown file! #{filepath}").red.bright
    exit 1
  end
  output[:text] = '<video controls><source src="@@PLUGINFILE@@/' + File.basename(filepath) \
                  + '"/>Your browser does not support the video tag.</video>'
  output[:file] = '<file name="' + File.basename(filepath) \
                  + '" path="/" encoding="base64">' \
                  + Base64.strict_encode64(File.open(filepath, 'rb').read) + '</file>'
  output[:type] = :video
  output
end