Class: MultimediaParadise::YoutubeEmbedder

Inherits:
Base
  • Object
show all
Defined in:
lib/multimedia_paradise/video/youtube_embedder.rb

Overview

MultimediaParadise::YoutubeEmbedder

Constant Summary collapse

DEFAULT_URL =
#

DEFAULT_URL

#
'https://www.youtube.com/watch?v=8wqxmlUXPAs'

Constants inherited from Base

Base::ERROR, Base::ERROR_LINE, Base::NAMESPACE, Base::USE_THIS_NAMESPACE_FOR_THE_COLOURS, Base::USE_THIS_NAMESPACE_FOR_THE_CORE_COLOURS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

[], #actions, #append_what_into, #be_silent, #be_verbose?, #beautiful_url, #cartoons_directory?, #clear_the_internal_hash, #cliner, #cliner_with_time_stamp, #colourized_comment, #copy_file, #crimson, #dataset_from_file_video_collection, #dd_mm_yyyy, #debug?, #default_readlines, #directory_to_realvids?, #do_not_use_opn, #dodgerblue, #does_the_video_player_support_this_commandline?, #e, #ecomment, #ecrimson, #efancy, #enable_debug, #ensure_main_encoding, #ensure_that_the_output_directory_exists, #eparse, #erev, #esystem, #ewarn, #file_video_collection?, #filter_for_audio_files, #filter_for_video_files, #forestgreen, #gold, #grey, #hh_mm_ss, #home_x_video?, #indianred, #infer_the_namespace, #internal_hash?, #is_audio_file?, #is_mkv?, #is_mp3?, #is_mp4?, #is_multimedia_file?, #is_on_roebe?, #is_video_file?, #konsole_colour_peru, #lightblue, #lightgreen, #load_yaml, #local_audio_directory?, #log_dir?, #map_symbol_to_locally_existing_file, #mediumorchid, #mediumpurple, #mediumslateblue, #mkdir, #move_file, #namespace?, #no_file_exists, #no_file_exists_at, #olive, #olivedrab, #opne, #opnecomment, #opnn, #orange, #palegoldenrod, #palegreen, #powderblue, #project_base_directory?, #rds, #register_sigint, #remove_file, #report_pwd, #reset_the_internal_hash, #return_all_video_files, #return_pwd, #return_random_video, #rev, #royalblue, #save_what_into, #sdir, #seagreen, #seconds_to_time_format, #select_only_video_files_from, #set_be_verbose, #set_use_colours, #sfancy, #sfile, #simp, #skyblue, #slateblue, #springgreen, #steelblue, #swarn, #teal, #time_right_now, #to_hh_mm_ss, #tomato, #true_rev, #try_to_rename_kde_konsole_tab, #ucliner, #use_colours?, #use_opn?, #use_which_video_player?, #verbose_truth, #video_collection?, #violet, #yaml_directory?, #yel

Methods included from CommandlineArgumentsModule

#all_input_starts_with_a_number?, #commandline_arguments?, #commandline_arguments_as_a_string, #first_argument?, #first_non_hyphened_commandline_argument?, #set_commandline_arguments

Constructor Details

#initialize(link, run_already = true) ⇒ YoutubeEmbedder

#

initialize

The first argument should be the link to youtube.

#


40
41
42
43
44
45
46
47
48
# File 'lib/multimedia_paradise/video/youtube_embedder.rb', line 40

def initialize(link, run_already = true)
  reset
  set_link(link)
  case run_already
  when :dont_run_yet
    run_already = false
  end
  run if run_already
end

Instance Attribute Details

#id=(value) ⇒ Object (writeonly)

#

Define four setters and getters - id, uri, provider, params.

#


25
26
27
# File 'lib/multimedia_paradise/video/youtube_embedder.rb', line 25

def id=(value)
  @id = value
end

#paramsObject

Returns the value of attribute params.



28
29
30
# File 'lib/multimedia_paradise/video/youtube_embedder.rb', line 28

def params
  @params
end

#providerObject

Returns the value of attribute provider.



27
28
29
# File 'lib/multimedia_paradise/video/youtube_embedder.rb', line 27

def provider
  @provider
end

#uriObject

Returns the value of attribute uri.



26
27
28
# File 'lib/multimedia_paradise/video/youtube_embedder.rb', line 26

def uri
  @uri
end

Instance Method Details

#determine_params(i = @uri) ⇒ Object

#

determine_params

We must be careful here - the input should be a string.

#


110
111
112
113
114
115
116
# File 'lib/multimedia_paradise/video/youtube_embedder.rb', line 110

def determine_params(i = @uri)
  @params = Hash[*i.query.split('&').map {|entry|
    splitted = entry.split('=')
    splitted[0] = splitted.first.to_sym # First entry becomes a symbol
    splitted
  }.flatten]
end

#determine_provider(i = @uri) ⇒ Object

#

determine_provider

We determine the provider here.

#


123
124
125
126
127
# File 'lib/multimedia_paradise/video/youtube_embedder.rb', line 123

