Class: EmbeddedFile::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/asker/loader/embedded_file/loader.rb

Overview

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

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"

Instance Method Summary collapse

Instance Method Details

#call(value, localdir) ⇒ Object

Returns Hash.

Parameters:

  • value (String)
  • localdir (String)

    Input file base folder

Returns:

  • Hash



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

def call(value, localdir)
  filepath = File.join(localdir, value)
  type = EmbebbedFile::Type.new.for(value, localdir)

  case type
  when :image
    return load_image(value, localdir)
  when :image_url
    return load_image_url(value, localdir)
  when :audio
    return load_audio(value, localdir)
  when :audio_url
    return load_audio_url(value, localdir)
  when :video
    return load_video(value, localdir)
  when :video_url
    return load_video_url(value, localdir)
  end

  {text: "<pre>#{File.read(filepath)}</pre>", file: :none, type: :text}
end

#load_audio(value, localdir) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/asker/loader/embedded_file/loader.rb', line 37

def load_audio(value, localdir)
  filepath = File.join(localdir, value)
  output = {}
  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_audio_url(value, localdir) ⇒ Object



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

def load_audio_url(value, localdir)
  {
    text: "<audio src=\"#{value}\" controls></audio>",
    file: :none,
    type: :url
  }
end

#load_image(value, localdir) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/asker/loader/embedded_file/loader.rb', line 58

def load_image(value, localdir)
  filepath = File.join(localdir, value)
  output = {}
  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_image_url(value, localdir) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/asker/loader/embedded_file/loader.rb', line 71

def load_image_url(value, localdir)
  {
    text: "<img src=\"#{value}\" alt=\"image\" width=\"400\" height=\"300\">",
    file: :none,
    type: :url
  }
end

#load_video(value, localdir) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/asker/loader/embedded_file/loader.rb', line 79

def load_video(value, localdir)
  filepath = File.join(localdir, value)
  output = {}
  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

#load_video_url(value, localdir) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/asker/loader/embedded_file/loader.rb', line 94

def load_video_url(value, localdir)
  {
    text: "<video controls width=\"400\" height=\"300\">" \
                    + "<source src=\"#{value}\"/></video>",
    file: :none,
    type: :url
  }
end