Class: Noty::Bookmark

Inherits:
Object
  • Object
show all
Defined in:
lib/noty/models/bookmark.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Bookmark

Returns a new instance of Bookmark.



10
11
12
13
14
15
16
# File 'lib/noty/models/bookmark.rb', line 10

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

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

Instance Attribute Details

#pathObject

Returns the value of attribute path.



8
9
10
# File 'lib/noty/models/bookmark.rb', line 8

def path
  @path
end

#titleObject

Returns the value of attribute title.



8
9
10
# File 'lib/noty/models/bookmark.rb', line 8

def title
  @title
end

#urlObject

Returns the value of attribute url.



8
9
10
# File 'lib/noty/models/bookmark.rb', line 8

def url
  @url
end

Class Method Details

.from_url(url) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/noty/models/bookmark.rb', line 18

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

Instance Method Details

#copyObject



47
48
49
# File 'lib/noty/models/bookmark.rb', line 47

def copy
  Helpers.copy url
end

#deleteObject



35
36
37
# File 'lib/noty/models/bookmark.rb', line 35

def delete
  File.delete path
end

#editObject



43
44
45
# File 'lib/noty/models/bookmark.rb', line 43

def edit
  Helpers.edit path
end

#openObject



39
40
41
# File 'lib/noty/models/bookmark.rb', line 39

def open
  Helpers.open_url url
end

#saveObject



31
32
33
# File 'lib/noty/models/bookmark.rb', line 31

def save
  File.write(path, to_yaml)
end

#to_s(short = false) ⇒ Object



51
52
53
# File 'lib/noty/models/bookmark.rb', line 51

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