Sha256: 08407257a4f0a449b770fd9a5d84ce11f3d9cf9af2fc240b7fb623323ce17122

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

class Storys::Story
  attr_reader :package
  attr_reader :path

  def initialize(package, path)
    @package = package
    @path = path
  end

  #It is assumed that the HTML document is a valid HTML expression of an NSF document
  def html
    @html ||= File.read(@path)
  end

  def path_hash
    Digest::SHA256.hexdigest(path.to_s)[0..16]
  end

  def url
    package.pathname_to_url(path, package.package_path)
  end

  def title
    title = title_from_html
    title = path.basename.to_s.chomp(path.extname.to_s) if title == ""

    directory_path = path.relative_path_from(package.root_path).dirname.to_s

    title = "#{directory_path}/#{title}" unless directory_path == "" || directory_path == "."
    title = title.gsub("/", " / ")

    title
  end

  def self.from_hash(package, data)
    Storys::Story.new(package, package.url_to_pathname(Addressable::URI.parse(data["url"])))
  end

  def to_hash
    {
      "url" => url,
      "wordCount" => word_count_from_html,
      "title" => title,
      "publishedOn" => path.mtime.to_i,
      "key" => path_hash
    }
  end

  private

  def title_from_html
    html =~ /<title>(.*?)<\/title>/m
    $1 ? CGI::unescapeHTML($1) : ""
  end

  def word_count_from_html
    html =~ /<body>(.*?)<\/body>/m
    body = CGI::unescapeHTML($1.gsub(/<\/?(p|b|i|h[1234567]).*?>/m, " "))
    (title + " " + (body ? body : "")).split(/\s+/).length
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
storys-0.0.4 lib/storys/story.rb