Skip to content

Commit 0fb1b28

Browse files
committed
Initial commit to http_require.
0 parents  commit 0fb1b28

10 files changed

+247
-0
lines changed

.document

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
README.rdoc
2+
lib/**/*.rb
3+
bin/*
4+
features/**/*.feature
5+
LICENSE

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.sw?
2+
.DS_Store
3+
coverage
4+
rdoc
5+
pkg

LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2009 Vitaly Kushner
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.markdown

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# http_require
2+
3+
## Idea
4+
5+
The idea is very simple:
6+
test.rb:
7+
# this will download bar.rb and eval its content.
8+
require "http://foo.com/bar.rb"
9+
10+
If a remote file (or one of its local dependencies) requires something that
11+
can't be found locally, it will try to find it remotely from the same location
12+
as the parent.
13+
14+
## Example
15+
16+
http://example.com/test/foo.rb:
17+
# this will load "http://example.com/test/foo/bar.rb"
18+
# if "foo/bar" is not available locally
19+
require "foo/bar"
20+
21+
## Homepage
22+
23+
You can find the latest sources on [github]:(http://github.com/astrails/http_require)
24+
25+
## Installation
26+
27+
sudo gem install astrails-http_require --source http://gems.github.com/
28+
29+
## Copyright
30+
31+
Copyright (c) 2009 Vitaly Kushner. See LICENSE for details.

Rakefile

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require 'rubygems'
2+
require 'rake'
3+
4+
begin
5+
require 'jeweler'
6+
Jeweler::Tasks.new do |gem|
7+
gem.name = "http_require"
8+
gem.summary = %Q{allows you to require "http://foo/bar.rb"}
9+
gem.email = "[email protected]"
10+
gem.homepage = "http://github.com/astrails/http_require"
11+
gem.authors = ["Vitaly Kushner"]
12+
13+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14+
end
15+
rescue LoadError
16+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
17+
end
18+
19+
require 'micronaut/rake_task'
20+
Micronaut::RakeTask.new(:examples) do |examples|
21+
examples.pattern = 'examples/**/*_example.rb'
22+
examples.ruby_opts << '-Ilib -Iexamples'
23+
end
24+
25+
Micronaut::RakeTask.new(:rcov) do |examples|
26+
examples.pattern = 'examples/**/*_example.rb'
27+
examples.rcov_opts = '-Ilib -Iexamples'
28+
examples.rcov = true
29+
end
30+
31+
32+
task :default => :examples
33+
34+
require 'rake/rdoctask'
35+
Rake::RDocTask.new do |rdoc|
36+
if File.exist?('VERSION.yml')
37+
config = YAML.load(File.read('VERSION.yml'))
38+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
39+
else
40+
version = ""
41+
end
42+
43+
rdoc.rdoc_dir = 'rdoc'
44+
rdoc.title = "http_require #{version}"
45+
rdoc.rdoc_files.include('README*')
46+
rdoc.rdoc_files.include('lib/**/*.rb')
47+
end
48+

VERSION.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
:major: 0
3+
:minor: 0
4+
:patch: 1

examples/example_helper.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'rubygems'
2+
require 'micronaut'
3+
require 'fakeweb'
4+
require 'ruby-debug'
5+
6+
$LOAD_PATH.unshift(File.dirname(__FILE__))
7+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8+
9+
require 'http_require'
10+
11+
FakeWeb.allow_net_connect = false
12+
13+
def not_in_editor?
14+
!(ENV.has_key?('TM_MODE') || ENV.has_key?('EMACS') || ENV.has_key?('VIM'))
15+
end
16+
17+
Micronaut.configure do |c|
18+
c.mock_with :mocha
19+
c.color_enabled = not_in_editor?
20+
c.filter_run :focused => true
21+
c.profile_examples = true
22+
end
23+

examples/http_require_example.rb

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require 'example_helper'
2+
3+
describe "HttpRequire" do
4+
5+
BASE = "http://foobar.com"
6+
A = "#{BASE}/a.rb"
7+
B = "#{BASE}/b.rb"
8+
9+
before do
10+
FakeWeb.clean_registry
11+
end
12+
13+
it "should eval file from a web" do
14+
FakeWeb.register_uri(:get, A, :string => "A")
15+
self.expects(:eval).with("A", anything, A)
16+
require A
17+
end
18+
19+
it "should set http_require_base" do
20+
FakeWeb.register_uri(:get, A, :string => "A")
21+
self.expects(:with_http_require_base).with(BASE)
22+
require A
23+
end
24+
25+
it "should require from web when http_require_base is set" do
26+
FakeWeb.register_uri(:get, A, :string => "A")
27+
self.expects(:eval).with('A', anything, A)
28+
with_http_require_base(BASE) do
29+
require 'a.rb'
30+
end
31+
end
32+
33+
it "should add '.rb' if missing" do
34+
FakeWeb.register_uri(:get, A, :string => "A")
35+
self.expects(:eval).with('A', anything, A)
36+
with_http_require_base(BASE) do
37+
require 'a'
38+
end
39+
end
40+
41+
end

http_require.gemspec

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- encoding: utf-8 -*-
2+
3+
Gem::Specification.new do |s|
4+
s.name = %q{http_require}
5+
s.version = "0.0.1"
6+
7+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8+
s.authors = ["Vitaly Kushner"]
9+
s.date = %q{2009-05-12}
10+
s.email = %q{[email protected]}
11+
s.extra_rdoc_files = ["README.markdown", "LICENSE"]
12+
s.files = ["README.markdown", "VERSION.yml", "lib/http_require.rb", "LICENSE"]
13+
s.has_rdoc = true
14+
s.homepage = %q{http://github.com/astrails/http_require}
15+
s.rdoc_options = ["--inline-source", "--charset=UTF-8"]
16+
s.require_paths = ["lib"]
17+
s.rubygems_version = %q{1.3.1}
18+
s.summary = %q{allows you to require "http://foo/bar.rb"}
19+
20+
if s.respond_to? :specification_version then
21+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
22+
s.specification_version = 2
23+
24+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
25+
else
26+
end
27+
else
28+
end
29+
end

lib/http_require.rb

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require 'open-uri'
2+
3+
module Kernel
4+
5+
class HttpLoadError < LoadError; end
6+
7+
@@http_require_base = nil
8+
def with_http_require_base(base)
9+
base, @http_require_base = @http_require_base, base
10+
yield
11+
ensure
12+
@http_require_base = base
13+
end
14+
15+
def http_require(path)
16+
uri = path.is_a?(URI) ? path : URI.parse(path)
17+
18+
content = uri.read
19+
20+
with_http_require_base(File.dirname(uri.to_s)) do
21+
eval(content, binding, uri.to_s)
22+
end
23+
24+
end
25+
26+
alias :require_without_http :require
27+
def require(path)
28+
uri = URI.parse(path) rescue nil
29+
return http_require(uri) if uri.is_a?(URI) && (uri.class != URI::Generic)
30+
31+
require_without_http(path)
32+
33+
rescue LoadError => e
34+
if @http_require_base
35+
path += ".rb" unless path[-3..-1] == ".rb"
36+
http_require "#{@http_require_base}/#{path}"
37+
else
38+
raise e
39+
end
40+
end
41+
end

0 commit comments

Comments
 (0)