class Pincerna::Bookmark

Show the list of Safari bookmarks.

Constants

ICON

The icon to show for each feedback item.

MATCHER

The expression to match.

SEPARATOR

The separator for paths.

Public Instance Methods

perform_filtering(query) click to toggle source

Reads the list of Chrome Bookmarks.

@param query [Array] A query to match against bookmarks names. @return [Array] A list of boomarks.

# File lib/pincerna/bookmark.rb, line 23
def perform_filtering(query)
  Pincerna::Cache.instance.use("bookmarks:#{self.class.to_s}", Pincerna::Cache::EXPIRATIONS[:short]) do
    # Get bookmarks and then only keep valid ones
    @bookmarks = []
    read_bookmarks
    filter_and_sort(@bookmarks, query)
  end
end
process_results(results) click to toggle source

Processes items to obtain feedback items.

@param results [Array] The items to process. @return [Array] The feedback items.

# File lib/pincerna/bookmark.rb, line 36
def process_results(results)
  results.map do |result|
    subtitle = result[:path] ? result[:path].gsub(/^\s\u2192 /, "") : "Action this item to open the URL in the browser ..."
    {title: result[:name], arg: result[:url], subtitle: subtitle, icon: ICON}
  end
end
read_bookmarks() click to toggle source

Reads the list of bookmarks.

# File lib/pincerna/bookmark.rb, line 44
def read_bookmarks
  raise ArgumentError.new("Must be overriden by subclasses.")
end

Protected Instance Methods

add_bookmark(title, url, path) click to toggle source

Adds a bookmarks to the list.

@param title [String] The title of the bookmark. @param url [String] The URL of the bookmark. @param path [String] The id of the parent folder.

# File lib/pincerna/bookmark.rb, line 72
def add_bookmark(title, url, path)
  @bookmarks << {name: title, url: url, path: path} if title && !title.empty? && url && !url.empty? && url !~ /^javascript:/
end

Private Instance Methods

filter_and_sort(bookmarks, query) click to toggle source

Filters and sorts bookmarks.

@param bookmarks [Array] The bookmarks list. @param query [String] The query to filter.

# File lib/pincerna/bookmark.rb, line 53
def filter_and_sort(bookmarks, query)
  # Filtering
  matcher = !query.empty? ? /^#{Regexp.escape(query.downcase)}/i : nil
  bookmarks = bookmarks.select {|bookmark| bookmark[:name] =~ matcher } if matcher

  # Sorting
  bookmarks.sort {|first, second|
    cmp = first[:name] <=> second[:name]
    cmp = first[:path] <=> second[:path] if cmp == 0
    cmp
  }
end