Sha256: b3f5c43cde730afbe9156bbcbb798e8c88ed3ef31cc295bb8fc837064af6708f

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require 'open-uri'
require 'yaml'
require 'noty/storage'
require 'noty/helpers'

module Noty
  class Bookmark
    attr_accessor :path, :url, :title

    def initialize(path)
      content = File.exist?(path) ? YAML.load_file(path) : ''

      @path = path
      @url = content['url']
      @title = content['title']
    end

    def self.from_url(url)
      file_path = File.join(STORAGE_PATH, Time.now.to_i.to_s + '.bookmark')
      content = begin
                  open(url).read
                rescue
                  ''
                end
      bookmark = new(file_path)
      bookmark.url = url
      bookmark.title = content.match(%r{<title>(.+)<\/title>}im).to_a[1]
      bookmark
    end

    def save
      File.write(path, to_yaml)
    end

    def delete
      File.delete path
    end

    def open
      Helpers.open_url url
    end

    def edit
      Helpers.edit path
    end

    def copy
      Helpers.copy url
    end

    def to_s(short = false)
      short ? title.to_s : "#{title} (#{url})"
    end

    private

    def to_yaml
      {
        'url' => url,
        'title' => title
      }.to_yaml
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
noty-0.1.1 lib/noty/models/bookmark.rb