class Amaranth::Video

Constants

OMIT_IF_BLANK_ATTRIBUTES
READONLY_ATTRIBUTES

Public Class Methods

all(team_slug: nil, project_slug: nil) click to toggle source
# File lib/amaranth/video.rb, line 5
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| members.include? key.to_sym }
  end
end
create(attributes) click to toggle source
# File lib/amaranth/video.rb, line 22
def self.create attributes
  Request.post("/api/videos/", attributes)
end
create_or_update_by_video_url(video_url, attributes) click to toggle source
# File lib/amaranth/video.rb, line 33
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) click to toggle source
# File lib/amaranth/video.rb, line 26
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| members.include? key.to_sym }
  end
end
new(attributes={}) click to toggle source
# File lib/amaranth/video.rb, line 40
def initialize attributes={}
  attributes.each do |key, value|
    self[key] = value
  end
end

Private Class Methods

fetch(url) click to toggle source
# File lib/amaranth/video.rb, line 14
                     def self.fetch url
  json = Request.get(url)
  objects = json["objects"]
  next_url = json["meta"]["next"]
  objects += fetch(next_url) if next_url
  objects
end

Public Instance Methods

persisted?() click to toggle source
# File lib/amaranth/video.rb, line 61
def persisted?
  id.to_s.length > 0
end
save() click to toggle source
# File lib/amaranth/video.rb, line 53
def save
  if persisted?
    save_existing
  else
    raise NotImplementedError
  end
end
update(attributes={}) click to toggle source
# File lib/amaranth/video.rb, line 46
def update attributes={}
  attributes.each do |key, value|
    self[key] = value
  end
  save
end

Private Instance Methods

blank?(value) click to toggle source
# File lib/amaranth/video.rb, line 77
def blank? value
  value.to_s.strip == ""
end
save_existing() click to toggle source
# File lib/amaranth/video.rb, line 70
def save_existing
  attributes = to_h
  attributes.reject! { |key, value| READONLY_ATTRIBUTES.include?(key) }
  attributes.reject! { |key, value| OMIT_IF_BLANK_ATTRIBUTES.include?(key) && blank?(value) }
  Request.put("/api/videos/#{id}/", attributes)
end