Class: MarkdownVideos::Services::ServiceBase

Inherits:
Object
  • Object
show all
Defined in:
lib/markdown_videos/services/service_base.rb

Direct Known Subclasses

VimeoService, YoutubeService

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(alt_text, markdown_url, options) ⇒ ServiceBase

Returns a new instance of ServiceBase.



25
26
27
28
29
# 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

Instance Attribute Details

#alt_textString

Returns escaped alternative text given through markdown syntax.

Returns:

  • (String)

    escaped alternative text given through markdown syntax



52
53
54
# File 'lib/markdown_videos/services/service_base.rb', line 52

def alt_text
  @alt_text
end

#class_nameObject

Returns the value of attribute class_name.



6
7
8
# File 'lib/markdown_videos/services/service_base.rb', line 6

def class_name
  @class_name
end

#heightObject

Returns the value of attribute height.



6
7
8
# File 'lib/markdown_videos/services/service_base.rb', line 6

def height
  @height
end

#markdown_urlObject

Returns the value of attribute markdown_url.



6
7
8
# File 'lib/markdown_videos/services/service_base.rb', line 6

def markdown_url
  @markdown_url
end

#markdown_url_dataMatchData

Returns match data from matching URL to service regexp.

Returns:

  • (MatchData)

    match data from matching URL to service regexp



62
63
64
# File 'lib/markdown_videos/services/service_base.rb', line 62

def markdown_url_data
  @markdown_url_data
end

#resource_idString

Returns resource ID of the service resource rendered base on given URL.

Returns:

  • (String)

    resource ID of the service resource rendered base on given URL



57
58
59
# File 'lib/markdown_videos/services/service_base.rb', line 57

def resource_id
  @resource_id
end

#widthObject

Returns the value of attribute width.



6
7
8
# File 'lib/markdown_videos/services/service_base.rb', line 6

def width
  @width
end

#wrapperObject

Returns the value of attribute wrapper.



6
7
8
# File 'lib/markdown_videos/services/service_base.rb', line 6

def wrapper
  @wrapper
end

Class Method Details

.regexpObject

URL matcher



20
21
22
# 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) ⇒ Object



15
16
17
# File 'lib/markdown_videos/services/service_base.rb', line 15

def self.support(markdown_url)
  markdown_url =~ regexp
end

Instance Method Details

#html_attributesHash

Default set of HTML attributes for default iframe element

Returns:

  • (Hash)


94
95
96
97
98
99
100
101
102
103
104
105
106
# 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_propertiesString

Renders the set of HTML attributes

Returns:

  • (String)

    string of dom properties



87
88
89
# 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

#renderObject



108
109
110
111
112
113
114
# File 'lib/markdown_videos/services/service_base.rb', line 108

def render
  if wrapper
    sprintf(wrapper, to_html)
  else
    to_html
  end
end

#service_urlString

Returns service URL to be consumed by #to_html.

Returns:

  • (String)

    service URL to be consumed by #to_html



67
68
69
70
71
72
73
74
75
# 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_htmlObject

Default HTML rendering



80
81
82
# File 'lib/markdown_videos/services/service_base.rb', line 80

def to_html
  "<iframe #{html_dom_properties}></iframe>"
end

#urlObject

URL to be consumed by #to_html



32
33
34
# File 'lib/markdown_videos/services/service_base.rb', line 32

def url
  raise NotImplementedError "#{self.class} should implement #url method"
end

#url_parametersArray<Symbol>

List of authorized url parameters

Returns:

  • (Array<Symbol>)

    e.g.: [:start]



39
40
41
# File 'lib/markdown_videos/services/service_base.rb', line 39

def url_parameters
  []
end