prefix-cmd 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. data/README.rdoc +32 -0
  2. data/Rakefile +58 -0
  3. data/bin/prefix-cmd +42 -0
  4. data/lib/prefix.rb +44 -0
  5. metadata +98 -0
data/README.rdoc ADDED
@@ -0,0 +1,32 @@
1
+ A simple command-line tool for working with the data collected by
2
+ Richard Cyganiak's excellent service http://prefix.cc.
3
+
4
+ I rely on the service as the easiest way to remember URIs for RDF vocabularies.
5
+ But I often find myself working on a train or a plane and so the service is
6
+ accessible.
7
+
8
+ What I really wanted is a way to query the knowledge base both on and offline.
9
+
10
+ Prefix command provides you with a simple command-line tool to do exactly that.
11
+
12
+ The command caches a local copy of the data available from:
13
+
14
+ http://prefix.cc/popular/all
15
+
16
+ The data is fetched and cached in:
17
+
18
+ ~/.prefix-cmd/prefixes.json
19
+
20
+ There are three options:
21
+
22
+ prefix-cmd init #initialise or refresh your local cache
23
+
24
+ To lookup a URI:
25
+
26
+ prefix-cmd uri [prefix]
27
+
28
+ To suggest a prefix:
29
+
30
+ prefix-cmd prefix [uri]
31
+
32
+ Thats all!
data/Rakefile ADDED
@@ -0,0 +1,58 @@
1
+ require 'rake'
2
+
3
+ require 'rake/gempackagetask'
4
+ require 'rake/rdoctask'
5
+ require 'rake/testtask'
6
+ require 'rake/clean'
7
+
8
+ require "redis"
9
+ require "digest/md5"
10
+
11
+ NAME = "prefix-cmd"
12
+ VER = "0.1"
13
+
14
+ PKG_FILES = %w( README.rdoc Rakefile ) +
15
+ Dir.glob("{bin,lib}/**/*")
16
+
17
+ CLEAN.include ['*.gem', 'pkg']
18
+ SPEC =
19
+ Gem::Specification.new do |s|
20
+ s.name = NAME
21
+ s.version = VER
22
+ s.platform = Gem::Platform::RUBY
23
+ s.required_ruby_version = ">= 1.8.7"
24
+ s.has_rdoc = true
25
+ s.extra_rdoc_files = ["README.rdoc"]
26
+ s.summary = "Prefix.cc Command"
27
+ s.description = s.summary
28
+ s.author = "Leigh Dodds"
29
+ s.email = 'leigh.dodds@talis.com'
30
+ s.homepage = 'http://github.org/ldodds/prefix-cmd'
31
+ s.files = PKG_FILES
32
+ s.require_path = "lib"
33
+ s.bindir = "bin"
34
+ s.executables = ["prefix-cmd"]
35
+ # s.test_file = "tests/unit/ts_api.rb"
36
+ s.add_dependency("hpricot")
37
+ s.add_dependency("json_pure")
38
+
39
+ end
40
+
41
+ Rake::GemPackageTask.new(SPEC) do |pkg|
42
+ pkg.need_tar = true
43
+ end
44
+
45
+ Rake::TestTask.new do |test|
46
+ test.test_files = FileList['tests/unit/tc_*.rb']
47
+ end
48
+
49
+ desc "Install from a locally built copy of the gem"
50
+ task :install do
51
+ sh %{rake package}
52
+ sh %{sudo gem install pkg/#{NAME}-#{VER}}
53
+ end
54
+
55
+ desc "Uninstall the gem"
56
+ task :uninstall => [:clean] do
57
+ sh %{sudo gem uninstall #{NAME}}
58
+ end
data/bin/prefix-cmd ADDED
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'prefix'
4
+
5
+ PROGRAM = File::basename $0
6
+
7
+ USAGE = <<-EOL
8
+ SYNOPSIS
9
+
10
+ #{ PROGRAM } mode [args]*
11
+
12
+ DESCRIPTION
13
+
14
+ MODE
15
+
16
+ init
17
+ Initializes your local cache of data, or refreshes a previously cached copy
18
+ The data is stored in ~/.prefix-cmd/prefixes.json
19
+
20
+ uri [prefix]
21
+ Lookup the uri for a given prefix
22
+
23
+ prefix [uri]
24
+ Lookup the prefix for a given vocabulary URI
25
+
26
+ EOL
27
+
28
+ mode = ARGV.shift
29
+
30
+ case mode
31
+ when "init"
32
+ Prefix.init
33
+ when "uri"
34
+ Prefix.uri ARGV.shift
35
+ when "prefix"
36
+ Prefix.prefix ARGV.shift
37
+ when "help"
38
+ USAGE.display
39
+ else
40
+ puts "Unknown command"
41
+ USAGE.display
42
+ end
data/lib/prefix.rb ADDED
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+ require 'hpricot'
3
+ require 'open-uri'
4
+ require 'json/pure'
5
+
6
+ class Prefix
7
+
8
+ HOME = ENV["HOME"] || ENV["HOMEPATH"] || File::expand_path("~")
9
+ CONFIG_DIR = File::join HOME, ".prefix-cmd"
10
+ CONFIG_FILE = File::join CONFIG_DIR, "prefixes.json"
11
+
12
+ def Prefix.init()
13
+ puts "Initializing prefix cache"
14
+ doc = Hpricot( open("http://prefix.cc/popular/all") )
15
+
16
+ prefixes = {}
17
+ doc.search("a").each do |el|
18
+ prefixes[ el.inner_html ] = el["resource"] if el["rel"] && el["rel"].match("rdfs:seeAlso")
19
+ end
20
+
21
+ FileUtils::mkdir_p CONFIG_DIR unless test ?d, CONFIG_DIR
22
+ test ?e, CONFIG_FILE and FileUtils::mv CONFIG_FILE, "#{CONFIG_FILE}.bak"
23
+
24
+ File.open(CONFIG_FILE, "w") do |f|
25
+ f.puts( prefixes.to_json )
26
+ end
27
+
28
+ end
29
+
30
+ def Prefix.uri(prefix)
31
+ init() unless test ?e, CONFIG_FILE
32
+ json = JSON.load( File.open(CONFIG_FILE, "r") )
33
+ puts json[prefix] || "Unknown prefix #{prefix}"
34
+ end
35
+
36
+ def Prefix.prefix(uri)
37
+ init() unless test ?e, CONFIG_FILE
38
+ json = JSON.load( File.open(CONFIG_FILE, "r") )
39
+ json.each do |k,v|
40
+ puts k if v == uri
41
+ end
42
+ end
43
+
44
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prefix-cmd
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
10
+ platform: ruby
11
+ authors:
12
+ - Leigh Dodds
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-26 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: hpricot
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: json_pure
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ description: Prefix.cc Command
49
+ email: leigh.dodds@talis.com
50
+ executables:
51
+ - prefix-cmd
52
+ extensions: []
53
+
54
+ extra_rdoc_files:
55
+ - README.rdoc
56
+ files:
57
+ - README.rdoc
58
+ - Rakefile
59
+ - bin/prefix-cmd
60
+ - lib/prefix.rb
61
+ has_rdoc: true
62
+ homepage: http://github.org/ldodds/prefix-cmd
63
+ licenses: []
64
+
65
+ post_install_message:
66
+ rdoc_options: []
67
+
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 57
76
+ segments:
77
+ - 1
78
+ - 8
79
+ - 7
80
+ version: 1.8.7
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ requirements: []
91
+
92
+ rubyforge_project:
93
+ rubygems_version: 1.3.7
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: Prefix.cc Command
97
+ test_files: []
98
+