class Klaro::Client::Story

Public Instance Methods

_title() click to toggle source
# File lib/klaro/client/resource/story.rb, line 5
def _title
  self[:title] || self[:description]
end
attachments() click to toggle source
Calls superclass method
# File lib/klaro/client/resource/story.rb, line 27
def attachments
  @attachments || super.map{|a| Attachment.dress(a, @client) }
end
cover_attachment(force = false) click to toggle source
# 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
details() click to toggle source
# File lib/klaro/client/resource/story.rb, line 17
def details
  details = self[:details] || self[:specification]
  @specification ||= MdText.new(details, :details)
end
Also aliased as: specification
download_and_relocate_attachments(root_path, target_folder, client) click to toggle source
# 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) click to toggle source
# 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_cards() click to toggle source
# File lib/klaro/client/resource/story.rb, line 23
def linked_cards
  @linked_cards ||= (self.linked || []).map{|s| Story.dress(s, @client) }
end
specification()
Alias for: details
summary() click to toggle source
# File lib/klaro/client/resource/story.rb, line 13
def summary
  @summary ||= MdText.new(self._title.split("\n")[1..-1].join("\n"), :summary)
end
title() click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

download_image(url, target_file, client) click to toggle source
# File lib/klaro/client/resource/story.rb, line 84
def download_image(url, target_file, client)
  target_file.parent.mkdir_p unless target_file.parent.exists?
  img = client.request.get("#{url}", true)
  target_file.write(img)
end
get_image_path(url, folder) click to toggle source
# File lib/klaro/client/resource/story.rb, line 78
def get_image_path(url, folder)
  file_name = URI.parse(url).query.gsub('n=', '')
  file_name = URI.decode_www_form_component(file_name)
  folder/"#{self['card-kind']}/#{identifier}/#{file_name}"
end
handle_image(url, root_path, folder, client) click to toggle source
# File lib/klaro/client/resource/story.rb, line 72
def handle_image(url, root_path, folder, client)
  image_path = get_image_path(url, folder)
  download_image(url, image_path, client) unless image_path.exists?
  '/' + image_path.relative_to(root_path).to_s
end