Class: Klaro::Client::Story

Inherits:
Resource
  • Object
show all
Defined in:
lib/klaro/client/resource/story.rb

Instance Method Summary collapse

Methods inherited from Resource

dress, #initialize, symbolize_keys

Constructor Details

This class inherits a constructor from Klaro::Client::Resource

Instance Method Details

#_titleObject



5
6
7
# File 'lib/klaro/client/resource/story.rb', line 5

def _title
  self[:title] || self[:description]
end

#attachmentsObject



27
28
29
# File 'lib/klaro/client/resource/story.rb', line 27

def attachments
  @attachments || super.map{|a| Attachment.dress(a, @client) }
end

#cover_attachment(force = false) ⇒ Object



31
32
33
34
35
# File 'lib/klaro/client/resource/story.rb', line 31

def cover_attachment(force = false)
  got = (self.attachments || []).find{|a| a[:isCover] }
  got = (self.attachments || []).first if got.nil? and force
  got
end

#detailsObject Also known as: specification



17
18
19
20
# File 'lib/klaro/client/resource/story.rb', line 17

def details
  details = self[:details] || self[:specification]
  @specification ||= MdText.new(details, :details)
end

#download_and_relocate_attachments(root_path, target_folder, client) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/klaro/client/resource/story.rb', line 45

def download_and_relocate_attachments(root_path, target_folder, client)
  as = self.attachments.map do |attachment|
    url = attachment.url
    url += "?n=" + URI.encode_www_form_component(attachment.filename) unless url =~ /\?n=/
    path = handle_image(url, root_path, target_folder, client)
    attachment.url = path
    attachment
  end
  self.class.dress(self.to_h.merge(
    attachments: as
  ), @client)
end

#download_and_relocate_images(root_path, target_folder, client) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/klaro/client/resource/story.rb', line 58

def download_and_relocate_images(root_path, target_folder, client)
  spec = self.specification.to_s.gsub(%r{!\[([^\]]*)\]\(([^\)]*)\)}) do
    m = Regexp.last_match
    label, url = m[1], m[2]
    image_relative_path = handle_image(url, root_path, target_folder, client)
    "![#{label}](#{image_relative_path})"
  end
  self.class.dress(self.to_h.merge(
    specification: spec
  ), @client)
end

#linked_cardsObject



23
24
25
# File 'lib/klaro/client/resource/story.rb', line 23

def linked_cards
  @linked_cards ||= (self.linked || []).map{|s| Story.dress(s, @client) }
end

#summaryObject



13
14
15
# File 'lib/klaro/client/resource/story.rb', line 13

def summary
  @summary ||= MdText.new(self._title.split("\n")[1..-1].join("\n"), :summary)
end

#titleObject



9
10
11
# File 'lib/klaro/client/resource/story.rb', line 9

def title
  @title ||= MdText.new(self._title.split("\n").first, :summary)
end

#to_url(with_identifier = true) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/klaro/client/resource/story.rb', line 37

def to_url(with_identifier = true)
  I18n.config.available_locales = [:en, :fr]
  url = I18n.transliterate(title.to_s)
  url = url.downcase.gsub(/[^\w]+/,"-").gsub(/^[-]+|[-]+$/,"")
  url = "#{self.identifier}-#{url}" if with_identifier
  url
end