rack-gist 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +23 -0
- data/Gemfile +13 -0
- data/LICENSE +20 -0
- data/README.rdoc +35 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/rack/gist.rb +126 -0
- data/rack-gist.gemspec +83 -0
- data/spec/body-full.html +7 -0
- data/spec/body-multiple.html +8 -0
- data/spec/body-none.html +6 -0
- data/spec/body-partial.html +7 -0
- data/spec/full-gist.js +7 -0
- data/spec/partial-gist.js +7 -0
- data/spec/rack-gist_spec.rb +142 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +74 -0
- metadata +211 -0
data/.document
ADDED
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
|
3
|
+
gem 'hpricot', '>= 0.8.2'
|
4
|
+
gem 'rest-client', '>= 1.6.0'
|
5
|
+
|
6
|
+
group :development do
|
7
|
+
gem 'fakeweb', '>= 1.2.8'
|
8
|
+
gem 'rspec', '>= 1.3.0'
|
9
|
+
gem 'yard', '>= 0.5.8'
|
10
|
+
gem 'rack', '>= 1.2.1'
|
11
|
+
gem 'activesupport', '>= 2.3.8'
|
12
|
+
gem 'ruby-debug'
|
13
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Daniel Huckstep
|
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.
|
data/README.rdoc
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
= rack-gist
|
2
|
+
|
3
|
+
Install, require, use
|
4
|
+
|
5
|
+
gem install rack-gist
|
6
|
+
require 'rack/gist'
|
7
|
+
use Rack::Gist
|
8
|
+
|
9
|
+
It loads standard gist embeds in the background. With jQuery. THE GISTS ARE NOW DIAMONDS!
|
10
|
+
|
11
|
+
It uses jQuery. Don't use jQuery? rack-gist loads jQuery form Google for you. Already have jQuery? Ignore it.
|
12
|
+
|
13
|
+
use Rack::Gist, :jquery => false
|
14
|
+
|
15
|
+
Use something that jQuery conflicts with? Submit me a patch to fix it!
|
16
|
+
|
17
|
+
Want to cache your gists?
|
18
|
+
|
19
|
+
use Rack::Gist, :cache => ActiveSupport::Cache::MemoryStore.new
|
20
|
+
|
21
|
+
Or use an existing cache.
|
22
|
+
|
23
|
+
== Note on Patches/Pull Requests
|
24
|
+
|
25
|
+
* Fork the project.
|
26
|
+
* Make your feature addition or bug fix.
|
27
|
+
* Add tests for it. This is important so I don't break it in a
|
28
|
+
future version unintentionally.
|
29
|
+
* Commit, do not mess with rakefile, version, or history.
|
30
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
31
|
+
* Send me a pull request. Bonus points for topic branches.
|
32
|
+
|
33
|
+
== Copyright
|
34
|
+
|
35
|
+
Copyright (c) 2010 Daniel Huckstep. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'bundler'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |gem|
|
8
|
+
gem.name = 'rack-gist'
|
9
|
+
gem.summary = %Q{Asynchronous Github Gists. All with rack.}
|
10
|
+
gem.description = %Q{Load gists in the background. KTHXBYE!}
|
11
|
+
gem.email = 'darkhelmet@darkhelmetlive.com'
|
12
|
+
gem.homepage = 'http://github.com/darkhelmet/rack-gist'
|
13
|
+
gem.authors = ['Daniel Huckstep']
|
14
|
+
gem.add_bundler_dependencies
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts 'Jeweler (or a dependency) not available. Install it with: gem install jeweler'
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :spec => :check_dependencies
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
begin
|
39
|
+
require 'yard'
|
40
|
+
YARD::Rake::YardocTask.new
|
41
|
+
rescue LoadError
|
42
|
+
task :yardoc do
|
43
|
+
abort 'YARD is not available. In order to run yardoc, you must: sudo gem install yard'
|
44
|
+
end
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.5
|
data/lib/rack/gist.rb
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
require 'hpricot'
|
2
|
+
require 'rest-client'
|
3
|
+
|
4
|
+
module Rack
|
5
|
+
class Gist
|
6
|
+
def initialize(app, options = {})
|
7
|
+
@app = app
|
8
|
+
@gist_path =
|
9
|
+
@options = {
|
10
|
+
:jquery => true
|
11
|
+
}.merge(options)
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
if path(env).match(%r{^/gist\.github\.com})
|
16
|
+
serve_gist(env)
|
17
|
+
else
|
18
|
+
status, headers, body = @app.call(env)
|
19
|
+
rewrite(status, headers, body)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def rewrite(status, headers, body)
|
26
|
+
if 'text/html' == headers['Content-Type']
|
27
|
+
body = [body].flatten
|
28
|
+
body.map! do |part|
|
29
|
+
Hpricot(part.to_s).tap do |doc|
|
30
|
+
extras = false
|
31
|
+
doc.search('script[@src*="gist.github.com"]').each do |tag|
|
32
|
+
extras = true
|
33
|
+
tag['src'].match(regex).tap do |match|
|
34
|
+
id, file = match[1, 2]
|
35
|
+
suffix, extra = file ? ["#file_#{file}", "rack-gist-file='#{file}'"] : ['', '']
|
36
|
+
tag.swap("<div class='rack-gist' id='rack-gist-#{id}' gist-id='#{id}' #{extra}>Can't see this Gist? <a rel='nofollow' href='http://gist.github.com/#{id}#{suffix}'>View it on Github!</a></div>")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
if extras
|
41
|
+
doc.search('head').append(css_html)
|
42
|
+
doc.search('body').tap do |node|
|
43
|
+
node.append(jquery_link) if @options[:jquery]
|
44
|
+
node.append(jquery_helper)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end.to_s
|
48
|
+
end
|
49
|
+
headers['Content-Length'] = body.map { |part| Rack::Utils.bytesize(part) }.inject(0) { |sum, size| sum + size }.to_s
|
50
|
+
end
|
51
|
+
[status, headers, body]
|
52
|
+
end
|
53
|
+
|
54
|
+
def serve_gist(env)
|
55
|
+
gist_id, file = path(env).match(%r{gist\.github\.com/(\d+)(?:/(.*))?\.js})[1,2]
|
56
|
+
cache = @options[:cache]
|
57
|
+
gist = (cache ? cache.fetch(cache_key(gist_id, file), :expires_in => 3600) { get_gist(gist_id, file) } : get_gist(gist_id, file)).to_s
|
58
|
+
[
|
59
|
+
200,
|
60
|
+
{
|
61
|
+
'Content-Type' => 'application/javascript',
|
62
|
+
'Content-Length' => Rack::Utils.bytesize(gist).to_s
|
63
|
+
},
|
64
|
+
[gist]
|
65
|
+
]
|
66
|
+
end
|
67
|
+
|
68
|
+
def get_gist(gist_id, file)
|
69
|
+
gist = RestClient.get(gist_url(gist_id, file))
|
70
|
+
gist = gist.split("\n").reject do |part|
|
71
|
+
part.empty?
|
72
|
+
end.last
|
73
|
+
selector = "#rack-gist-#{gist_id}"
|
74
|
+
selector << %Q{[rack-gist-file="#{file}"]} if file
|
75
|
+
gist.sub(/document\.write/, %Q{$('#{selector}').replaceWith})
|
76
|
+
end
|
77
|
+
|
78
|
+
def cache_key(gist_id, file)
|
79
|
+
key = "rack-gist:#{gist_id}"
|
80
|
+
key << ":#{file.gsub(/\s/, '')}" unless file.nil?
|
81
|
+
key
|
82
|
+
end
|
83
|
+
|
84
|
+
def gist_url(gist_id, file)
|
85
|
+
url = "http://gist.github.com/#{gist_id}.js"
|
86
|
+
url << "?file=#{file}" unless file.nil?
|
87
|
+
url
|
88
|
+
end
|
89
|
+
|
90
|
+
def path(env)
|
91
|
+
Rack::Utils.unescape(env['PATH_INFO'])
|
92
|
+
end
|
93
|
+
|
94
|
+
def regex
|
95
|
+
@regex ||= %r{gist\.github\.com/(\d+)\.js(?:\?file=(.*))?}
|
96
|
+
end
|
97
|
+
|
98
|
+
def css_html
|
99
|
+
"<link rel='stylesheet' href='http://gist.github.com/stylesheets/gist/embed.css' />\n"
|
100
|
+
end
|
101
|
+
|
102
|
+
def jquery_link
|
103
|
+
"<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>\n"
|
104
|
+
end
|
105
|
+
|
106
|
+
def jquery_helper
|
107
|
+
<<-EOJQ
|
108
|
+
<script type='text/javascript'>
|
109
|
+
//<![CDATA[
|
110
|
+
$(document).ready(function() {
|
111
|
+
$('.rack-gist').each(function() {
|
112
|
+
var div = this;
|
113
|
+
var url = '/gist.github.com/' + $(this).attr('gist-id');
|
114
|
+
var file = false;
|
115
|
+
if (file = $(this).attr('rack-gist-file')) {
|
116
|
+
url += '/' + file;
|
117
|
+
}
|
118
|
+
$.getScript(url + '.js');
|
119
|
+
});
|
120
|
+
});
|
121
|
+
//]]>
|
122
|
+
</script>
|
123
|
+
EOJQ
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
data/rack-gist.gemspec
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{rack-gist}
|
8
|
+
s.version = "1.0.5"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Daniel Huckstep"]
|
12
|
+
s.date = %q{2010-07-16}
|
13
|
+
s.description = %q{Load gists in the background. KTHXBYE!}
|
14
|
+
s.email = %q{darkhelmet@darkhelmetlive.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"Gemfile",
|
23
|
+
"LICENSE",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"lib/rack/gist.rb",
|
28
|
+
"rack-gist.gemspec",
|
29
|
+
"spec/body-full.html",
|
30
|
+
"spec/body-multiple.html",
|
31
|
+
"spec/body-none.html",
|
32
|
+
"spec/body-partial.html",
|
33
|
+
"spec/full-gist.js",
|
34
|
+
"spec/partial-gist.js",
|
35
|
+
"spec/rack-gist_spec.rb",
|
36
|
+
"spec/spec.opts",
|
37
|
+
"spec/spec_helper.rb"
|
38
|
+
]
|
39
|
+
s.homepage = %q{http://github.com/darkhelmet/rack-gist}
|
40
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
41
|
+
s.require_paths = ["lib"]
|
42
|
+
s.rubygems_version = %q{1.3.7}
|
43
|
+
s.summary = %q{Asynchronous Github Gists. All with rack.}
|
44
|
+
s.test_files = [
|
45
|
+
"spec/rack-gist_spec.rb",
|
46
|
+
"spec/spec_helper.rb"
|
47
|
+
]
|
48
|
+
|
49
|
+
if s.respond_to? :specification_version then
|
50
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
51
|
+
s.specification_version = 3
|
52
|
+
|
53
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
54
|
+
s.add_runtime_dependency(%q<hpricot>, [">= 0.8.2"])
|
55
|
+
s.add_runtime_dependency(%q<rest-client>, [">= 1.6.0"])
|
56
|
+
s.add_development_dependency(%q<fakeweb>, [">= 1.2.8"])
|
57
|
+
s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
|
58
|
+
s.add_development_dependency(%q<yard>, [">= 0.5.8"])
|
59
|
+
s.add_development_dependency(%q<rack>, [">= 1.2.1"])
|
60
|
+
s.add_development_dependency(%q<activesupport>, [">= 2.3.8"])
|
61
|
+
s.add_development_dependency(%q<ruby-debug>, [">= 0"])
|
62
|
+
else
|
63
|
+
s.add_dependency(%q<hpricot>, [">= 0.8.2"])
|
64
|
+
s.add_dependency(%q<rest-client>, [">= 1.6.0"])
|
65
|
+
s.add_dependency(%q<fakeweb>, [">= 1.2.8"])
|
66
|
+
s.add_dependency(%q<rspec>, [">= 1.3.0"])
|
67
|
+
s.add_dependency(%q<yard>, [">= 0.5.8"])
|
68
|
+
s.add_dependency(%q<rack>, [">= 1.2.1"])
|
69
|
+
s.add_dependency(%q<activesupport>, [">= 2.3.8"])
|
70
|
+
s.add_dependency(%q<ruby-debug>, [">= 0"])
|
71
|
+
end
|
72
|
+
else
|
73
|
+
s.add_dependency(%q<hpricot>, [">= 0.8.2"])
|
74
|
+
s.add_dependency(%q<rest-client>, [">= 1.6.0"])
|
75
|
+
s.add_dependency(%q<fakeweb>, [">= 1.2.8"])
|
76
|
+
s.add_dependency(%q<rspec>, [">= 1.3.0"])
|
77
|
+
s.add_dependency(%q<yard>, [">= 0.5.8"])
|
78
|
+
s.add_dependency(%q<rack>, [">= 1.2.1"])
|
79
|
+
s.add_dependency(%q<activesupport>, [">= 2.3.8"])
|
80
|
+
s.add_dependency(%q<ruby-debug>, [">= 0"])
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
data/spec/body-full.html
ADDED
data/spec/body-none.html
ADDED
data/spec/full-gist.js
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
document.write('<link rel="stylesheet" href="http://gist.github.com/stylesheets/gist/embed.css"/>')
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
document.write('<div id=\"gist-348301\" class=\"gist\">\n \n \n \n \n\n <div class=\"gist-file\">\n <div class=\"gist-data gist-syntax\">\n \n \n \n <div class=\"gist-highlight\"><pre><div class=\'line\' id=\'LC1\'>REGISTER com.darkhax.blog.pig.jar;<\/div><div class=\'line\' id=\'LC2\'>DEFINE Parser com.darkhax.blog.pig.LogParser();<\/div><div class=\'line\' id=\'LC3\'><br/><\/div><div class=\'line\' id=\'LC4\'>logs = LOAD \'apache.log.bz2\' USING TextLoader AS (line: chararray);<\/div><div class=\'line\' id=\'LC5\'>log_events = FOREACH logs GENERATE FLATTEN(Parser(line));<\/div><div class=\'line\' id=\'LC6\'><br/><\/div><div class=\'line\' id=\'LC7\'>by_action = GROUP log_events BY action;<\/div><div class=\'line\' id=\'LC8\'>counts = FOREACH by_action GENERATE group, COUNT(log_events);<\/div><div class=\'line\' id=\'LC9\'>STORE counts INTO \'count_summary\';<\/div><\/pre><\/div>\n \n <\/div>\n\n <div class=\"gist-meta\">\n <a href=\"http://gist.github.com/raw/348301/07d20a92ce5e56bde493066a0154f200da37c953/example.pig\" style=\"float:right;\">view raw<\/a>\n <a href=\"http://gist.github.com/348301#file_example.pig\" style=\"float:right;margin-right:10px;color:#666\">example.pig<\/a>\n <a href=\"http://gist.github.com/348301\">This Gist<\/a> brought to you by <a href=\"http://github.com\">GitHub<\/a>.\n <\/div>\n <\/div>\n \n \n\n <div class=\"gist-file\">\n <div class=\"gist-data gist-syntax\">\n \n \n \n <div class=\"gist-highlight\"><pre><div class=\'line\' id=\'LC1\'><span class=\"kn\">package<\/span> <span class=\"n\">com<\/span><span class=\"o\">.<\/span><span class=\"na\">codebaby<\/span><span class=\"o\">.<\/span><span class=\"na\">monitor<\/span><span class=\"o\">.<\/span><span class=\"na\">pig<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC2\'><br/><\/div><div class=\'line\' id=\'LC3\'><span class=\"kn\">import<\/span> <span class=\"nn\">java.io.IOException<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC4\'><br/><\/div><div class=\'line\' id=\'LC5\'><span class=\"kn\">import<\/span> <span class=\"nn\">org.apache.pig.EvalFunc<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC6\'><span class=\"kn\">import<\/span> <span class=\"nn\">org.apache.pig.data.DataType<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC7\'><span class=\"kn\">import<\/span> <span class=\"nn\">org.apache.pig.data.Tuple<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC8\'><span class=\"kn\">import<\/span> <span class=\"nn\">org.apache.pig.data.TupleFactory<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC9\'><span class=\"kn\">import<\/span> <span class=\"nn\">org.apache.pig.impl.logicalLayer.schema.Schema<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC10\'><br/><\/div><div class=\'line\' id=\'LC11\'><span class=\"c1\">// Inherit from EvalFunc<Tuple> to implement a EvalFunc that returns a Tuple<\/span><\/div><div class=\'line\' id=\'LC12\'><span class=\"kd\">public<\/span> <span class=\"kd\">class<\/span> <span class=\"nc\">LogParser<\/span> <span class=\"kd\">extends<\/span> <span class=\"n\">EvalFunc<\/span><span class=\"o\"><<\/span><span class=\"n\">Tuple<\/span><span class=\"o\">><\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC13\'><br/><\/div><div class=\'line\' id=\'LC14\'> <span class=\"c1\">// The main method in question. Gets run for every 'thing' that gets sent to<\/span><\/div><div class=\'line\' id=\'LC15\'> <span class=\"c1\">// this UDF<\/span><\/div><div class=\'line\' id=\'LC16\'> <span class=\"kd\">public<\/span> <span class=\"n\">Tuple<\/span> <span class=\"nf\">exec<\/span><span class=\"o\">(<\/span><span class=\"n\">Tuple<\/span> <span class=\"n\">input<\/span><span class=\"o\">)<\/span> <span class=\"kd\">throws<\/span> <span class=\"n\">IOException<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC17\'> <span class=\"k\">if<\/span> <span class=\"o\">(<\/span><span class=\"kc\">null<\/span> <span class=\"o\">==<\/span> <span class=\"n\">input<\/span> <span class=\"o\">||<\/span> <span class=\"n\">input<\/span><span class=\"o\">.<\/span><span class=\"na\">size<\/span><span class=\"o\">()<\/span> <span class=\"o\">!=<\/span> <span class=\"mi\">1<\/span><span class=\"o\">)<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC18\'> <span class=\"k\">return<\/span> <span class=\"kc\">null<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC19\'> <span class=\"o\">}<\/span><\/div><div class=\'line\' id=\'LC20\'> <\/div><div class=\'line\' id=\'LC21\'> <span class=\"n\">String<\/span> <span class=\"n\">line<\/span> <span class=\"o\">=<\/span> <span class=\"o\">(<\/span><span class=\"n\">String<\/span><span class=\"o\">)<\/span> <span class=\"n\">input<\/span><span class=\"o\">.<\/span><span class=\"na\">get<\/span><span class=\"o\">(<\/span><span class=\"mi\">0<\/span><span class=\"o\">);<\/span><\/div><div class=\'line\' id=\'LC22\'> <span class=\"k\">try<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC23\'> <span class=\"c1\">// In Soviet Russia, factory builds you!<\/span><\/div><div class=\'line\' id=\'LC24\'> <span class=\"n\">TupleFactory<\/span> <span class=\"n\">tf<\/span> <span class=\"o\">=<\/span> <span class=\"n\">TupleFactory<\/span><span class=\"o\">.<\/span><span class=\"na\">getInstance<\/span><span class=\"o\">();<\/span><\/div><div class=\'line\' id=\'LC25\'> <span class=\"n\">Tuple<\/span> <span class=\"n\">t<\/span> <span class=\"o\">=<\/span> <span class=\"n\">tf<\/span><span class=\"o\">.<\/span><span class=\"na\">newTuple<\/span><span class=\"o\">();<\/span><\/div><div class=\'line\' id=\'LC26\'><br/><\/div><div class=\'line\' id=\'LC27\'> <span class=\"n\">t<\/span><span class=\"o\">.<\/span><span class=\"na\">append<\/span><span class=\"o\">(<\/span><span class=\"n\">getHttpMethod<\/span><span class=\"o\">());<\/span><\/div><div class=\'line\' id=\'LC28\'> <span class=\"n\">t<\/span><span class=\"o\">.<\/span><span class=\"na\">append<\/span><span class=\"o\">(<\/span><span class=\"n\">getIP<\/span><span class=\"o\">());<\/span><\/div><div class=\'line\' id=\'LC29\'> <span class=\"n\">t<\/span><span class=\"o\">.<\/span><span class=\"na\">append<\/span><span class=\"o\">(<\/span><span class=\"n\">getDate<\/span><span class=\"o\">());<\/span><\/div><div class=\'line\' id=\'LC30\'><br/><\/div><div class=\'line\' id=\'LC31\'> <span class=\"c1\">// The tuple we are returning now has 3 elements, all strings.<\/span><\/div><div class=\'line\' id=\'LC32\'> <span class=\"c1\">// In order, they are the HTTP method, the IP address, and the date.<\/span><\/div><div class=\'line\' id=\'LC33\'><br/><\/div><div class=\'line\' id=\'LC34\'> <span class=\"k\">return<\/span> <span class=\"n\">t<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC35\'> <span class=\"o\">}<\/span> <span class=\"k\">catch<\/span> <span class=\"o\">(<\/span><span class=\"n\">Exception<\/span> <span class=\"n\">e<\/span><span class=\"o\">)<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC36\'> <span class=\"c1\">// Any problems? Just return null and this one doesn't get<\/span><\/div><div class=\'line\' id=\'LC37\'> <span class=\"c1\">// 'generated' by pig<\/span><\/div><div class=\'line\' id=\'LC38\'> <span class=\"k\">return<\/span> <span class=\"kc\">null<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC39\'> <span class=\"o\">}<\/span><\/div><div class=\'line\' id=\'LC40\'> <span class=\"o\">}<\/span><\/div><div class=\'line\' id=\'LC41\'><br/><\/div><div class=\'line\' id=\'LC42\'> <span class=\"kd\">public<\/span> <span class=\"n\">Schema<\/span> <span class=\"nf\">outputSchema<\/span><span class=\"o\">(<\/span><span class=\"n\">Schema<\/span> <span class=\"n\">input<\/span><span class=\"o\">)<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC43\'> <span class=\"k\">try<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC44\'> <span class=\"n\">Schema<\/span> <span class=\"n\">s<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"n\">Schema<\/span><span class=\"o\">();<\/span><\/div><div class=\'line\' id=\'LC45\'><br/><\/div><div class=\'line\' id=\'LC46\'> <span class=\"n\">s<\/span><span class=\"o\">.<\/span><span class=\"na\">add<\/span><span class=\"o\">(<\/span><span class=\"k\">new<\/span> <span class=\"n\">Schema<\/span><span class=\"o\">.<\/span><span class=\"na\">FieldSchema<\/span><span class=\"o\">(<\/span><span class=\"s\">\"action\"<\/span><span class=\"o\">,<\/span> <span class=\"n\">DataType<\/span><span class=\"o\">.<\/span><span class=\"na\">CHARARRAY<\/span><span class=\"o\">));<\/span><\/div><div class=\'line\' id=\'LC47\'> <span class=\"n\">s<\/span><span class=\"o\">.<\/span><span class=\"na\">add<\/span><span class=\"o\">(<\/span><span class=\"k\">new<\/span> <span class=\"n\">Schema<\/span><span class=\"o\">.<\/span><span class=\"na\">FieldSchema<\/span><span class=\"o\">(<\/span><span class=\"s\">\"ip\"<\/span><span class=\"o\">,<\/span> <span class=\"n\">DataType<\/span><span class=\"o\">.<\/span><span class=\"na\">CHARARRAY<\/span><span class=\"o\">));<\/span><\/div><div class=\'line\' id=\'LC48\'> <span class=\"n\">s<\/span><span class=\"o\">.<\/span><span class=\"na\">add<\/span><span class=\"o\">(<\/span><span class=\"k\">new<\/span> <span class=\"n\">Schema<\/span><span class=\"o\">.<\/span><span class=\"na\">FieldSchema<\/span><span class=\"o\">(<\/span><span class=\"s\">\"date\"<\/span><span class=\"o\">,<\/span> <span class=\"n\">DataType<\/span><span class=\"o\">.<\/span><span class=\"na\">CHARARRAY<\/span><span class=\"o\">));<\/span><\/div><div class=\'line\' id=\'LC49\'><br/><\/div><div class=\'line\' id=\'LC50\'> <span class=\"k\">return<\/span> <span class=\"n\">s<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC51\'> <span class=\"o\">}<\/span> <span class=\"k\">catch<\/span> <span class=\"o\">(<\/span><span class=\"n\">Exception<\/span> <span class=\"n\">e<\/span><span class=\"o\">)<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC52\'> <span class=\"c1\">// Any problems? Just return null...there probably won't be any<\/span><\/div><div class=\'line\' id=\'LC53\'> <span class=\"c1\">// problems though.<\/span><\/div><div class=\'line\' id=\'LC54\'> <span class=\"k\">return<\/span> <span class=\"kc\">null<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC55\'> <span class=\"o\">}<\/span><\/div><div class=\'line\' id=\'LC56\'> <span class=\"o\">}<\/span><\/div><div class=\'line\' id=\'LC57\'><br/><\/div><div class=\'line\' id=\'LC58\'> <span class=\"kd\">public<\/span> <span class=\"n\">String<\/span> <span class=\"nf\">getHttpMethod<\/span><span class=\"o\">()<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC59\'> <span class=\"k\">return<\/span> <span class=\"s\">\"\"<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC60\'> <span class=\"o\">}<\/span><\/div><div class=\'line\' id=\'LC61\'><br/><\/div><div class=\'line\' id=\'LC62\'> <span class=\"kd\">public<\/span> <span class=\"n\">String<\/span> <span class=\"nf\">getIP<\/span><span class=\"o\">()<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC63\'> <span class=\"k\">return<\/span> <span class=\"s\">\"\"<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC64\'> <span class=\"o\">}<\/span><\/div><div class=\'line\' id=\'LC65\'><br/><\/div><div class=\'line\' id=\'LC66\'> <span class=\"kd\">public<\/span> <span class=\"n\">String<\/span> <span class=\"nf\">getDate<\/span><span class=\"o\">()<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC67\'> <span class=\"k\">return<\/span> <span class=\"s\">\"\"<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC68\'> <span class=\"o\">}<\/span><\/div><div class=\'line\' id=\'LC69\'><span class=\"o\">}<\/span><\/div><div class=\'line\' id=\'LC70\'><br/><\/div><\/pre><\/div>\n \n <\/div>\n\n <div class=\"gist-meta\">\n <a href=\"http://gist.github.com/raw/348301/f249e58fe0fc9897e8d575764ecabf153d2da683/LogParser.java\" style=\"float:right;\">view raw<\/a>\n <a href=\"http://gist.github.com/348301#file_log_parser.java\" style=\"float:right;margin-right:10px;color:#666\">LogParser.java<\/a>\n <a href=\"http://gist.github.com/348301\">This Gist<\/a> brought to you by <a href=\"http://github.com\">GitHub<\/a>.\n <\/div>\n <\/div>\n \n \n<\/div>\n')
|
@@ -0,0 +1,7 @@
|
|
1
|
+
document.write('<link rel="stylesheet" href="http://gist.github.com/stylesheets/gist/embed.css"/>')
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
document.write('<div id=\"gist-348301\" class=\"gist\">\n \n \n \n \n\n <div class=\"gist-file\">\n <div class=\"gist-data gist-syntax\">\n \n \n \n <div class=\"gist-highlight\"><pre><div class=\'line\' id=\'LC1\'>REGISTER com.darkhax.blog.pig.jar;<\/div><div class=\'line\' id=\'LC2\'>DEFINE Parser com.darkhax.blog.pig.LogParser();<\/div><div class=\'line\' id=\'LC3\'><br/><\/div><div class=\'line\' id=\'LC4\'>logs = LOAD \'apache.log.bz2\' USING TextLoader AS (line: chararray);<\/div><div class=\'line\' id=\'LC5\'>log_events = FOREACH logs GENERATE FLATTEN(Parser(line));<\/div><div class=\'line\' id=\'LC6\'><br/><\/div><div class=\'line\' id=\'LC7\'>by_action = GROUP log_events BY action;<\/div><div class=\'line\' id=\'LC8\'>counts = FOREACH by_action GENERATE group, COUNT(log_events);<\/div><div class=\'line\' id=\'LC9\'>STORE counts INTO \'count_summary\';<\/div><\/pre><\/div>\n \n <\/div>\n\n <div class=\"gist-meta\">\n <a href=\"http://gist.github.com/raw/348301/07d20a92ce5e56bde493066a0154f200da37c953/example.pig\" style=\"float:right;\">view raw<\/a>\n <a href=\"http://gist.github.com/348301#file_example.pig\" style=\"float:right;margin-right:10px;color:#666\">example.pig<\/a>\n <a href=\"http://gist.github.com/348301\">This Gist<\/a> brought to you by <a href=\"http://github.com\">GitHub<\/a>.\n <\/div>\n <\/div>\n \n \n \n<\/div>\n')
|
@@ -0,0 +1,142 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "Rack::Gist" do
|
4
|
+
before(:all) do
|
5
|
+
@gist_id = 348301
|
6
|
+
@app = lambda do |env|
|
7
|
+
headers = { 'Content-Type' => 'text/html' }
|
8
|
+
body = File.read(File.join(File.dirname(__FILE__), "body-#{env['PATH_INFO'].gsub(/[^\w]/, '')}.html"))
|
9
|
+
[200, headers, body]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def mock_env(path = '/full')
|
14
|
+
Rack::MockRequest.env_for(path)
|
15
|
+
end
|
16
|
+
|
17
|
+
def middleware(options = {})
|
18
|
+
Rack::Gist.new(@app, options)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should pass Rack::Lint' do
|
22
|
+
status, headers, body = Rack::Lint.new(middleware).call(mock_env)
|
23
|
+
status, headers, body = Rack::Lint.new(middleware).call(mock_env('/multiple'))
|
24
|
+
status, headers, body = Rack::Lint.new(middleware).call(mock_env("/gist.github.com/#{@gist_id}/example.pig.js"))
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should rewrite gist embed tags for full gists' do
|
28
|
+
middleware.tap do |a|
|
29
|
+
status, headers, body = a.call(mock_env)
|
30
|
+
status.should == 200
|
31
|
+
headers['Content-Type'].should == 'text/html'
|
32
|
+
body.to_s.should have_html_tag('div').with('id' => "rack-gist-#{@gist_id}", 'gist-id' => @gist_id, 'class' => 'rack-gist')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should rewrite gist embed tags for partial gists' do
|
37
|
+
middleware.tap do |a|
|
38
|
+
status, headers, body = a.call(mock_env('/partial'))
|
39
|
+
status.should == 200
|
40
|
+
headers['Content-Type'].should == 'text/html'
|
41
|
+
body.to_s.should have_html_tag('div').with('id' => "rack-gist-#{@gist_id}", 'gist-id' => @gist_id, 'class' => 'rack-gist', 'rack-gist-file' => 'example.pig')
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should not include the github css file if no gists are present' do
|
46
|
+
middleware.tap do |a|
|
47
|
+
status, headers, body = a.call(mock_env('/none'))
|
48
|
+
status.should == 200
|
49
|
+
headers['Content-Type'].should == 'text/html'
|
50
|
+
body.to_s.should_not have_html_tag('link').with('rel' => 'stylesheet', 'href' => 'http://gist.github.com/stylesheets/gist/embed.css')
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should include the github css file once' do
|
55
|
+
middleware.tap do |a|
|
56
|
+
status, headers, body = a.call(mock_env('/multiple'))
|
57
|
+
status.should == 200
|
58
|
+
headers['Content-Type'].should == 'text/html'
|
59
|
+
body.to_s.should have_html_tag('link').with('rel' => 'stylesheet', 'href' => 'http://gist.github.com/stylesheets/gist/embed.css')
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'should include jquery by default' do
|
64
|
+
middleware.tap do |a|
|
65
|
+
status, headers, body = a.call(mock_env)
|
66
|
+
status.should == 200
|
67
|
+
headers['Content-Type'].should == 'text/html'
|
68
|
+
body.to_s.should have_html_tag('script[@src*="jquery"]')
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'should not include jquery if the option is passed to disable it' do
|
73
|
+
middleware(:jquery => false).tap do |a|
|
74
|
+
status, headers, body = a.call(mock_env)
|
75
|
+
status.should == 200
|
76
|
+
headers['Content-Type'].should == 'text/html'
|
77
|
+
body.to_s.should_not have_html_tag('script[@src*="jquery"]')
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'should include required jquery helper' do
|
82
|
+
middleware(:jquery => false).tap do |a|
|
83
|
+
status, headers, body = a.call(mock_env)
|
84
|
+
status.should == 200
|
85
|
+
headers['Content-Type'].should == 'text/html'
|
86
|
+
body.to_s.should have_html_tag('script').containing('CDATA')
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
it "shouldn't include the jquery helper if no gist is present" do
|
91
|
+
middleware.tap do |a|
|
92
|
+
status, headers, body = a.call(mock_env)
|
93
|
+
status.should == 200
|
94
|
+
headers['Content-Type'].should == 'text/html'
|
95
|
+
body.to_s.should_not have_html_tag('script')
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'should update Content-Length' do
|
100
|
+
middleware.tap do |a|
|
101
|
+
status, headers, body = a.call(mock_env)
|
102
|
+
status.should == 200
|
103
|
+
headers['Content-Type'].should == 'text/html'
|
104
|
+
headers['Content-Length'].should == '913'
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'should proxy/serve single gists' do
|
109
|
+
middleware.tap do |a|
|
110
|
+
status, headers, body = a.call(mock_env("/gist.github.com/#{@gist_id}/example.pig.js"))
|
111
|
+
status.should == 200
|
112
|
+
headers['Content-Type'].should == 'application/javascript'
|
113
|
+
body.to_s.should == %q{$('#rack-gist-%s[rack-gist-file="example.pig"]').replaceWith('<div id=\"gist-348301\" class=\"gist\">\n \n \n \n \n\n <div class=\"gist-file\">\n <div class=\"gist-data gist-syntax\">\n \n \n \n <div class=\"gist-highlight\"><pre><div class=\'line\' id=\'LC1\'>REGISTER com.darkhax.blog.pig.jar;<\/div><div class=\'line\' id=\'LC2\'>DEFINE Parser com.darkhax.blog.pig.LogParser();<\/div><div class=\'line\' id=\'LC3\'><br/><\/div><div class=\'line\' id=\'LC4\'>logs = LOAD \'apache.log.bz2\' USING TextLoader AS (line: chararray);<\/div><div class=\'line\' id=\'LC5\'>log_events = FOREACH logs GENERATE FLATTEN(Parser(line));<\/div><div class=\'line\' id=\'LC6\'><br/><\/div><div class=\'line\' id=\'LC7\'>by_action = GROUP log_events BY action;<\/div><div class=\'line\' id=\'LC8\'>counts = FOREACH by_action GENERATE group, COUNT(log_events);<\/div><div class=\'line\' id=\'LC9\'>STORE counts INTO \'count_summary\';<\/div><\/pre><\/div>\n \n <\/div>\n\n <div class=\"gist-meta\">\n <a href=\"http://gist.github.com/raw/348301/07d20a92ce5e56bde493066a0154f200da37c953/example.pig\" style=\"float:right;\">view raw<\/a>\n <a href=\"http://gist.github.com/348301#file_example.pig\" style=\"float:right;margin-right:10px;color:#666\">example.pig<\/a>\n <a href=\"http://gist.github.com/348301\">This Gist<\/a> brought to you by <a href=\"http://github.com\">GitHub<\/a>.\n <\/div>\n <\/div>\n \n \n \n<\/div>\n')} % @gist_id
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'should proxy/server multiple gists' do
|
118
|
+
middleware.tap do |a|
|
119
|
+
status, headers, body = a.call(mock_env("/gist.github.com/#{@gist_id}.js"))
|
120
|
+
status.should == 200
|
121
|
+
headers['Content-Type'].should == 'application/javascript'
|
122
|
+
body.to_s.should == %q{$('#rack-gist-%s').replaceWith('<div id=\"gist-348301\" class=\"gist\">\n \n \n \n \n\n <div class=\"gist-file\">\n <div class=\"gist-data gist-syntax\">\n \n \n \n <div class=\"gist-highlight\"><pre><div class=\'line\' id=\'LC1\'>REGISTER com.darkhax.blog.pig.jar;<\/div><div class=\'line\' id=\'LC2\'>DEFINE Parser com.darkhax.blog.pig.LogParser();<\/div><div class=\'line\' id=\'LC3\'><br/><\/div><div class=\'line\' id=\'LC4\'>logs = LOAD \'apache.log.bz2\' USING TextLoader AS (line: chararray);<\/div><div class=\'line\' id=\'LC5\'>log_events = FOREACH logs GENERATE FLATTEN(Parser(line));<\/div><div class=\'line\' id=\'LC6\'><br/><\/div><div class=\'line\' id=\'LC7\'>by_action = GROUP log_events BY action;<\/div><div class=\'line\' id=\'LC8\'>counts = FOREACH by_action GENERATE group, COUNT(log_events);<\/div><div class=\'line\' id=\'LC9\'>STORE counts INTO \'count_summary\';<\/div><\/pre><\/div>\n \n <\/div>\n\n <div class=\"gist-meta\">\n <a href=\"http://gist.github.com/raw/348301/07d20a92ce5e56bde493066a0154f200da37c953/example.pig\" style=\"float:right;\">view raw<\/a>\n <a href=\"http://gist.github.com/348301#file_example.pig\" style=\"float:right;margin-right:10px;color:#666\">example.pig<\/a>\n <a href=\"http://gist.github.com/348301\">This Gist<\/a> brought to you by <a href=\"http://github.com\">GitHub<\/a>.\n <\/div>\n <\/div>\n \n \n\n <div class=\"gist-file\">\n <div class=\"gist-data gist-syntax\">\n \n \n \n <div class=\"gist-highlight\"><pre><div class=\'line\' id=\'LC1\'><span class=\"kn\">package<\/span> <span class=\"n\">com<\/span><span class=\"o\">.<\/span><span class=\"na\">codebaby<\/span><span class=\"o\">.<\/span><span class=\"na\">monitor<\/span><span class=\"o\">.<\/span><span class=\"na\">pig<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC2\'><br/><\/div><div class=\'line\' id=\'LC3\'><span class=\"kn\">import<\/span> <span class=\"nn\">java.io.IOException<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC4\'><br/><\/div><div class=\'line\' id=\'LC5\'><span class=\"kn\">import<\/span> <span class=\"nn\">org.apache.pig.EvalFunc<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC6\'><span class=\"kn\">import<\/span> <span class=\"nn\">org.apache.pig.data.DataType<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC7\'><span class=\"kn\">import<\/span> <span class=\"nn\">org.apache.pig.data.Tuple<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC8\'><span class=\"kn\">import<\/span> <span class=\"nn\">org.apache.pig.data.TupleFactory<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC9\'><span class=\"kn\">import<\/span> <span class=\"nn\">org.apache.pig.impl.logicalLayer.schema.Schema<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC10\'><br/><\/div><div class=\'line\' id=\'LC11\'><span class=\"c1\">// Inherit from EvalFunc<Tuple> to implement a EvalFunc that returns a Tuple<\/span><\/div><div class=\'line\' id=\'LC12\'><span class=\"kd\">public<\/span> <span class=\"kd\">class<\/span> <span class=\"nc\">LogParser<\/span> <span class=\"kd\">extends<\/span> <span class=\"n\">EvalFunc<\/span><span class=\"o\"><<\/span><span class=\"n\">Tuple<\/span><span class=\"o\">><\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC13\'><br/><\/div><div class=\'line\' id=\'LC14\'> <span class=\"c1\">// The main method in question. Gets run for every 'thing' that gets sent to<\/span><\/div><div class=\'line\' id=\'LC15\'> <span class=\"c1\">// this UDF<\/span><\/div><div class=\'line\' id=\'LC16\'> <span class=\"kd\">public<\/span> <span class=\"n\">Tuple<\/span> <span class=\"nf\">exec<\/span><span class=\"o\">(<\/span><span class=\"n\">Tuple<\/span> <span class=\"n\">input<\/span><span class=\"o\">)<\/span> <span class=\"kd\">throws<\/span> <span class=\"n\">IOException<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC17\'> <span class=\"k\">if<\/span> <span class=\"o\">(<\/span><span class=\"kc\">null<\/span> <span class=\"o\">==<\/span> <span class=\"n\">input<\/span> <span class=\"o\">||<\/span> <span class=\"n\">input<\/span><span class=\"o\">.<\/span><span class=\"na\">size<\/span><span class=\"o\">()<\/span> <span class=\"o\">!=<\/span> <span class=\"mi\">1<\/span><span class=\"o\">)<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC18\'> <span class=\"k\">return<\/span> <span class=\"kc\">null<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC19\'> <span class=\"o\">}<\/span><\/div><div class=\'line\' id=\'LC20\'> <\/div><div class=\'line\' id=\'LC21\'> <span class=\"n\">String<\/span> <span class=\"n\">line<\/span> <span class=\"o\">=<\/span> <span class=\"o\">(<\/span><span class=\"n\">String<\/span><span class=\"o\">)<\/span> <span class=\"n\">input<\/span><span class=\"o\">.<\/span><span class=\"na\">get<\/span><span class=\"o\">(<\/span><span class=\"mi\">0<\/span><span class=\"o\">);<\/span><\/div><div class=\'line\' id=\'LC22\'> <span class=\"k\">try<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC23\'> <span class=\"c1\">// In Soviet Russia, factory builds you!<\/span><\/div><div class=\'line\' id=\'LC24\'> <span class=\"n\">TupleFactory<\/span> <span class=\"n\">tf<\/span> <span class=\"o\">=<\/span> <span class=\"n\">TupleFactory<\/span><span class=\"o\">.<\/span><span class=\"na\">getInstance<\/span><span class=\"o\">();<\/span><\/div><div class=\'line\' id=\'LC25\'> <span class=\"n\">Tuple<\/span> <span class=\"n\">t<\/span> <span class=\"o\">=<\/span> <span class=\"n\">tf<\/span><span class=\"o\">.<\/span><span class=\"na\">newTuple<\/span><span class=\"o\">();<\/span><\/div><div class=\'line\' id=\'LC26\'><br/><\/div><div class=\'line\' id=\'LC27\'> <span class=\"n\">t<\/span><span class=\"o\">.<\/span><span class=\"na\">append<\/span><span class=\"o\">(<\/span><span class=\"n\">getHttpMethod<\/span><span class=\"o\">());<\/span><\/div><div class=\'line\' id=\'LC28\'> <span class=\"n\">t<\/span><span class=\"o\">.<\/span><span class=\"na\">append<\/span><span class=\"o\">(<\/span><span class=\"n\">getIP<\/span><span class=\"o\">());<\/span><\/div><div class=\'line\' id=\'LC29\'> <span class=\"n\">t<\/span><span class=\"o\">.<\/span><span class=\"na\">append<\/span><span class=\"o\">(<\/span><span class=\"n\">getDate<\/span><span class=\"o\">());<\/span><\/div><div class=\'line\' id=\'LC30\'><br/><\/div><div class=\'line\' id=\'LC31\'> <span class=\"c1\">// The tuple we are returning now has 3 elements, all strings.<\/span><\/div><div class=\'line\' id=\'LC32\'> <span class=\"c1\">// In order, they are the HTTP method, the IP address, and the date.<\/span><\/div><div class=\'line\' id=\'LC33\'><br/><\/div><div class=\'line\' id=\'LC34\'> <span class=\"k\">return<\/span> <span class=\"n\">t<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC35\'> <span class=\"o\">}<\/span> <span class=\"k\">catch<\/span> <span class=\"o\">(<\/span><span class=\"n\">Exception<\/span> <span class=\"n\">e<\/span><span class=\"o\">)<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC36\'> <span class=\"c1\">// Any problems? Just return null and this one doesn't get<\/span><\/div><div class=\'line\' id=\'LC37\'> <span class=\"c1\">// 'generated' by pig<\/span><\/div><div class=\'line\' id=\'LC38\'> <span class=\"k\">return<\/span> <span class=\"kc\">null<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC39\'> <span class=\"o\">}<\/span><\/div><div class=\'line\' id=\'LC40\'> <span class=\"o\">}<\/span><\/div><div class=\'line\' id=\'LC41\'><br/><\/div><div class=\'line\' id=\'LC42\'> <span class=\"kd\">public<\/span> <span class=\"n\">Schema<\/span> <span class=\"nf\">outputSchema<\/span><span class=\"o\">(<\/span><span class=\"n\">Schema<\/span> <span class=\"n\">input<\/span><span class=\"o\">)<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC43\'> <span class=\"k\">try<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC44\'> <span class=\"n\">Schema<\/span> <span class=\"n\">s<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"n\">Schema<\/span><span class=\"o\">();<\/span><\/div><div class=\'line\' id=\'LC45\'><br/><\/div><div class=\'line\' id=\'LC46\'> <span class=\"n\">s<\/span><span class=\"o\">.<\/span><span class=\"na\">add<\/span><span class=\"o\">(<\/span><span class=\"k\">new<\/span> <span class=\"n\">Schema<\/span><span class=\"o\">.<\/span><span class=\"na\">FieldSchema<\/span><span class=\"o\">(<\/span><span class=\"s\">\"action\"<\/span><span class=\"o\">,<\/span> <span class=\"n\">DataType<\/span><span class=\"o\">.<\/span><span class=\"na\">CHARARRAY<\/span><span class=\"o\">));<\/span><\/div><div class=\'line\' id=\'LC47\'> <span class=\"n\">s<\/span><span class=\"o\">.<\/span><span class=\"na\">add<\/span><span class=\"o\">(<\/span><span class=\"k\">new<\/span> <span class=\"n\">Schema<\/span><span class=\"o\">.<\/span><span class=\"na\">FieldSchema<\/span><span class=\"o\">(<\/span><span class=\"s\">\"ip\"<\/span><span class=\"o\">,<\/span> <span class=\"n\">DataType<\/span><span class=\"o\">.<\/span><span class=\"na\">CHARARRAY<\/span><span class=\"o\">));<\/span><\/div><div class=\'line\' id=\'LC48\'> <span class=\"n\">s<\/span><span class=\"o\">.<\/span><span class=\"na\">add<\/span><span class=\"o\">(<\/span><span class=\"k\">new<\/span> <span class=\"n\">Schema<\/span><span class=\"o\">.<\/span><span class=\"na\">FieldSchema<\/span><span class=\"o\">(<\/span><span class=\"s\">\"date\"<\/span><span class=\"o\">,<\/span> <span class=\"n\">DataType<\/span><span class=\"o\">.<\/span><span class=\"na\">CHARARRAY<\/span><span class=\"o\">));<\/span><\/div><div class=\'line\' id=\'LC49\'><br/><\/div><div class=\'line\' id=\'LC50\'> <span class=\"k\">return<\/span> <span class=\"n\">s<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC51\'> <span class=\"o\">}<\/span> <span class=\"k\">catch<\/span> <span class=\"o\">(<\/span><span class=\"n\">Exception<\/span> <span class=\"n\">e<\/span><span class=\"o\">)<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC52\'> <span class=\"c1\">// Any problems? Just return null...there probably won't be any<\/span><\/div><div class=\'line\' id=\'LC53\'> <span class=\"c1\">// problems though.<\/span><\/div><div class=\'line\' id=\'LC54\'> <span class=\"k\">return<\/span> <span class=\"kc\">null<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC55\'> <span class=\"o\">}<\/span><\/div><div class=\'line\' id=\'LC56\'> <span class=\"o\">}<\/span><\/div><div class=\'line\' id=\'LC57\'><br/><\/div><div class=\'line\' id=\'LC58\'> <span class=\"kd\">public<\/span> <span class=\"n\">String<\/span> <span class=\"nf\">getHttpMethod<\/span><span class=\"o\">()<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC59\'> <span class=\"k\">return<\/span> <span class=\"s\">\"\"<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC60\'> <span class=\"o\">}<\/span><\/div><div class=\'line\' id=\'LC61\'><br/><\/div><div class=\'line\' id=\'LC62\'> <span class=\"kd\">public<\/span> <span class=\"n\">String<\/span> <span class=\"nf\">getIP<\/span><span class=\"o\">()<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC63\'> <span class=\"k\">return<\/span> <span class=\"s\">\"\"<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC64\'> <span class=\"o\">}<\/span><\/div><div class=\'line\' id=\'LC65\'><br/><\/div><div class=\'line\' id=\'LC66\'> <span class=\"kd\">public<\/span> <span class=\"n\">String<\/span> <span class=\"nf\">getDate<\/span><span class=\"o\">()<\/span> <span class=\"o\">{<\/span><\/div><div class=\'line\' id=\'LC67\'> <span class=\"k\">return<\/span> <span class=\"s\">\"\"<\/span><span class=\"o\">;<\/span><\/div><div class=\'line\' id=\'LC68\'> <span class=\"o\">}<\/span><\/div><div class=\'line\' id=\'LC69\'><span class=\"o\">}<\/span><\/div><div class=\'line\' id=\'LC70\'><br/><\/div><\/pre><\/div>\n \n <\/div>\n\n <div class=\"gist-meta\">\n <a href=\"http://gist.github.com/raw/348301/f249e58fe0fc9897e8d575764ecabf153d2da683/LogParser.java\" style=\"float:right;\">view raw<\/a>\n <a href=\"http://gist.github.com/348301#file_log_parser.java\" style=\"float:right;margin-right:10px;color:#666\">LogParser.java<\/a>\n <a href=\"http://gist.github.com/348301\">This Gist<\/a> brought to you by <a href=\"http://github.com\">GitHub<\/a>.\n <\/div>\n <\/div>\n \n \n<\/div>\n')} % @gist_id
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'should cache things if given a cache' do
|
127
|
+
cache = ActiveSupport::Cache::MemoryStore.new
|
128
|
+
middleware(:cache => cache).tap do |a|
|
129
|
+
status, headers, body = a.call(mock_env("/gist.github.com/#{@gist_id}/example.pig.js"))
|
130
|
+
status.should == 200
|
131
|
+
headers['Content-Type'].should == 'application/javascript'
|
132
|
+
|
133
|
+
RestClient.should_not_receive(:get)
|
134
|
+
cache.should_receive(:fetch).once.with("rack-gist:#{@gist_id}:example.pig", :expires_in => 3600).and_return(body) # !.hour
|
135
|
+
|
136
|
+
status, headers, body2 = a.call(mock_env("/gist.github.com/#{@gist_id}/example.pig.js"))
|
137
|
+
status.should == 200
|
138
|
+
headers['Content-Type'].should == 'application/javascript'
|
139
|
+
body.should == body2
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'rack/gist'
|
4
|
+
|
5
|
+
require 'rack/mock'
|
6
|
+
require 'rack/lint'
|
7
|
+
|
8
|
+
require 'active_support/core_ext/benchmark'
|
9
|
+
require 'active_support/core_ext/array'
|
10
|
+
require 'active_support/core_ext/class/attribute_accessors'
|
11
|
+
require 'active_support/cache'
|
12
|
+
require 'active_support/cache/memory_store'
|
13
|
+
|
14
|
+
require 'spec'
|
15
|
+
require 'spec/autorun'
|
16
|
+
|
17
|
+
Spec::Runner.configure do |config|
|
18
|
+
config.include(Spec::Matchers)
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'fakeweb'
|
22
|
+
|
23
|
+
# Disallow web access
|
24
|
+
FakeWeb.allow_net_connect = false
|
25
|
+
FakeWeb.register_uri(:get, 'http://gist.github.com/348301.js', :body => File.read(File.join(File.dirname(__FILE__), 'full-gist.js')), :content_type => 'text/javascript; charset=utf-8')
|
26
|
+
FakeWeb.register_uri(:get, 'http://gist.github.com/348301.js?file=example.pig', :body => File.read(File.join(File.dirname(__FILE__), 'partial-gist.js')), :content_type => 'text/javascript; charset=utf-8')
|
27
|
+
|
28
|
+
require 'hpricot'
|
29
|
+
|
30
|
+
Spec::Matchers.define :have_html_tag do |tag|
|
31
|
+
chain :count do |count|
|
32
|
+
@count = count
|
33
|
+
end
|
34
|
+
|
35
|
+
chain :with do |contents|
|
36
|
+
@contents = contents
|
37
|
+
end
|
38
|
+
|
39
|
+
chain :containing do |inner|
|
40
|
+
@inner = inner
|
41
|
+
end
|
42
|
+
|
43
|
+
match do |doc|
|
44
|
+
@count ||= 1
|
45
|
+
@contents ||= {}
|
46
|
+
@inner ||= //
|
47
|
+
doc = Hpricot(doc)
|
48
|
+
doc.search(tag).tap do |results|
|
49
|
+
results.size.should == @count
|
50
|
+
results.each do |node|
|
51
|
+
node.inner_html.match(@inner).should_not be_nil
|
52
|
+
@contents.each_pair do |attribute, expected|
|
53
|
+
node[attribute].should == expected.to_s
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
failure_message_for_should do |doc|
|
60
|
+
msg = "expected #{doc} to have HTML tag #{tag}"
|
61
|
+
if @count > 1
|
62
|
+
msg << " #{@count} times"
|
63
|
+
end
|
64
|
+
|
65
|
+
extra = []
|
66
|
+
|
67
|
+
@contents.each_pair do |attribute, value|
|
68
|
+
extra << "#{attribute} = #{value}"
|
69
|
+
end
|
70
|
+
|
71
|
+
msg << ' ' << extra.join(', ')
|
72
|
+
msg
|
73
|
+
end
|
74
|
+
end
|
metadata
ADDED
@@ -0,0 +1,211 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-gist
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 5
|
10
|
+
version: 1.0.5
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Daniel Huckstep
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-07-16 00:00:00 -06:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
prerelease: false
|
23
|
+
type: :runtime
|
24
|
+
name: hpricot
|
25
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 59
|
31
|
+
segments:
|
32
|
+
- 0
|
33
|
+
- 8
|
34
|
+
- 2
|
35
|
+
version: 0.8.2
|
36
|
+
requirement: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
prerelease: false
|
39
|
+
type: :runtime
|
40
|
+
name: rest-client
|
41
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 15
|
47
|
+
segments:
|
48
|
+
- 1
|
49
|
+
- 6
|
50
|
+
- 0
|
51
|
+
version: 1.6.0
|
52
|
+
requirement: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
prerelease: false
|
55
|
+
type: :development
|
56
|
+
name: fakeweb
|
57
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 15
|
63
|
+
segments:
|
64
|
+
- 1
|
65
|
+
- 2
|
66
|
+
- 8
|
67
|
+
version: 1.2.8
|
68
|
+
requirement: *id003
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
prerelease: false
|
71
|
+
type: :development
|
72
|
+
name: rspec
|
73
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
hash: 27
|
79
|
+
segments:
|
80
|
+
- 1
|
81
|
+
- 3
|
82
|
+
- 0
|
83
|
+
version: 1.3.0
|
84
|
+
requirement: *id004
|
85
|
+
- !ruby/object:Gem::Dependency
|
86
|
+
prerelease: false
|
87
|
+
type: :development
|
88
|
+
name: yard
|
89
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
hash: 27
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
- 5
|
98
|
+
- 8
|
99
|
+
version: 0.5.8
|
100
|
+
requirement: *id005
|
101
|
+
- !ruby/object:Gem::Dependency
|
102
|
+
prerelease: false
|
103
|
+
type: :development
|
104
|
+
name: rack
|
105
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
hash: 29
|
111
|
+
segments:
|
112
|
+
- 1
|
113
|
+
- 2
|
114
|
+
- 1
|
115
|
+
version: 1.2.1
|
116
|
+
requirement: *id006
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
prerelease: false
|
119
|
+
type: :development
|
120
|
+
name: activesupport
|
121
|
+
version_requirements: &id007 !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
hash: 19
|
127
|
+
segments:
|
128
|
+
- 2
|
129
|
+
- 3
|
130
|
+
- 8
|
131
|
+
version: 2.3.8
|
132
|
+
requirement: *id007
|
133
|
+
- !ruby/object:Gem::Dependency
|
134
|
+
prerelease: false
|
135
|
+
type: :development
|
136
|
+
name: ruby-debug
|
137
|
+
version_requirements: &id008 !ruby/object:Gem::Requirement
|
138
|
+
none: false
|
139
|
+
requirements:
|
140
|
+
- - ">="
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
hash: 3
|
143
|
+
segments:
|
144
|
+
- 0
|
145
|
+
version: "0"
|
146
|
+
requirement: *id008
|
147
|
+
description: Load gists in the background. KTHXBYE!
|
148
|
+
email: darkhelmet@darkhelmetlive.com
|
149
|
+
executables: []
|
150
|
+
|
151
|
+
extensions: []
|
152
|
+
|
153
|
+
extra_rdoc_files:
|
154
|
+
- LICENSE
|
155
|
+
- README.rdoc
|
156
|
+
files:
|
157
|
+
- .document
|
158
|
+
- .gitignore
|
159
|
+
- Gemfile
|
160
|
+
- LICENSE
|
161
|
+
- README.rdoc
|
162
|
+
- Rakefile
|
163
|
+
- VERSION
|
164
|
+
- lib/rack/gist.rb
|
165
|
+
- rack-gist.gemspec
|
166
|
+
- spec/body-full.html
|
167
|
+
- spec/body-multiple.html
|
168
|
+
- spec/body-none.html
|
169
|
+
- spec/body-partial.html
|
170
|
+
- spec/full-gist.js
|
171
|
+
- spec/partial-gist.js
|
172
|
+
- spec/rack-gist_spec.rb
|
173
|
+
- spec/spec.opts
|
174
|
+
- spec/spec_helper.rb
|
175
|
+
has_rdoc: true
|
176
|
+
homepage: http://github.com/darkhelmet/rack-gist
|
177
|
+
licenses: []
|
178
|
+
|
179
|
+
post_install_message:
|
180
|
+
rdoc_options:
|
181
|
+
- --charset=UTF-8
|
182
|
+
require_paths:
|
183
|
+
- lib
|
184
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
185
|
+
none: false
|
186
|
+
requirements:
|
187
|
+
- - ">="
|
188
|
+
- !ruby/object:Gem::Version
|
189
|
+
hash: 3
|
190
|
+
segments:
|
191
|
+
- 0
|
192
|
+
version: "0"
|
193
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
194
|
+
none: false
|
195
|
+
requirements:
|
196
|
+
- - ">="
|
197
|
+
- !ruby/object:Gem::Version
|
198
|
+
hash: 3
|
199
|
+
segments:
|
200
|
+
- 0
|
201
|
+
version: "0"
|
202
|
+
requirements: []
|
203
|
+
|
204
|
+
rubyforge_project:
|
205
|
+
rubygems_version: 1.3.7
|
206
|
+
signing_key:
|
207
|
+
specification_version: 3
|
208
|
+
summary: Asynchronous Github Gists. All with rack.
|
209
|
+
test_files:
|
210
|
+
- spec/rack-gist_spec.rb
|
211
|
+
- spec/spec_helper.rb
|