class MasterOfAllScience::Screencap

Attributes

episode[R]
id[R]
timestamp[R]

Public Class Methods

new(attributes) click to toggle source
# File lib/master_of_all_science/screencap.rb, line 14
def initialize(attributes)
  @id = attributes['Id']
  @episode = attributes['Episode']
  @timestamp = attributes['Timestamp']
end
random(query) click to toggle source
# File lib/master_of_all_science/screencap.rb, line 47
def self.random(query)
  screencaps = search(query)
  screencaps[rand(screencaps.size)] if screencaps.any?
end

Public Instance Methods

caption() click to toggle source
# File lib/master_of_all_science/screencap.rb, line 31
def caption
  @caption ||= begin
    response = Faraday.get("#{CAPTION_URL}?e=#{episode}&t=#{timestamp}")
    body = JSON.parse(response.body)

    format_captions(body['Subtitles'].collect { |s| s['Content'] })
  end
end
image_url() click to toggle source
# File lib/master_of_all_science/screencap.rb, line 20
def image_url
  "#{SITE_URL}/img/#{episode}/#{timestamp}.jpg"
end
meme_url(caption = nil) click to toggle source
# File lib/master_of_all_science/screencap.rb, line 24
def meme_url(caption = nil)
  caption = self.caption if caption.nil?
  caption = caption.join("\n") if caption.is_a?(Array)

  "#{SITE_URL}/meme/#{episode}/#{timestamp}.jpg?b64lines=#{Base64.strict_encode64 caption}"
end

Private Instance Methods

format_captions(captions) click to toggle source
# File lib/master_of_all_science/screencap.rb, line 53
def format_captions(captions)
  caption_string = captions.join(' ')

  WordWrap.ww(caption_string, 25).chop
end