Class: Bremen::Youtube

Inherits:
Base
  • Object
show all
Defined in:
lib/bremen/youtube.rb

Constant Summary collapse

BASE_URL =
'http://gdata.youtube.com/feeds/api/videos/'

Instance Attribute Summary

Attributes included from Track

#author, #created_at, #length, #thumbnail_url, #title, #uid, #updated_at, #url

Class Method Summary collapse

Methods inherited from Base

find, search

Methods included from Request

#build_query, #get

Methods included from Track

#initialize

Class Method Details

.build_query(options = {}) ⇒ Object



16
17
18
# File 'lib/bremen/youtube.rb', line 16

def build_query options = {}
  super(options.merge(alt: 'json'))
end

.find_url(uid_or_url) ⇒ Object



20
21
22
23
# File 'lib/bremen/youtube.rb', line 20

def find_url uid_or_url
  uid = uid_or_url.scan(%r{[?&]v=(.{11})}).flatten.first || uid_or_url
  "#{BASE_URL}#{uid}?#{build_query}"
end

.from_api(hash = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bremen/youtube.rb', line 36

def from_api hash = {}
  uid = hash['id']['$t'].sub('http://gdata.youtube.com/feeds/api/videos/', '')
  author = hash['author'].first
  new({
    uid: uid,
    url: "http://www.youtube.com/watch?v=#{uid}",
    title: hash['title']['$t'],
    author: Bremen::Author.new({
      uid: author.key?('yt$userId') ? author['yt$userId']['$t'] : nil,
      url: author['uri']['$t'].sub('gdata.youtube.com/feeds/api/users/', 'www.youtube.com/channel/UC'),
      name: author['name']['$t'],
    }),
    length: hash['media$group']['yt$duration']['seconds'].to_i,
    thumbnail_url: hash['media$group']['media$thumbnail'][0]['url'],
    created_at: Time.parse(hash['published']['$t']).utc,
    updated_at: Time.parse(hash['updated']['$t']).utc,
  })
end

.search_url(options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/bremen/youtube.rb', line 25

def search_url options = {}
  options = default_options.merge(options)
  query = {
    vq: options[:keyword],
    orderby: options[:order],
    :"max-results" => options[:limit],
    :"start-index" => options[:page],
  }
  "#{BASE_URL}-/#{options[:category]}/#{options[:tag]}/?#{build_query(query)}"
end