class MarkdownVideos::Services::ServiceBase

Attributes

alt_text[RW]
class_name[RW]
height[RW]
markdown_url[RW]
markdown_url_data[RW]
resource_id[RW]
width[RW]
wrapper[RW]

Public Class Methods

new(alt_text, markdown_url, options) click to toggle source
# File lib/markdown_videos/services/service_base.rb, line 25
def initialize(alt_text, markdown_url, options)
  @alt_text = alt_text
  @markdown_url = markdown_url.strip
  options.each { |option, value| self.send("#{option}=", value) }
end
regexp() click to toggle source

URL matcher

# File lib/markdown_videos/services/service_base.rb, line 20
def self.regexp
  raise NotImplementedError "#{self.class} should implement #regexp method"
end
support(markdown_url) click to toggle source
# File lib/markdown_videos/services/service_base.rb, line 15
def self.support(markdown_url)
  markdown_url =~ regexp
end

Public Instance Methods

html_attributes() click to toggle source

Default set of HTML attributes for default iframe element

@return [Hash]

# File lib/markdown_videos/services/service_base.rb, line 94
def html_attributes
  {
    title: alt_text,
    src: service_url,
    width: width,
    height: height,
    class: class_name,
    frameborder: "0",
    webkitallowfullscreen: true,
    mozallowfullscreen: true,
    allowfullscreen: true
  }
end
html_dom_properties() click to toggle source

Renders the set of HTML attributes

@return [String] string of dom properties

# File lib/markdown_videos/services/service_base.rb, line 87
def html_dom_properties
  html_attributes.map { |k, v| render_param(k, v) }.compact.join(" ")
end
render() click to toggle source
# File lib/markdown_videos/services/service_base.rb, line 108
def render
  if wrapper
    sprintf(wrapper, to_html)
  else
    to_html
  end
end
service_url() click to toggle source

@return [String] service URL to be consumed by to_html

# File lib/markdown_videos/services/service_base.rb, line 67
def service_url
  given_url = Addressable::URI.parse(markdown_url)
  given_url_parameters = given_url.query_values || {}
  given_url_parameters.select! { |attribute, value| url_parameters.include?(attribute.to_sym) && (!value.nil? || !value.empty?) }

  service_url = Addressable::URI.parse(url)
  service_url.query_values = given_url_parameters unless given_url_parameters.nil? || given_url_parameters.empty?
  service_url.to_s
end
to_html() click to toggle source

Default HTML rendering

@retun [String] markup to render

# File lib/markdown_videos/services/service_base.rb, line 80
def to_html
  "<iframe #{html_dom_properties}></iframe>"
end
url() click to toggle source

URL to be consumed by to_html

# File lib/markdown_videos/services/service_base.rb, line 32
def url
  raise NotImplementedError "#{self.class} should implement #url method"
end
url_parameters() click to toggle source

List of authorized url parameters

@return [Array<Symbol>] e.g.: [:start]

# File lib/markdown_videos/services/service_base.rb, line 39
def url_parameters
  []
end

Private Instance Methods

render_param(attribute, value) click to toggle source

Renders HTML5 attribute, meant to be used with `Hash.map`

@param attribute [Symbol, String] @param value [true, String, false] `true` if you want only the attribute. `false`, `nil` or empty String to skip attribute

@return [String] attribute=“value”

# File lib/markdown_videos/services/service_base.rb, line 124
def render_param(attribute, value)
  if value == true
    attribute.to_s
  elsif !value || value.to_s.empty?
    nil
  else
    "#{attribute}=\"#{value}\""
  end
end