Skip to content

Commit 9bf3f42

Browse files
committedJan 26, 2011
First commit
0 parents  commit 9bf3f42

File tree

7 files changed

+100
-0
lines changed

7 files changed

+100
-0
lines changed
 

‎.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pkg/*
2+
*.gem
3+
.bundle

‎Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source "http://rubygems.org"
2+
3+
# Specify your gem's dependencies in webloc.gemspec
4+
gemspec

‎README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# webloc
2+
3+
*webloc* is a Ruby library that can read from and write to <tt>.webloc</tt> files as used on OS X.
4+
5+
It works on both Ruby 1.9.2 and 1.8.7 (though development is focused on 1.9.2).
6+
7+
## Installation
8+
9+
gem install webloc
10+
11+
## Usage
12+
13+
It's pretty simple.
14+
15+
Reading a .webloc file:
16+
17+
Webloc.load(ARGV.first).url
18+
19+
Writing to a .webloc file:
20+
21+
Webloc.new('http://peterc.org/').save('peterc.webloc')
22+
23+
## License
24+
25+
Copyright (C) 2011 Peter Cooper
26+
27+
webloc is licensed under the terms of the MIT License

‎Rakefile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require 'bundler'
2+
Bundler::GemHelper.install_tasks

‎lib/webloc.rb

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require 'plist'
2+
3+
class Webloc
4+
attr_accessor :url
5+
6+
def initialize(url)
7+
@url = url
8+
end
9+
10+
def self.load(filename)
11+
data = File.read(filename)
12+
data = data.force_encoding('binary') rescue data
13+
14+
if data !~ /\<plist/
15+
offset = (data =~ /SURL_/)
16+
length = data[offset + 6]
17+
length = length.ord rescue length
18+
url = data[offset + 7,length]
19+
else
20+
url = Plist::parse_xml(filename)['URL'] rescue nil
21+
end
22+
23+
raise ArgumentError unless url
24+
new(url)
25+
end
26+
27+
def data
28+
@data = "\x62\x70\x6C\x69\x73\x74\x30\x30\xD1\x01\x02\x53\x55\x52\x4C\x5F\x10"
29+
@data += @url.length.chr
30+
@data += @url
31+
@data += "\x08\x0B\x0F\x00\x00\x00\x00\x00\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
32+
@data += (@url.length + 18).chr
33+
end
34+
35+
def save(filename)
36+
File.open(filename, 'w:binary') { |f| f.print data }
37+
end
38+
end

‎lib/webloc/version.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Webloc
2+
VERSION = "0.0.1"
3+
end

‎webloc.gemspec

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- encoding: utf-8 -*-
2+
$:.push File.expand_path("../lib", __FILE__)
3+
require "webloc/version"
4+
5+
Gem::Specification.new do |s|
6+
s.name = "webloc"
7+
s.version = Webloc::VERSION
8+
s.platform = Gem::Platform::RUBY
9+
s.authors = ["Peter Cooper"]
10+
s.email = ["peter@petercooper.co.uk"]
11+
s.homepage = "http://github.com/peterc/webloc"
12+
s.summary = %q{Reads and writes .webloc files on OS X}
13+
s.description = %q{Webloc reads and writes .webloc files on OS X}
14+
15+
s.rubyforge_project = "webloc"
16+
17+
s.files = `git ls-files`.split("\n")
18+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20+
s.require_paths = ["lib"]
21+
22+
s.add_dependency 'plist'
23+
end

0 commit comments

Comments
 (0)
Please sign in to comment.