def determine_provider(i = @uri)
  @provider = case i.host
              when /youtube.com/ then :youtube
              end
end

#embed(dimensions = @width+'x'+@height, options = {}) ⇒ Object

#

embed

#


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/multimedia_paradise/video/youtube_embedder.rb', line 63

def embed(
    dimensions = @width+'x'+@height,
    options = {}
  )
  options[:load] = true if options[:load].nil?
  if dimensions.include? 'x'
    h, w = dimensions.split('x')
  else
    h = w = dimensions # Assume '50' as input actually means '50x50'.
  end
  if youtube?
    @embed = %(
      <iframe width="#{h}" height="#{w}" #{options[:load] ? 'src' : 'data-src'}="http://www.youtube.com/embed/#{id}?html5=1"
    )
    # Process the @options next.
    options.each {| key, value|
      next if key == :load
      @embed << %( data-#{key}="#{value}" )
    }
    @embed << %( frameborder="0" allowfullscreen></iframe>)
  else
    @embed << 'NOT FOUND ANYTHING.<br>'
  end
end

#embed?Boolean Also known as: embedded_string?

#

embed?

#

Returns:

  • (Boolean)


91
92
93
# File 'lib/multimedia_paradise/video/youtube_embedder.rb', line 91

def embed?
  @embed
end

#feedbackObject

#

feedback

#


155
156
157
158
159
160
161
162
163
164
165
# File 'lib/multimedia_paradise/video/youtube_embedder.rb', line 155

def feedback
  lpad = 32
  e 'Now testing some components for '+simp(url?)+':'
  e ('The id is: ').ljust(lpad)+sfancy(id)
  e ('The provider is: ').ljust(lpad)+sfancy(provider.to_s)
  e ('The thumbnail is: ').ljust(lpad)+sfancy(thumbnail)
  e
  e 'And to embed this into your webpage, use this code here:'+N+N
  e '  '+embed
  e N+N
end

#id?Boolean Also known as: id

#

id?

#

Returns:

  • (Boolean)


141
142
143
# File 'lib/multimedia_paradise/video/youtube_embedder.rb', line 141

def id?
  params[:v] if youtube?
end

#parse_uri(i = @link) ⇒ Object

#

parse_uri

This will set @uri.

#


134
135
136
# File 'lib/multimedia_paradise/video/youtube_embedder.rb', line 134

def parse_uri(i = @link)
  @uri = URI(i)
end

#resetObject

#

reset

#


53
54
55
56
57
58
# File 'lib/multimedia_paradise/video/youtube_embedder.rb', line 53

def reset
  super()
  @embed = ''
  set_width('560')
  set_height('315')
end

#runObject

#

run

#


217
218
219
220
221
222
223
# File 'lib/multimedia_paradise/video/youtube_embedder.rb', line 217

def run
  parse_uri
  determine_provider
  determine_params
  embed
  return @embed
end

#set_height(i) ⇒ Object

#

set_height

#


180
181
182
183
184
185
# File 'lib/multimedia_paradise/video/youtube_embedder.rb', line 180

def set_height(i)
  if i.is_a? Hash and i.has_key?(:height)
    i = i.delete :height
  end
  @height = i.to_s
end
#
#


98
99
100
101
102
103
# File 'lib/multimedia_paradise/video/youtube_embedder.rb', line 98

def set_link(i = nil)
  i = i.first if i.is_a? Array
  i = DEFAULT_URL if i.nil?
  i = i.to_s.dup
  @link = i
end

#set_width(i) ⇒ Object

#

set_width

#


170
171
172
173
174
175
# File 'lib/multimedia_paradise/video/youtube_embedder.rb', line 170

def set_width(i)
  if i.is_a? Hash and i.has_key?(:width)
    i = i.delete :width
  end
  @width = i.to_s
end

#thumbnail(size = :default) ⇒ Object

#

thumbnail

Give back a thumbnail of the youtube video.

Each YouTube video has 4 generated images.

They are predictably formatted as follows:

http://img.youtube.com/vi/<insert-youtube-video-id-here>/0.jpg
http://img.youtube.com/vi/<insert-youtube-video-id-here>/1.jpg
http://img.youtube.com/vi/<insert-youtube-video-id-here>/2.jpg
http://img.youtube.com/vi/<insert-youtube-video-id-here>/3.jpg
#


208
209
210
211
212
# File 'lib/multimedia_paradise/video/youtube_embedder.rb', line 208

def thumbnail(size = :default)
  if youtube?
    "http://img.youtube.com/vi/#{id?}/0.jpg"
  end
end

#url?Boolean

#

url?

#

Returns:

  • (Boolean)


190
191
192
# File 'lib/multimedia_paradise/video/youtube_embedder.rb', line 190

def url?
  @uri.to_s
end

#youtube?Boolean

#

youtube?

#

Returns:

  • (Boolean)


148
149
150
# File 'lib/multimedia_paradise/video/youtube_embedder.rb', line 148

def youtube?
  provider == :youtube # This sets the @provider ivar.
end