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
36
37
38
39
40
# 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,
    url: value
  }
end

#load_audio(value, localdir) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/asker/loader/embedded_file/loader.rb', line 42

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[:url] = value
  output
end

#load_audio_url(value, localdir) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/asker/loader/embedded_file/loader.rb', line 56

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

#load_image(value, localdir) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/asker/loader/embedded_file/loader.rb', line 65

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[:url] = value
  output
end

#load_image_url(value, localdir) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/asker/loader/embedded_file/loader.rb', line 79

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

#load_video(value, localdir) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/asker/loader/embedded_file/loader.rb', line 88

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[:url] = value
  output
end

#load_video_url(value, localdir) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/asker/loader/embedded_file/loader.rb', line 104

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