murlsh 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/public/js/js.js CHANGED
@@ -1,256 +1,275 @@
1
+ /*global $, window*/
2
+
1
3
  "use strict";
2
4
 
3
5
  var Murlsh = {};
4
6
 
5
- Murlsh.tag = function(name, attr, text) {
6
- var klass;
7
- var result = $('<' + name + ' />');
8
-
9
- if (attr) {
10
- if (attr.klass) {
11
- klass = attr.klass;
12
- delete attr.klass;
13
- }
14
- result.attr(attr);
15
- }
16
-
17
- if (text) {
18
- result.text(text);
19
- }
20
-
21
- if (klass) {
22
- result.addClass(klass);
23
- }
24
-
25
- return result;
7
+ Murlsh.img = function (src, text) {
8
+ text = text || '';
9
+ return $('<img />', {
10
+ src : src,
11
+ alt : text,
12
+ title : text
13
+ });
26
14
  };
27
15
 
28
- Murlsh.img = function(src, text) {
29
- text = text || '';
30
- return Murlsh.tag('img', {
31
- src : src,
32
- alt : text,
33
- title : text
34
- });
16
+ Murlsh.closer_add = function (x, header) {
17
+ var html = (typeof x === 'object') ? $('<div />').append(x).html() : x;
18
+
19
+ $.jGrowl(html, {
20
+ closeTemplate : 'X',
21
+ glue : 'before',
22
+ header : header,
23
+ sticky : true
24
+ });
35
25
  };
36
26
 
37
- Murlsh.closer_add = function(x, header) {
38
- var html = (typeof x == 'object') ? Murlsh.tag('div').append(x).html() : x;
27
+ Murlsh.object_tag = function (data, height, width, params) {
28
+ var object = $('<object />').attr({
29
+ data : data,
30
+ height : height,
31
+ type : 'application/x-shockwave-flash',
32
+ width : width
33
+ });
39
34
 
40
- $.jGrowl(html, {
41
- closeTemplate : 'X',
42
- glue :'before',
43
- header : header,
44
- sticky : true
35
+ $.each(params, function (i, v) {
36
+ object.append($('<param />', v));
45
37
  });
46
- };
47
38
 
48
- Murlsh.object_tag = function(data, height, width, params) {
49
- var result = '<object data="' + data + '" height="' + height +
50
- '" type="application/x-shockwave-flash" width="' + width + '">';
51
- $.each(params, function(i, v) {
52
- result += '<param name="' + v.name + '" value="' + v.value + '" />';
53
- });
54
- result += '</object>';
55
- return result;
39
+ return object;
56
40
  };
57
41
 
58
- Murlsh.flickr_thumb = function(d) {
59
- var photo = d.photo;
60
- if (d.stat == 'ok') {
61
- var base = 'http://farm' + photo.farm + '.static.flickr.com/' +
62
- photo.server + '/' + photo.id + '_';
63
- var zoom;
64
- if (photo.originalsecret) {
65
- zoom = base + photo.originalsecret + '_o.' + photo.originalformat;
66
- } else {
67
- zoom = base + photo.secret + '_m.jpg';
42
+ Murlsh.flickr_thumb = function (d) {
43
+ var photo = d.photo,
44
+ base,
45
+ owner,
46
+ zoom;
47
+ if (d.stat === 'ok') {
48
+ base = 'http://farm' + photo.farm + '.static.flickr.com/' +
49
+ photo.server + '/' + photo.id + '_';
50
+ zoom = base + photo.secret + '_m.jpg';
51
+
52
+ if (photo.originalsecret) {
53
+ zoom = base + photo.originalsecret + '_o.' + photo.originalformat;
54
+ }
55
+
56
+ owner = photo.owner;
57
+ return Murlsh.img(base + photo.secret + '_s.jpg',
58
+ photo.title._content +
59
+ (owner && owner.username ? ' by ' + owner.username : '')
60
+ ).addClass('thumb flickr').data('zoom', zoom);
68
61
  }
69
-
70
- var owner = photo.owner;
71
- return Murlsh.img(base + photo.secret + '_s.jpg',
72
- photo.title._content +
73
- (owner && owner.username ? ' by ' + owner.username : '')
74
- ).addClass('thumb flickr').data('zoom', zoom);
75
- }
76
62
  };
77
63
 
78
- Murlsh.flickr_click = function() {
79
- Murlsh.closer_add(Murlsh.img($(this).data('zoom')));
64
+ Murlsh.flickr_click = function () {
65
+ Murlsh.closer_add(Murlsh.img($(this).data('zoom')));
80
66
  };
81
67
 
82
- Murlsh.img_thumb = function(prefix, ext) {
83
- return Murlsh.img(prefix + 'th.' +
84
- (ext.match(/^pdf$/i) ? 'png' : ext)).addClass('thumb');
68
+ Murlsh.img_thumb = function (prefix, ext) {
69
+ return Murlsh.img(prefix + 'th.' +
70
+ (ext.match(/^pdf$/i) ? 'png' : ext)).addClass('thumb');
85
71
  };
86
72
 
87
- Murlsh.img_click = function() {
88
- Murlsh.closer_add(Murlsh.img($(this).data('href')));
73
+ Murlsh.img_click = function () {
74
+ Murlsh.closer_add(Murlsh.img($(this).data('href')));
89
75
  };
90
76
 
91
- Murlsh.vimeo_thumb = function(d) {
92
- return Murlsh.img(d.thumbnail_url, d.title).addClass('thumb vimeo').attr({
93
- height : d.thumbnail_height,
94
- width : d.thumbnail_width
95
- });
77
+ Murlsh.vimeo_thumb = function (d) {
78
+ return Murlsh.img(d.thumbnail_url, d.title).addClass('thumb vimeo').attr({
79
+ height : d.thumbnail_height,
80
+ width : d.thumbnail_width
81
+ });
96
82
  };
97
83
 
98
- Murlsh.vimeo_click = function() {
99
- Murlsh.closer_add($(this).data('embed_html'));
84
+ Murlsh.vimeo_click = function () {
85
+ Murlsh.closer_add($(this).data('embed_html'));
100
86
  };
101
87
 
102
- Murlsh.youtube_thumb = function(id) {
103
- return Murlsh.img('http://img.youtube.com/vi/' + id + '/1.jpg',
104
- 'click to watch').addClass('thumb youtube').data('id', id);
88
+ Murlsh.youtube_thumb = function (id) {
89
+ return Murlsh.img('http://img.youtube.com/vi/' + id + '/1.jpg',
90
+ 'click to watch').addClass('thumb youtube').data('id', id);
105
91
  };
106
92
 
107
- Murlsh.youtube_click = function() {
108
- var movie = 'http://www.youtube.com/v/' + $(this).data('id') +
109
- '?hd=1&amp;hl=en&amp;fs=1&amp;showinfo=0&amp;showsearch=0';
110
- Murlsh.closer_add(Murlsh.object_tag(movie, 344, 425, [{ name : 'movie', value : movie }]));
93
+ Murlsh.youtube_click = function () {
94
+ var movie = 'http://www.youtube.com/v/' + $(this).data('id') + '?' +
95
+ $.param({
96
+ hd : 1,
97
+ hl : 'en',
98
+ fs : 1,
99
+ showinfo : 0,
100
+ showsearch : 0
101
+ });
102
+ Murlsh.closer_add(Murlsh.object_tag(movie, 344, 425,
103
+ [{ name : 'movie', value : movie }]));
111
104
  };
112
105
 
113
- Murlsh.thumb_insert = function(img, click_function, a) {
114
- if (img) {
115
- if (Murlsh.is_iphone()) {
116
- a.prepend(img);
117
- } else {
118
- a.before(img.click(click_function));
106
+ Murlsh.thumb_insert = function (img, click_function, a) {
107
+ if (img) {
108
+ if (Murlsh.is_iphone()) {
109
+ a.prepend(img);
110
+ } else {
111
+ a.before(img.click(click_function));
112
+ }
119
113
  }
120
- }
121
114
  };
122
115
 
123
- Murlsh.is_iphone = function() {
124
- return navigator.userAgent.match(/i(phone|pod)/i);
116
+ Murlsh.is_iphone = function () {
117
+ return navigator.userAgent.match(/i(phone|pod)/i);
125
118
  };
126
119
 
127
120
  Murlsh.href_res = {
128
- flickr :
129
- /^http:\/\/(?:www\.)?flickr\.com\/photos\/[^\/]+?\/([0-9]+)/i,
130
- imageshack :
131
- /^(http:\/\/img\d+\.imageshack\.us\/img\d+\/\d+\/\w+\.)(jpe?g|gif|png)$/i,
132
- mp3 :
133
- /.*\.mp3$/i,
134
- s3 :
135
- /^(http:\/\/static\.mmb\.s3\.amazonaws.com\/.*\.)(jpe?g|gif|pdf|png)$/i,
136
- vimeo :
137
- /^http:\/\/(?:www\.)?vimeo\.com\/([0-9]+)$/i,
138
- youtube :
139
- /^http:\/\/(?:(?:www|uk)\.)?youtube\.com\/watch\?v=(.+?)(?:&|$)/i
121
+ flickr :
122
+ /^http:\/\/(?:www\.)?flickr\.com\/photos\/[@\w\-]+?\/([\d]+)/i,
123
+ imageshack :
124
+ /^(http:\/\/img\d+\.imageshack\.us\/img\d+\/\d+\/\w+\.)(jpe?g|gif|png)$/i,
125
+ mp3 :
126
+ /\.mp3$/i,
127
+ s3 :
128
+ /^(http:\/\/static\.mmb\.s3\.amazonaws\.com\/\w+\.)(jpe?g|gif|pdf|png)$/i,
129
+ vimeo :
130
+ /^http:\/\/(?:www\.)?vimeo\.com\/(\d+)$/i,
131
+ youtube :
132
+ /^http:\/\/(?:(?:www|uk)\.)?youtube\.com\/watch\?v=([\w\-]+)(?:&|$)/i
140
133
  };
141
134
 
142
- Murlsh.add_extra = function() {
143
- var this_a = $(this);
144
-
145
- var href = $(this).attr('href');
146
-
147
- var match = {};
148
- $.each(Murlsh.href_res, function(x, re) { return !(match[x] = re.exec(href)); });
135
+ Murlsh.add_extra = function () {
136
+ var href = $(this).attr('href'),
137
+ match = {},
138
+ swf = 'swf/player_mp3_mini.swf',
139
+ thumb;
149
140
 
150
- var thumb;
141
+ $.each(Murlsh.href_res, function (x, re) {
142
+ return !(match[x] = re.exec(href));
143
+ });
151
144
 
152
- if (match.flickr) {
153
- var callback = function(d) {
154
- Murlsh.thumb_insert(Murlsh.flickr_thumb(d), Murlsh.flickr_click, this_a);
155
- };
156
- $.getJSON('http://api.flickr.com/services/rest/?api_key=d04e574aaf11bf2e1c03cba4ee7e5725&method=flickr.photos.getinfo&format=json&photo_id=' +
157
- match.flickr[1] + '&jsoncallback=?', callback);
158
- } else if (match.imageshack) {
159
- thumb = Murlsh.img_thumb(match.imageshack[1], match.imageshack[2]).data(
160
- 'href', match.imageshack[0]);
161
- Murlsh.thumb_insert(thumb, Murlsh.img_click, this_a.html('imageshack.us'));
162
- } else if (match.mp3) {
163
- var swf = 'swf/player_mp3_mini.swf';
164
- $(this).before(Murlsh.object_tag(swf, 20, 200, [
165
- { name : 'bgcolor', value : '#000000' },
166
- { name : 'FlashVars', value : 'mp3=' + match.mp3[0] },
167
- { name : 'movie', value : swf }
168
- ]));
169
- } else if (match.s3) {
170
- thumb = Murlsh.img_thumb(match.s3[1], match.s3[2]);
171
- if (match.s3[2].match(/^pdf$/i)) {
172
- this_a.before(thumb).html('pdf');
173
- } else {
174
- if (Murlsh.is_iphone()) {
175
- this_a.html(thumb);
176
- } else {
177
- this_a.html('link');
178
- this_a.before(thumb.data('href', match.s3[0]).click(Murlsh.img_click));
179
- }
145
+ if (match.flickr) {
146
+ $.ajax({
147
+ url : 'http://api.flickr.com/services/rest/',
148
+ data : {
149
+ api_key : 'd04e574aaf11bf2e1c03cba4ee7e5725',
150
+ format : 'json',
151
+ method : 'flickr.photos.getinfo',
152
+ photo_id : match.flickr[1]
153
+ },
154
+ dataType : 'jsonp',
155
+ jsonp : 'jsoncallback',
156
+ success : function (d) {
157
+ Murlsh.thumb_insert(Murlsh.flickr_thumb(d),
158
+ Murlsh.flickr_click, $(this));
159
+ },
160
+ context : $(this)
161
+ });
162
+ } else if (match.imageshack) {
163
+ Murlsh.thumb_insert(
164
+ Murlsh.img_thumb(match.imageshack[1], match.imageshack[2]).data(
165
+ 'href', match.imageshack[0]),
166
+ Murlsh.img_click, $(this).html('imageshack.us'));
167
+ } else if (match.mp3) {
168
+ $(this).before(Murlsh.object_tag(swf, 20, 200, [
169
+ { name : 'bgcolor', value : '#000000' },
170
+ { name : 'FlashVars', value : 'mp3=' + href },
171
+ { name : 'movie', value : swf }
172
+ ]));
173
+ } else if (match.s3) {
174
+ thumb = Murlsh.img_thumb(match.s3[1], match.s3[2]);
175
+
176
+ if (match.s3[2].match(/^pdf$/i)) {
177
+ $(this).before(thumb).html('pdf');
178
+ } else {
179
+ if (Murlsh.is_iphone()) {
180
+ $(this).html(thumb);
181
+ } else {
182
+ $(this).html('link');
183
+ $(this).before(thumb.data('href', match.s3[0]).click(
184
+ Murlsh.img_click));
185
+ }
186
+ }
187
+ } else if (match.vimeo) {
188
+ $.ajax({
189
+ url : 'http://vimeo.com/api/oembed.json',
190
+ data : { url : 'http://vimeo.com/' + match.vimeo[1] },
191
+ dataType : 'jsonp',
192
+ success : function (d) {
193
+ Murlsh.thumb_insert(Murlsh.vimeo_thumb(d).data('embed_html',
194
+ d.html.replace(/&/g, '&amp;')),
195
+ Murlsh.vimeo_click, $(this));
196
+ },
197
+ context : $(this)
198
+ });
199
+ } else if (match.youtube) {
200
+ Murlsh.thumb_insert(Murlsh.youtube_thumb(match.youtube[1]),
201
+ Murlsh.youtube_click, $(this));
180
202
  }
181
- } else if (match.vimeo) {
182
- var callback = function(d) {
183
- var thumb = Murlsh.vimeo_thumb(d).data('embed_html', d.html);
184
- Murlsh.thumb_insert(thumb, Murlsh.vimeo_click, this_a);
185
- };
186
- $.getJSON('http://vimeo.com/api/oembed.json?url=http%3A//vimeo.com/' +
187
- match.vimeo[1] + '&callback=?', callback);
188
- } else if (match.youtube) {
189
- thumb = Murlsh.youtube_thumb(match.youtube[1]);
190
- Murlsh.thumb_insert(thumb, Murlsh.youtube_click, this_a);
191
- }
192
203
  };
193
204
 
194
- Murlsh.format_li = function(d) {
195
- var li = Murlsh.tag('li').append(Murlsh.tag('a', { href : d.url }, d.title));
196
-
197
- if (d.name) {
198
- li.prepend(Murlsh.tag('div', { klass : 'name' }, d.name));
199
- }
205
+ Murlsh.format_li = function (d) {
206
+ var li = $('<li />').append($('a', {
207
+ href : d.url,
208
+ text : d.title
209
+ })),
210
+ icon_size = 32;
200
211
 
201
- var icon_size = 32;
212
+ if (d.name) {
213
+ li.prepend($('<div />', { text : d.name }).addClass('name'));
214
+ }
202
215
 
203
- if (d.email) {
204
- li.prepend(Murlsh.tag('div', { klass : 'icon' }).append(
205
- Murlsh.img(
206
- 'http://www.gravatar.com/avatar/' + d.email + '?s=' + icon_size,
207
- d.name).attr({
208
- width : icon_size,
209
- height : icon_size
210
- })));
211
- }
216
+ if (d.email) {
217
+ li.prepend($('<div />').addClass('icon').append(
218
+ Murlsh.img(
219
+ 'http://www.gravatar.com/avatar/' + d.email + '?s=' + icon_size,
220
+ d.name).attr({
221
+ width : icon_size,
222
+ height : icon_size
223
+ })));
224
+ }
212
225
 
213
- return li;
226
+ return li;
214
227
  };
215
228
 
216
- Murlsh.iphone_init = function() {
217
- window.onorientationchange = function() {
218
- if (window.orientation === 0 || window.orientation == 180) {
219
- $('#urls').width(290);
220
- } else {
221
- $('#urls').width(450);
222
- }
223
- };
229
+ Murlsh.iphone_init = function () {
230
+ window.onorientationchange = function () {
231
+ var width = 450;
232
+ if (window.orientation === 0 || window.orientation === 180) {
233
+ width = 290;
234
+ }
235
+ $('#urls').width(width);
236
+ };
224
237
 
225
- window.onorientationchange();
238
+ window.onorientationchange();
226
239
 
227
- $('#urls li:first').prepend(Murlsh.tag('a', { href : '#bottom' }, 'bottom'));
228
- $('#urls li:last').append(Murlsh.tag('a', { href : '#urls' }, 'top'));
240
+ $('#urls li:first').prepend($('<a />', {
241
+ href : '#bottom',
242
+ text : 'bottom'
243
+ }));
244
+ $('#urls li:last').append($('<a />', {
245
+ href : '#urls',
246
+ text : 'top'
247
+ }));
229
248
  };
230
249
 
231
- $(document).ready(function() {
232
- if (Murlsh.is_iphone()) {
233
- Murlsh.iphone_init();
234
- }
235
- $('#urls a').map(Murlsh.add_extra);
236
-
237
- $('#submit').click(function() {
238
- $.post('url', {
239
- url : $('#url').val(),
240
- via : $('#via').val(),
241
- auth : $('#auth').val()
242
- }, function(d) {
243
- $.each(d, function(i, v) {
244
- var li = Murlsh.format_li(v);
245
- $('#urls > li:first').after(li);
246
- $(li).children('a:first').map(Murlsh.add_extra);
247
- });
248
- $('#url').val('');
249
- $('#via').val('');
250
- }, 'json');
251
- });
252
-
253
- if ($.cookie('auth')) {
254
- $('#auth').val($.cookie('auth'));
255
- }
250
+ $(document).ready(function () {
251
+ if (Murlsh.is_iphone()) {
252
+ Murlsh.iphone_init();
253
+ }
254
+ $('#urls a').map(Murlsh.add_extra);
255
+
256
+ $('#submit').click(function () {
257
+ $.post('url', {
258
+ url : $('#url').val(),
259
+ via : $('#via').val(),
260
+ auth : $('#auth').val()
261
+ }, function (d) {
262
+ $.each(d, function (i, v) {
263
+ var li = Murlsh.format_li(v);
264
+ $('#urls > li:first').after(li);
265
+ $(li).children('a:first').map(Murlsh.add_extra);
266
+ });
267
+ $('#url').val('');
268
+ $('#via').val('');
269
+ }, 'json');
270
+ });
271
+
272
+ if ($.cookie('auth')) {
273
+ $('#auth').val($.cookie('auth'));
274
+ }
256
275
  });
@@ -0,0 +1,101 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+
3
+ require 'tempfile'
4
+ require 'time'
5
+
6
+ require 'murlsh'
7
+
8
+ require 'test/unit'
9
+
10
+ class AtomFeedTest < Test::Unit::TestCase
11
+
12
+ class MockUrl
13
+
14
+ def initialize(content_type, id, is_image, name, time, title_stripped,
15
+ url, via)
16
+ @content_type,
17
+ @id,
18
+ @is_image,
19
+ @name,
20
+ @time,
21
+ @title_stripped,
22
+ @url,
23
+ @via =
24
+ content_type,
25
+ id,
26
+ is_image,
27
+ name,
28
+ time,
29
+ title_stripped,
30
+ url,
31
+ via
32
+ end
33
+
34
+ def is_image?; is_image; end
35
+
36
+ attr_reader :content_type
37
+ attr_reader :id
38
+ attr_reader :is_image
39
+ attr_reader :name
40
+ attr_reader :time
41
+ attr_reader :title_stripped
42
+ attr_reader :url
43
+ attr_reader :via
44
+ end
45
+
46
+ def test_atom_feed
47
+ feed = Murlsh::AtomFeed.new('http://test.com/test/', :title => 'test')
48
+ time1 = Time.parse('dec 19 2009 12:34:56pm').utc
49
+ time2 = Time.parse('dec 20 2009 10:10:10am').utc
50
+
51
+ entries = [
52
+ MockUrl.new('text/html', 1, false, 'test 1', time1, 'test title',
53
+ 'http://matthewm.boedicker.org/', 'http://www.google.com'),
54
+ MockUrl.new('image/jpeg', 2, true, 'test 2', time2, 'image test',
55
+ 'http://matthewm.boedicker.org/test.jpg', nil)
56
+ ]
57
+
58
+ expected = <<EOS
59
+ <?xml version="1.0" encoding="UTF-8"?>
60
+ <feed xmlns="http://www.w3.org/2005/Atom">
61
+ <id>http://test.com/test/</id>
62
+ <link href="http://test.com/test/atom.xml" rel="self"/>
63
+ <title>test</title>
64
+ <updated>2009-12-20T15:10:10Z</updated>
65
+ <entry>
66
+ <author>
67
+ <name>test 1</name>
68
+ </author>
69
+ <title>test title</title>
70
+ <id>tag:test.com,2009-12-19:test.com/test/1</id>
71
+ <summary>test title</summary>
72
+ <updated>2009-12-19T17:34:56Z</updated>
73
+ <link href="http://matthewm.boedicker.org/"/>
74
+ <link type="text/html" title="google.com" href="http://www.google.com" rel="via"/>
75
+ </entry>
76
+ <entry>
77
+ <author>
78
+ <name>test 2</name>
79
+ </author>
80
+ <title>image test</title>
81
+ <id>tag:test.com,2009-12-20:test.com/test/2</id>
82
+ <summary>image test</summary>
83
+ <updated>2009-12-20T15:10:10Z</updated>
84
+ <link href="http://matthewm.boedicker.org/test.jpg"/>
85
+ <link type="image/jpeg" title="Full-size" href="http://matthewm.boedicker.org/test.jpg" rel="enclosure"/>
86
+ </entry>
87
+ </feed>
88
+ EOS
89
+
90
+ assert_equal(expected, feed.make(entries, :indent => 2))
91
+
92
+ f = Tempfile.open('test_atom_feed')
93
+ feed.make(entries, :indent => 2, :target => f)
94
+
95
+ f.open
96
+ assert_equal(expected, f.read)
97
+ f.close
98
+
99
+ end
100
+
101
+ end