module BookmarksReader

TODO: make private methods… well, private!

Constants

MOZILLA_FIREFOX_CONF_DIR

Attributes

db[R]

Public Instance Methods

bookmarks_count() click to toggle source
# File lib/fundler/bookmarks_reader.rb, line 45
def bookmarks_count
  stored_bookmarks.size
end
drop_bookmarks(format = :txt) click to toggle source

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.

# 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_places() click to toggle source
# File lib/fundler/bookmarks_reader.rb, line 128
def dump_places
  FileUtils.cp(locate_places, pwd)
end
find_bookmarks_by_tag(tag_id) click to toggle source
# 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_tags() click to toggle source
# 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_tag() click to toggle source
# 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) click to toggle source
# 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_tags() click to toggle source
# 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_places() click to toggle source
# 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_bookmarks() click to toggle source
# 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