Class: Frinkiac::Screencap

Inherits:
Object
  • Object
show all
Defined in:
lib/frinkiac/screencap.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Screencap

Returns a new instance of Screencap.



14
15
16
17
18
# File 'lib/frinkiac/screencap.rb', line 14

def initialize(attributes)
  @id = attributes['Id']
  @episode = attributes['Episode']
  @timestamp = attributes['Timestamp']
end

Instance Attribute Details

#episodeObject (readonly)

Returns the value of attribute episode.



12
13
14
# File 'lib/frinkiac/screencap.rb', line 12

def episode
  @episode
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/frinkiac/screencap.rb', line 12

def id
  @id
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



12
13
14
# File 'lib/frinkiac/screencap.rb', line 12

def timestamp
  @timestamp
end

Class Method Details

.random(query) ⇒ Object



47
48
49
50
# File 'lib/frinkiac/screencap.rb', line 47

def self.random(query)
  screencaps = search(query)
  screencaps[rand(screencaps.size)] if screencaps.any?
end

.search(query) ⇒ Object



40
41
42
43
44
45
# File 'lib/frinkiac/screencap.rb', line 40

def self.search(query)
  response = Faraday.get("#{API_URL}?q=#{query}")
  screencaps = JSON.parse(response.body)

  screencaps.map { |attributes| new(attributes) }
end

Instance Method Details

#captionObject



31
32
33
34
35
36
37
38
# File 'lib/frinkiac/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_urlObject



20
21
22
# File 'lib/frinkiac/screencap.rb', line 20

def image_url
  "#{SITE_URL}/img/#{episode}/#{timestamp}.jpg"
end

#meme_url(caption = nil) ⇒ Object



24
25
26
27
28
29
# File 'lib/frinkiac/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