Module: BookmarksReader

Includes:
QueryConstants
Included in:
Fundler
Defined in:
lib/fundler/bookmarks_reader.rb

Overview

TODO: make private methods… well, private!

Constant Summary collapse

MOZILLA_FIREFOX_CONF_DIR =
File.expand_path('~') + '/.mozilla/firefox/'

Constants included from QueryConstants

QueryConstants::BOOKMARKS_QUERY, QueryConstants::QUERY_BY_TAG, QueryConstants::QUERY_FOR_LINK, QueryConstants::QUERY_FOR_SIMPLE_BOOKMARKS, QueryConstants::QUERY_FOR_TAGS, QueryConstants::QUERY_FOR_TAG_BY_ID

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



13
14
15
# File 'lib/fundler/bookmarks_reader.rb', line 13

def db
  @db
end

Instance Method Details

#bookmarks_countObject



45
46
47
# File 'lib/fundler/bookmarks_reader.rb', line 45

def bookmarks_count
  stored_bookmarks.size
end

#drop_bookmarks(format = :txt) ⇒ Object

drop_bookmarks browse the home directory of the current user, locate the places.sqlite db_file generated by Firefox and retrieve all the bookmarks stored.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fundler/bookmarks_reader.rb', line 88

def drop_bookmarks(format = :txt)
  output = []
  if db
    get_bookmarks_with_url_title_descr_tag.each_with_index do |row, index|
      case format.intern
      when :txt
        output << "#{index}: #{row}"
        output << '---'
      when :html
        puts "TODO"
      when :markdown
        puts "TODO"
      when :json
        output << row.to_json
      else
        # exit 1
      end
    end
  end
  File.open("./bookmarks_dump.#{format.to_s}", 'w') do |file|
    file.puts output
  end
end

#dump_placesObject



128
129
130
# File 'lib/fundler/bookmarks_reader.rb', line 128

def dump_places
  FileUtils.cp(locate_places, pwd)
end

#find_bookmarks_by_tag(tag_id) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/fundler/bookmarks_reader.rb', line 17

def find_bookmarks_by_tag(tag_id)
  query = QUERY_BY_TAG
  query.gsub(/@tag_id/,tag_id)
  db.execute(query) do |row|
    (result ||= []) << row
  end
end

#get_bookmarks_with_tagsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fundler/bookmarks_reader.rb', line 49

def get_bookmarks_with_tags
  all = []
  @stored_bookmarks = {}
  if db
    db.execute(QUERY_FOR_SIMPLE_BOOKMARKS) do |bookmark|
      all << bookmark
    end
    all.each do |bookmark|
      bookmark_id, bookmark_fk, bookmark_title = bookmark
      if bookmark_fk
        link_query = QUERY_FOR_LINK.sub(/@fk_id/, bookmark_fk.to_s)
        links = db.execute(link_query) 
        links.each do |link|
          link_id, link_fk, link_parent = link
          tag_query = QUERY_FOR_TAG_BY_ID.gsub(/@parent/, link_parent.to_s)
          db.execute(tag_query) do |tag|
            tag_id, tag_title = tag
            @stored_bookmarks[bookmark_id] = { tag: tag_title, title: bookmark_title }
          end
        end
      end
    end
  end
  @stored_bookmarks.extend(HashBookmarksUtil)
end

#get_bookmarks_with_url_title_descr_tagObject



75
76
77
78
79
80
81
82
83
# File 'lib/fundler/bookmarks_reader.rb', line 75

def get_bookmarks_with_url_title_descr_tag
  unless stored_bookmarks.empty?
    db.execute(BOOKMARKS_QUERY).each do |bookmark|
      url, title, id, description = bookmark
      @stored_bookmarks[id].merge!({url: url, description: description})
    end
  end
  @stored_bookmarks
end

#get_tag_by_id(id) ⇒ Object



34
35
36
37
38
# File 'lib/fundler/bookmarks_reader.rb', line 34

def get_tag_by_id(id)
  query = QUERY_FOR_TAG_BY_ID
  query.gsub(/@tag_id/,id)
  db.get_first_row(query)
end

#get_tagsObject



25
26
27
28
29
30
31
32
# File 'lib/fundler/bookmarks_reader.rb', line 25

def get_tags
  tags = {}
  db.execute(QUERY_FOR_TAGS).each do |row|
    tag = row.split('|')
    tags[tag[0]] = tag[1]
  end
  tags
end

#locate_placesObject



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/fundler/bookmarks_reader.rb', line 112

def locate_places
  begin
    dot_firefox = Dir.open(MOZILLA_FIREFOX_CONF_DIR)
    profile_dir = dot_firefox.select {|dir| dir =~ /.*default/}
    db_file = MOZILLA_FIREFOX_CONF_DIR + profile_dir.join + '/places.sqlite'
  rescue Errno::ENOENT
    puts "no db_bookmarks found"
    # exit 1
    nil
  end
end

#stored_bookmarksObject



40
41
42
43
# File 'lib/fundler/bookmarks_reader.rb', line 40

def stored_bookmarks
  # humm... I don't like this choice
  @stored_bookmarks ||= get_bookmarks_with_tags
end