webloc 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in webloc.gemspec
4
+ gemspec
@@ -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
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.libs << 'lib' << 'test'
7
+ test.pattern = 'test/**/*_test.rb'
8
+ test.verbose = true
9
+ end
10
+
11
+ task :default => :test
@@ -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
@@ -0,0 +1,3 @@
1
+ class Webloc
2
+ VERSION = "0.0.1"
3
+ end
Binary file
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
+ <plist version="1.0">
4
+ <dict>
5
+ <key>URL</key>
6
+ <string>https://github.com/peterc/webloc</string>
7
+ </dict>
8
+ </plist>
@@ -0,0 +1,26 @@
1
+ require 'test/unit'
2
+ require 'webloc'
3
+
4
+ class WeblocTest < Test::Unit::TestCase
5
+ def test_webloc_object_requires_url
6
+ assert_raise(ArgumentError) { Webloc.new }
7
+ end
8
+
9
+ def test_webloc_object_created_with_url
10
+ assert_equal 'http://example.com', Webloc.new('http://example.com').url
11
+ end
12
+
13
+ def test_webloc_object_loaded_from_old_style_file
14
+ assert_equal 'https://github.com/peterc/webloc', Webloc.load(File.dirname(__FILE__) + '/oldstyle.webloc').url
15
+ end
16
+
17
+ def test_webloc_object_loaded_from_plist_file
18
+ assert_equal 'https://github.com/peterc/webloc', Webloc.load(File.dirname(__FILE__) + '/pliststyle.webloc').url
19
+ end
20
+
21
+ def test_webloc_generates_valid_data
22
+ data = File.read(File.dirname(__FILE__) + '/oldstyle.webloc')
23
+ data = data.force_encoding('binary') rescue data
24
+ assert_equal data, Webloc.new('https://github.com/peterc/webloc').data
25
+ end
26
+ end
@@ -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
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webloc
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Peter Cooper
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-26 00:00:00 +00:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: plist
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ description: Webloc reads and writes .webloc files on OS X
34
+ email:
35
+ - peter@petercooper.co.uk
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - README.md
46
+ - Rakefile
47
+ - lib/webloc.rb
48
+ - lib/webloc/version.rb
49
+ - test/oldstyle.webloc
50
+ - test/pliststyle.webloc
51
+ - test/webloc_test.rb
52
+ - webloc.gemspec
53
+ has_rdoc: true
54
+ homepage: http://github.com/peterc/webloc
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options: []
59
+
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project: webloc
81
+ rubygems_version: 1.3.7
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Reads and writes .webloc files on OS X
85
+ test_files:
86
+ - test/oldstyle.webloc
87
+ - test/pliststyle.webloc
88
+ - test/webloc_test.rb