Class: Gadgeto::VideoUrl

Inherits:
Object
  • Object
show all
Defined in:
lib/gadgeto/video_url.rb

Constant Summary collapse

YOUTUBE_REGEXP =
/^.*((youtu.be\/)|(v\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/i
VIMEO_REGEXP =
/http:\/\/(www\.)?vimeo.com\/(.*\/)?(\w*#)?(\d+)($|\/)/i
YOUTUBE_EMBEDDED_TEMPLATE =
'http://www.youtube.com/embed/%s?wmode=transparent&autoplay=%s'
VIMEO_EMBEDDED_TEMPLATE =
'http://player.vimeo.com/video/%s?title=0&byline=0&portrait=0&autoplay=%s'
SUPPORTED_SERVICE_TYPES =
[:youtube, :vimeo]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ VideoUrl

Return a new instance with the given URL

Parameters

url - URL of the video



18
19
20
# File 'lib/gadgeto/video_url.rb', line 18

def initialize(url)
  @url = url
end

Instance Attribute Details

#urlObject

Returns the value of attribute url.



12
13
14
# File 'lib/gadgeto/video_url.rb', line 12

def url
  @url
end

Class Method Details

.supported_servicesObject

Returns all supported service types as array of symbols



62
63
64
# File 'lib/gadgeto/video_url.rb', line 62

def self.supported_services
  return SUPPORTED_SERVICE_TYPES
end

.supported_video_typesObject

DEPRECATED: Please use supported_services instead.



67
68
69
70
# File 'lib/gadgeto/video_url.rb', line 67

def self.supported_video_types
  warn "[DEPRECATION] `supported_video_types` is deprecated.  Please use `supported_services` instead."
  return supported_services
end

.valid?(url) ⇒ Boolean

Returns true if the URL is valid

Parameters

url - URL to validate

Returns:

  • (Boolean)


76
77
78
# File 'lib/gadgeto/video_url.rb', line 76

def self.valid?(url)
  !!url.match(/(#{YOUTUBE_REGEXP})|(#{VIMEO_REGEXP})/)
end

Instance Method Details

#embedded(options = {}) ⇒ Object

Returns the URL for this video embedded

Parameters

  • options - Configuration for the embedded URL.

Options

  • :autoplay - Autoplay on or off (default on)



46
47
48
49
50
51
52
53
54
# File 'lib/gadgeto/video_url.rb', line 46

def embedded(options={})
  autoplay = options[:autoplay].nil? ? true : options[:autoplay]
  autoplay = !!autoplay ? '1' : '0'
  embeded_template = case self.service
                     when :youtube then YOUTUBE_EMBEDDED_TEMPLATE
                     when :vimeo then VIMEO_EMBEDDED_TEMPLATE
                     end
  return embeded_template % [self.id, autoplay]
end

#idObject

Returns the own video service id



32
33
34
35
36
37
# File 'lib/gadgeto/video_url.rb', line 32

def id
  case self.service
    when :youtube then parse_video_id_for_youtube
    when :vimeo then parse_video_id_for_vimeo
  end
end

#serviceObject

Set the own media type.



23
24
25
26
27
28
29
# File 'lib/gadgeto/video_url.rb', line 23

def service
  case self.url
    when YOUTUBE_REGEXP then :youtube
    when VIMEO_REGEXP then :vimeo
    else nil
  end
end

#valid?Boolean

Returns true if the URL is valid

Returns:

  • (Boolean)


57
58
59
# File 'lib/gadgeto/video_url.rb', line 57

def valid?
  VideoUrl.valid?(self.url)
end