require_gist 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,25 @@
1
+ require "fileutils"
2
+ require "open-uri"
3
+
4
+ def require_gist(gist_url, gist_sha1 = nil)
5
+ if gist_url =~ /^\d/
6
+ gist_url = "https://gist.github.com/raw/#{ gist_url }"
7
+ end
8
+ gist_id, gist_rev, file = gist_url.match(%r{ https://gist.github.com/raw/(.+?)/(.+?)/(.+) }x).captures
9
+ dir = FileUtils.mkdir_p(File.join(ENV["HOME"], ".gist"))
10
+ feature_name = [ gist_id, gist_rev, file ] * "-"
11
+ feature_path = File.join(dir, feature_name)
12
+
13
+ unless File.exist?(feature_path)
14
+ File.open(feature_path, "w") do |f|
15
+ f.write open(gist_url).read
16
+ end
17
+ end
18
+
19
+ if gist_sha1
20
+ require "digest/sha1"
21
+ raise LoadError, "tampered locally cached copy of gist detected" if Digest::SHA1.hexdigest(IO.read(feature_path)) != gist_sha1
22
+ end
23
+
24
+ require feature_path
25
+ end
@@ -0,0 +1,19 @@
1
+ %w(../
2
+ ../../lib).each { |load_path| $LOAD_PATH.unshift(File.expand_path(load_path, __FILE__)) }
3
+
4
+
5
+ require "require_gist"
6
+
7
+ describe "RequireGist" do
8
+ it "requires gists" do
9
+ require_gist "https://gist.github.com/raw/372689/df2b91fc88db48e3fa58a1cfffdbf214651a1bbf/require_gist_test_gist.rb"
10
+ ::RequireGistTestGist.should == 4
11
+ end
12
+
13
+ it "requires gists and checks SHA1" do
14
+ require_gist "372689/df2b91fc88db48e3fa58a1cfffdbf214651a1bbf/require_gist_test_gist.rb", "cddb5f13eb08f46a6e0b1c9fdccb80bc237f3c99" # http://gist.github.com/372689
15
+ ::RequireGistTestGist.should == 4
16
+ end
17
+ end
18
+
19
+
@@ -0,0 +1 @@
1
+ RequireGistTestGist = 4
@@ -0,0 +1,219 @@
1
+ // ==UserScript==
2
+ // @name require_gist
3
+ // @namespace http://userscripts.org/users/151038
4
+ // @include http://gist.github.com/*
5
+ // @include https://gist.github.com/*
6
+ // @require http://userscripts.org/scripts/source/68059.user.js
7
+ // ==/UserScript==
8
+
9
+ (function() {
10
+ var main = function() {
11
+ console.log("require_gist");
12
+
13
+ var jQuery = unsafeWindow.jQuery;
14
+
15
+ if (/http:\/\/gist.github.com\/\d+/.test(window.location.href)) {
16
+ var raw_link = jQuery("a[href^=/raw]:first");
17
+ console.log("found link");
18
+ if (raw_link.length) {
19
+ console.log("requesting link");
20
+ jQuery.get(raw_link.attr("href"), function(data) {
21
+ var require_gist_code = "require_gist \"" + raw_link.attr("href").replace("/raw/", "") + "\", \"" + SHA1(data) + "\" # " + window.location.href;
22
+ var label = "<td class=\"label\">require_gist:</td>";
23
+ var code = "<td>" +
24
+ "<a class=\"gist-embed-link\" href=\"#\" style=\"display: inline;\">show code</a>" +
25
+ "<input type=\"text\" style=\"display: none;\" size=25 class=\"gist-embed-box\">" +
26
+ "</td>";
27
+ var tr = jQuery("<tr>" + label + code + "</tr>");
28
+ tr.find(".gist-embed-link").click(function(event) {
29
+ event.preventDefault();
30
+ jQuery(this).hide();
31
+ var input = tr.find("td:last input").val(require_gist_code).show();
32
+ input.get(0).select();
33
+ });
34
+
35
+ jQuery("#repos .meta table").append(tr);
36
+
37
+ console.log();
38
+ });
39
+ }
40
+ }
41
+ }
42
+
43
+ /**
44
+ *
45
+ * Secure Hash Algorithm (SHA1)
46
+ * http://www.webtoolkit.info/
47
+ *
48
+ **/
49
+
50
+ function SHA1 (msg) {
51
+
52
+ function rotate_left(n,s) {
53
+ var t4 = ( n<<s ) | (n>>>(32-s));
54
+ return t4;
55
+ };
56
+
57
+ function lsb_hex(val) {
58
+ var str="";
59
+ var i;
60
+ var vh;
61
+ var vl;
62
+
63
+ for( i=0; i<=6; i+=2 ) {
64
+ vh = (val>>>(i*4+4))&0x0f;
65
+ vl = (val>>>(i*4))&0x0f;
66
+ str += vh.toString(16) + vl.toString(16);
67
+ }
68
+ return str;
69
+ };
70
+
71
+ function cvt_hex(val) {
72
+ var str="";
73
+ var i;
74
+ var v;
75
+
76
+ for( i=7; i>=0; i-- ) {
77
+ v = (val>>>(i*4))&0x0f;
78
+ str += v.toString(16);
79
+ }
80
+ return str;
81
+ };
82
+
83
+
84
+ function Utf8Encode(string) {
85
+ string = string.replace(/\r\n/g,"\n");
86
+ var utftext = "";
87
+
88
+ for (var n = 0; n < string.length; n++) {
89
+
90
+ var c = string.charCodeAt(n);
91
+
92
+ if (c < 128) {
93
+ utftext += String.fromCharCode(c);
94
+ }
95
+ else if((c > 127) && (c < 2048)) {
96
+ utftext += String.fromCharCode((c >> 6) | 192);
97
+ utftext += String.fromCharCode((c & 63) | 128);
98
+ }
99
+ else {
100
+ utftext += String.fromCharCode((c >> 12) | 224);
101
+ utftext += String.fromCharCode(((c >> 6) & 63) | 128);
102
+ utftext += String.fromCharCode((c & 63) | 128);
103
+ }
104
+
105
+ }
106
+
107
+ return utftext;
108
+ };
109
+
110
+ var blockstart;
111
+ var i, j;
112
+ var W = new Array(80);
113
+ var H0 = 0x67452301;
114
+ var H1 = 0xEFCDAB89;
115
+ var H2 = 0x98BADCFE;
116
+ var H3 = 0x10325476;
117
+ var H4 = 0xC3D2E1F0;
118
+ var A, B, C, D, E;
119
+ var temp;
120
+
121
+ msg = Utf8Encode(msg);
122
+
123
+ var msg_len = msg.length;
124
+
125
+ var word_array = new Array();
126
+ for( i=0; i<msg_len-3; i+=4 ) {
127
+ j = msg.charCodeAt(i)<<24 | msg.charCodeAt(i+1)<<16 |
128
+ msg.charCodeAt(i+2)<<8 | msg.charCodeAt(i+3);
129
+ word_array.push( j );
130
+ }
131
+
132
+ switch( msg_len % 4 ) {
133
+ case 0:
134
+ i = 0x080000000;
135
+ break;
136
+ case 1:
137
+ i = msg.charCodeAt(msg_len-1)<<24 | 0x0800000;
138
+ break;
139
+
140
+ case 2:
141
+ i = msg.charCodeAt(msg_len-2)<<24 | msg.charCodeAt(msg_len-1)<<16 | 0x08000;
142
+ break;
143
+
144
+ case 3:
145
+ i = msg.charCodeAt(msg_len-3)<<24 | msg.charCodeAt(msg_len-2)<<16 | msg.charCodeAt(msg_len-1)<<8 | 0x80;
146
+ break;
147
+ }
148
+
149
+ word_array.push( i );
150
+
151
+ while( (word_array.length % 16) != 14 ) word_array.push( 0 );
152
+
153
+ word_array.push( msg_len>>>29 );
154
+ word_array.push( (msg_len<<3)&0x0ffffffff );
155
+
156
+
157
+ for ( blockstart=0; blockstart<word_array.length; blockstart+=16 ) {
158
+
159
+ for( i=0; i<16; i++ ) W[i] = word_array[blockstart+i];
160
+ for( i=16; i<=79; i++ ) W[i] = rotate_left(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
161
+
162
+ A = H0;
163
+ B = H1;
164
+ C = H2;
165
+ D = H3;
166
+ E = H4;
167
+
168
+ for( i= 0; i<=19; i++ ) {
169
+ temp = (rotate_left(A,5) + ((B&C) | (~B&D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
170
+ E = D;
171
+ D = C;
172
+ C = rotate_left(B,30);
173
+ B = A;
174
+ A = temp;
175
+ }
176
+
177
+ for( i=20; i<=39; i++ ) {
178
+ temp = (rotate_left(A,5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
179
+ E = D;
180
+ D = C;
181
+ C = rotate_left(B,30);
182
+ B = A;
183
+ A = temp;
184
+ }
185
+
186
+ for( i=40; i<=59; i++ ) {
187
+ temp = (rotate_left(A,5) + ((B&C) | (B&D) | (C&D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
188
+ E = D;
189
+ D = C;
190
+ C = rotate_left(B,30);
191
+ B = A;
192
+ A = temp;
193
+ }
194
+
195
+ for( i=60; i<=79; i++ ) {
196
+ temp = (rotate_left(A,5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
197
+ E = D;
198
+ D = C;
199
+ C = rotate_left(B,30);
200
+ B = A;
201
+ A = temp;
202
+ }
203
+
204
+ H0 = (H0 + A) & 0x0ffffffff;
205
+ H1 = (H1 + B) & 0x0ffffffff;
206
+ H2 = (H2 + C) & 0x0ffffffff;
207
+ H3 = (H3 + D) & 0x0ffffffff;
208
+ H4 = (H4 + E) & 0x0ffffffff;
209
+
210
+ }
211
+
212
+ var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
213
+
214
+ return temp.toLowerCase();
215
+
216
+ }
217
+
218
+ main();
219
+ })();
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: require_gist
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
+ - Evgeniy Dolzhenko
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-20 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: dolzenko@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/require_gist.rb
31
+ - spec/require_gist_spec.rb
32
+ - spec/require_gist_test_gist.rb
33
+ - thirdparty/require_gist.user.js
34
+ has_rdoc: true
35
+ homepage: http://dolzhenko.org
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.3.6
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Require code directly from GitHub's Gists
64
+ test_files: []
65
+