Class: Prefix

Inherits:
Object
  • Object
show all
Defined in:
lib/prefix.rb

Constant Summary collapse

HOME =
ENV["HOME"] || ENV["HOMEPATH"] || File::expand_path("~")
CONFIG_DIR =
File::join HOME, ".prefix-cmd"
CONFIG_FILE =
File::join CONFIG_DIR, "prefixes.json"

Class Method Summary collapse

Class Method Details

.initObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/prefix.rb', line 12

def Prefix.init()
  puts "Initializing prefix cache"
  doc = Hpricot( open("http://prefix.cc/popular/all") )
  
  prefixes = {}
  doc.search("a").each do |el|     
   prefixes[ el.inner_html ] = el["resource"]  if el["rel"] && el["rel"].match("rdfs:seeAlso")
  end
  
  FileUtils::mkdir_p CONFIG_DIR unless test ?d, CONFIG_DIR
  test ?e, CONFIG_FILE and FileUtils::mv CONFIG_FILE, "#{CONFIG_FILE}.bak"    
  
  File.open(CONFIG_FILE, "w") do |f|
    f.puts( prefixes.to_json )
  end
  
end

.prefix(uri) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/prefix.rb', line 36

def Prefix.prefix(uri)
  init() unless test ?e, CONFIG_FILE    
  json = JSON.load( File.open(CONFIG_FILE, "r") )
  json.each do |k,v|
    puts k if v == uri
  end
end

.uri(prefix) ⇒ Object



30
31
32
33
34
# File 'lib/prefix.rb', line 30

def Prefix.uri(prefix)
  init() unless test ?e, CONFIG_FILE
  json = JSON.load( File.open(CONFIG_FILE, "r") )
  puts json[prefix] || "Unknown prefix #{prefix}"
end