Class: Amaranth::Video

Inherits:
Collection show all
Defined in:
lib/amaranth/video.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Collection

#==, #attributes, #attributes=, field, fields, #initialize

Constructor Details

This class inherits a constructor from Amaranth::Collection

Class Method Details

.all(team_slug: nil, project_slug: nil) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/amaranth/video.rb', line 16

def self.all team_slug: nil, project_slug: nil
  url = "https://amara.org/api/videos/?limit=100"
  url += "&team=#{team_slug}" if team_slug
  url += "&project=#{project_slug}" if project_slug
  fetch(url).map do |attributes|
    new attributes.keep_if { |key, value| fields.include? key.to_sym }
  end
end

.create(attributes) ⇒ Object



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

def self.create attributes
  Request.post("/api/videos/", attributes)
end

.create_or_update_by_video_url(video_url, attributes) ⇒ Object



36
37
38
39
40
41
# File 'lib/amaranth/video.rb', line 36

def self.create_or_update_by_video_url video_url, attributes
  Amaranth::Video.create attributes.merge(video_url: video_url)
rescue Amaranth::RequestError => exception
  raise unless exception.message.include?("Video already ")
  Amaranth::Video.find_by_video_url(video_url).update(attributes)
end

.find_by_video_url(video_url) ⇒ Object



29
30
31
32
33
34
# File 'lib/amaranth/video.rb', line 29

def self.find_by_video_url video_url
  if json = Request.get("/api/videos/?video_url=#{video_url}")
    attributes = json["objects"].first
    new attributes.keep_if { |key, value| fields.include? key.to_sym }
  end
end

Instance Method Details

#persisted?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/amaranth/video.rb', line 56

def persisted?
  id.to_s.length > 0
end

#saveObject



48
49
50
51
52
53
54
# File 'lib/amaranth/video.rb', line 48

def save
  if persisted?
    save_existing
  else
    raise NotImplementedError
  end
end

#update(attributes = {}) ⇒ Object



43
44
45
46
# File 'lib/amaranth/video.rb', line 43

def update attributes={}
  self.attributes = attributes
  save
end