eat 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in eat.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,21 @@
1
+ =eat
2
+
3
+ Instead of
4
+
5
+ require 'open-uri'
6
+ open('http://169.254.169.254/latest/meta-data/security-groups').gets
7
+ open('/home/seamus/foo.txt').gets
8
+
9
+ Try
10
+
11
+ require 'eat'
12
+ eat('http://169.254.169.254/latest/meta-data/security-groups')
13
+ eat('/home/seamus/foo.txt')
14
+
15
+ ==Supported schemas
16
+
17
+ * local filesystem (it will try to use <tt>sudo</tt> if it can't read the file)
18
+ * http (the default timeout is 5 seconds)
19
+ * https (it won't check the SSL certificate... if you want security, don't use this!)
20
+
21
+ Copyright 2011 Seamus Abshere
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake'
5
+ require 'rake/testtask'
6
+ Rake::TestTask.new(:test) do |test|
7
+ test.libs << 'lib' << 'test'
8
+ test.pattern = 'test/**/test_*.rb'
9
+ test.verbose = true
10
+ end
data/eat.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "eat/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "eat"
7
+ s.version = Eat::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Seamus Abshere"]
10
+ s.email = ["seamus@abshere.net"]
11
+ s.homepage = "https://github.com/seamusabshere/eat"
12
+ s.summary = %q{A more trustworthy open-uri for use with RSS feeds, config scripts, etc.}
13
+ s.description = %q{Lets you open local and remote files by immediately returning their contents as a string.}
14
+
15
+ s.rubyforge_project = "eat"
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
+ s.add_development_dependency 'test-unit'
22
+ end
data/lib/eat.rb ADDED
@@ -0,0 +1,29 @@
1
+ require 'uri'
2
+
3
+ module Eat
4
+ def eat(filesystem_path_or_uri, remote_timeout = 5)
5
+ uri = ::URI.parse filesystem_path_or_uri
6
+ case uri.scheme
7
+ when nil
8
+ if ::File.readable? uri.path
9
+ ::IO.read uri.path
10
+ else
11
+ `sudo cat #{uri.path}`
12
+ end
13
+ when 'http', 'https'
14
+ require 'net/http'
15
+ require 'net/https' if uri.scheme == 'https'
16
+ (defined?(::SystemTimer) ? ::SystemTimer : ::Timeout).timeout(remote_timeout) do
17
+ http = ::Net::HTTP.new uri.host, uri.port
18
+ if uri.scheme == 'https'
19
+ http.use_ssl = true
20
+ # if you were trying to be real safe, you wouldn't use this library
21
+ http.verify_mode = ::OpenSSL::SSL::VERIFY_NONE
22
+ end
23
+ http.request ::Net::HTTP::Get.new(uri.request_uri)
24
+ end.body
25
+ end
26
+ end
27
+ end
28
+
29
+ ::Object.send :include, ::Eat
@@ -0,0 +1,3 @@
1
+ module Eat
2
+ VERSION = "0.0.1"
3
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup
4
+ require 'test/unit'
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ require 'eat'
8
+ class Test::Unit::TestCase
9
+ end
data/test/test_eat.rb ADDED
@@ -0,0 +1,48 @@
1
+ require 'helper'
2
+
3
+ require 'tempfile'
4
+ class Tempfile
5
+ class TriedToUseMe < RuntimeError; end
6
+ def initialize(*args)
7
+ raise TriedToUseMe
8
+ end
9
+ end
10
+
11
+ class TestEat < Test::Unit::TestCase
12
+ def test_filesystem
13
+ assert eat(__FILE__).include?('class TestEat < Test::Unit::TestCase')
14
+ end
15
+
16
+ def test_http
17
+ assert eat('http://brighterplanet.com/robots.txt').include?('User-agent')
18
+ end
19
+
20
+ def test_https
21
+ assert eat('https://brighterplanet.com/robots.txt').include?('User-agent')
22
+ end
23
+
24
+ def test_sudo_filesystem
25
+ f = File.open('test_sudo_filesystem.txt', 'w')
26
+ f.write "hello world"
27
+ f.close
28
+ `sudo chown root #{f.path}`
29
+ `sudo chmod go-rwx #{f.path}`
30
+ assert !File.readable?(f.path)
31
+ assert eat(f.path).include?('hello world')
32
+ ensure
33
+ `sudo rm -f #{f.path}`
34
+ end
35
+
36
+ def test_openuri_uses_tempfile
37
+ assert_raises(Tempfile::TriedToUseMe) do
38
+ require 'open-uri'
39
+ open 'http://do1ircpq72156.cloudfront.net/0.2.47/javascripts/prototype.rails-3.0.3.js'
40
+ end
41
+ end
42
+
43
+ def test_eat_doesnt_use_tempfile
44
+ assert_nothing_raised do
45
+ eat 'http://do1ircpq72156.cloudfront.net/0.2.47/javascripts/prototype.rails-3.0.3.js'
46
+ end
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eat
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Seamus Abshere
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-11 00:00:00 -06:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: test-unit
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: Lets you open local and remote files by immediately returning their contents as a string.
36
+ email:
37
+ - seamus@abshere.net
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - .gitignore
46
+ - Gemfile
47
+ - README.rdoc
48
+ - Rakefile
49
+ - eat.gemspec
50
+ - lib/eat.rb
51
+ - lib/eat/version.rb
52
+ - test/helper.rb
53
+ - test/test_eat.rb
54
+ has_rdoc: true
55
+ homepage: https://github.com/seamusabshere/eat
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options: []
60
+
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project: eat
84
+ rubygems_version: 1.3.7
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: A more trustworthy open-uri for use with RSS feeds, config scripts, etc.
88
+ test_files:
89
+ - test/helper.rb
90
+ - test/test_eat.rb