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

.curie?(prefix_or_curie) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/prefix.rb', line 53

def Prefix.curie?(prefix_or_curie)
  prefix_or_curie.include? ':'
end

.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



45
46
47
48
49
50
51
# File 'lib/prefix.rb', line 45

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_or_curie) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/prefix.rb', line 30

def Prefix.uri(prefix_or_curie)
  init() unless test ?e, CONFIG_FILE
  json = JSON.load( File.open(CONFIG_FILE, "r") )
  if Prefix.curie? prefix_or_curie
    match = prefix_or_curie.match /([^:]*):(.*)/
    if json[match[1]]
      puts json[match[1]] + match[2]
    else
      puts "Unknown prefix #{match[1]}"
    end
  else
    puts json[prefix_or_curie] || "Unknown prefix #{prefix_or_curie}"
  end
end