Module: GetUrl::LocalUrls

Extended by:
LocalUrls
Included in:
LocalUrls
Defined in:
lib/geturl/geturl-local-urls.rb

Overview

The LocalUrls manages the local stored urls.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#private_source_idObject (readonly)

Returns the value of attribute private_source_id.



11
12
13
# File 'lib/geturl/geturl-local-urls.rb', line 11

def private_source_id
  @private_source_id
end

Instance Method Details

#delete_url(url) ⇒ Object

Deletes the given url from the local storage.

Parameters:

  • url (String)

    The url.



51
52
53
54
55
# File 'lib/geturl/geturl-local-urls.rb', line 51

def delete_url(url)
  load_urls if @urls.nil?
  @items.delete_if {|item| item['url'] == url}
  save_urls
end

#load_urlsObject

Loads all urls items from the local storage.



14
15
16
# File 'lib/geturl/geturl-local-urls.rb', line 14

def load_urls
  @items = FileManager.load_items_from_yaml_file(@local_urls_file) rescue []
end

#save_url(url, name, options = {}) ⇒ Object

Adds or updates the data for the given url, and stores in the local storage.

Parameters:

  • url (String)

    The url.

  • name (String)

    The name.

  • options (Hash) (defaults to: {})

    The options, which might contains the tags and description.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/geturl/geturl-local-urls.rb', line 30

def save_url(url, name, options = {})
  return if (url.to_s.empty?) || (name.to_s.empty?)

  load_urls
  @items.delete_if {|item| item['url'] == url}
  options['tags'] ||= []
  options['tags'].map! {|tag| tag.strip}

  @items << {
      'url' => url,
      'source' => 'local',
      'name' => name,
      'description' => options['description'],
      'tags' => options['tags']
  }
  save_urls
end

#save_urlsObject

Stores all urls ites to the local storage.



19
20
21
22
23
# File 'lib/geturl/geturl-local-urls.rb', line 19

def save_urls
  FileManager.save_items_to_yaml_file(@items, @local_urls_file)
  FileManager.clear_all_items_cache
  return @items.size
end