Created
August 31, 2012 14:40
-
-
Save tobstarr/3553793 to your computer and use it in GitHub Desktop.
Bookmark System
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
first,link: http://www.some.url | |
second,link: http://www.someother.url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require File.expand_path("../lib/bookmark.rb", __FILE__) | |
Bookmark.run_with_args(ARGV) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Bookmark | |
class << self | |
def run_with_args(args) | |
query = args.join(" ") | |
bookmarks = Bookmark.matching(query) | |
if bookmarks.empty? | |
puts "no bookmarks found for #{query.inspect}" | |
else | |
bookmarks.map(&:open) | |
end | |
end | |
def matching(query, bookmarks_string = default_bookmarks_string) | |
from_file_content(bookmarks_string).select do |bm| | |
bm.matches?(query) | |
end | |
end | |
private | |
def from_line(line) | |
tags_s, resource = line.split(": ") | |
new(tags_s.split(/[\s,]+/), resource) | |
end | |
def from_file_content(content) | |
content.split("\n").map { |line| from_line(line) } | |
end | |
def default_bookmarks_path | |
File.expand_path("~/.bookmarks") | |
end | |
def default_bookmarks_string | |
File.exists?(default_bookmarks_path) ? File.read(default_bookmarks_path) : '' | |
end | |
end | |
def initialize(tags, url) | |
@tags = tags | |
@url = url | |
end | |
def matches?(string) | |
string.strip.split(/\s+/).all? do |query_tag| | |
@tags.any? { |tag| tag.start_with?(query_tag) } | |
end | |
end | |
def open | |
system %(open "#{@url}") | |
end | |
private | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment