1
- ///Asks Meetup.com for any public event listing from one of the PyLadies chapters upcoming in the next week
2
- //Localized for use by any chapter--just copy file over to your location site and sub your meetup key for "meetup_key" and meetup group id (indicated as data id on locations index.html page) for "chapter_ids" in addUpcomingMeetups function.
3
-
4
- //helper functions
5
- function create_urls ( input ) {
6
- return input
7
- . replace ( / ( f t p | h t t p | h t t p s | f i l e ) : \/ \/ [ \S ] + ( \b | $ ) / gim, '"$&" target="_blank"' )
8
- . replace ( / ( [ ^ \/ ] ) ( w w w [ \S ] + ( \b | $ ) ) / gim, '"http://$2" target="_blank"' ) ;
9
- } //end url parsing helper
10
-
11
- function convert_time_miliseconds_to_date ( t ) {
12
- var d = new Date ( t ) ; // The 0 there is the key, which sets the date to the epoch
13
- return String ( d ) . slice ( 0 , 11 ) ;
14
- } //end convert miliseconds_to_date
15
-
16
- // functions
17
- function addUpcomingMeetups ( chapter_ids ) {
18
- var meetup_key = '70704f53354b686770246f68494e2637' ;
19
-
20
- $ . ajax ( {
21
- url : 'http://api.meetup.com/2/events.json?key=' + meetup_key + '&group_id=' + chapter_ids + '&fields=group_photo&time=0m,1m&status=upcoming&sign=true' ,
22
- dataType : 'jsonp' ,
23
- success : function ( data ) {
24
- buildHtml ( data ) ;
25
- } , //end success
26
- error : function ( data ) {
27
- handleError ( data ) ;
28
- } //end error
29
- } ) ; //end .ajax()
30
- return false ;
31
- } //end addUpcomingMeetups function
32
-
33
- function buildHtml ( data ) {
1
+ /* Note: Updated to reflect JS Module pattern for encapsulation (v2)
2
+ * To call: PyladiesMeetupWidget.addUpcomingMeetups(chapterIds)
3
+ * Requests location meetups from Meetup.com coming up in the next month
4
+ * and uses data to construct html entries for each event that are appended
5
+ * to an '#upcomingMeetupsList' div in page's html.
6
+ * Can be given any number of chapterIds.
7
+ */
8
+
9
+ var PyladiesMeetupWidget = ( function ( ) {
10
+ var key = '70704f53354b686770246f68494e2637' ;
11
+
12
+ // Private methods //
13
+
14
+ _createUrls = function ( url ) {
15
+ // creates <a href> links from urls for event clickthroughs
16
+ return url
17
+ . replace ( / ( f t p | h t t p | h t t p s | f i l e ) : \/ \/ [ \S ] + ( \b | $ ) / gim, '"$&" target="_blank"' )
18
+ . replace ( / ( [ ^ \/ ] ) ( w w w [ \S ] + ( \b | $ ) ) / gim, '"http://$2" target="_blank"' ) ;
19
+ } ;
20
+
21
+ _convertMilisecondsToDate = function ( ms ) {
22
+ // converts miliseconds since epoch to date format
23
+ var date = new Date ( ms ) ;
24
+ return String ( date ) . slice ( 0 , 11 ) ;
25
+ } ;
26
+
27
+ _makeAjaxRequest = function ( ids ) {
28
+ // fetch data
29
+ $ . ajax ( {
30
+ url : 'http://api.meetup.com/2/events.json?key=' + key + '&group_id=' + ids +
31
+ '&fields=group_photo&time=0m,1m&status=upcoming&sign=true' ,
32
+ dataType : 'jsonp' ,
33
+ success : function ( data ) {
34
+ _buildHtml ( data ) ;
35
+ } ,
36
+ error : function ( data ) {
37
+ _handleError ( data ) ;
38
+ }
39
+ } ) ;
40
+ } ;
41
+
42
+ _getJSON = function ( datum ) {
43
+ // {_buildHtml helper}
44
+ // note: Not every meetup group event request returns a photo--not sure why, but
45
+ // noticed that neither Taiwan or Bangalore's logos returned, so perhaps there
46
+ // is some difference in data offered in parts of Asia?
47
+ var json = {
48
+ thumbLink : datum . group . group_photo ? datum . group . group_photo . thumb_link : "" ,
49
+ groupName : datum . group . name ,
50
+ eventLink : _createUrls ( datum . event_url ) ,
51
+ eventName : datum . name ,
52
+ eventDate : _convertMilisecondsToDate ( datum . time )
53
+ } ;
54
+
55
+ return json ;
56
+ } ;
57
+
58
+ _buildHtml = function ( data ) {
59
+ var html ;
60
+ var datum ;
61
+ var json ;
62
+
63
+ // remove any old meetup list still attached to dom and append new $ul
34
64
$ ( '#upcomingMeetupsList ul' ) . remove ( ) ;
35
65
$ ( '#upcomingMeetupsList' ) . append ( '<ul></ul>' ) ;
36
- for ( var key in data . results ) {
37
- url_link = create_urls ( data . results [ key ] . event_url ) ;
38
- event_date = convert_time_miliseconds_to_date ( data . results [ key ] . time ) ;
39
- var html = '<img class="meetup_group_icon" src="' + data . results [ key ] . group . group_photo . thumb_link + '" height="80" width="80" />' ;
40
- html += '<p class="meetup_listing_group">' + data . results [ key ] . group . name + '</p>' ;
41
- html += '<p class="meetup_listing_event_title"><a href=' + url_link + '>' + data . results [ key ] . name + '</a></p>' ;
42
- html += '<p class="meetup_event_date">' + event_date + '</p>' ;
43
- $ ( '#upcomingMeetupsList' ) . find ( 'ul' ) . append ( '<li>' + html + '</li>' ) ;
66
+
67
+ for ( var i = 0 ; i < data . results . length ; i ++ ) {
68
+ datum = data . results [ i ] ;
69
+ json = _getJSON ( datum ) ;
70
+
71
+ // todo(fw): pull this out into a template
72
+ html = '<img class="meetup_group_icon" src="' + json . thumbLink + '" height="80" width="80" />' ;
73
+ html += '<p class="meetup_listing_group">' + json . groupName + '</p>' ;
74
+ html += '<p class="meetup_listing_event_title"><a href=' + json . eventLink + '>' + json . eventName + '</a></p>' ;
75
+ html += '<p class="meetup_event_date">' + json . eventDate + '</p>' ;
76
+
77
+ $ ( '#upcomingMeetupsList ul' ) . append ( '<li>' + html + '</li>' ) ;
44
78
}
45
- }
79
+ } ;
46
80
47
- function handleError ( data ) {
81
+ _handleError = function ( data ) {
82
+ // remove any stale list attached to dom and print error message
48
83
$ ( '#upcomingMeetupsList ul' ) . remove ( ) ;
49
- $ ( '#upcomingMeetupsList' ) . append ( '<ul></ul>' ) ;
50
- $ ( '#upcomingMeetupsList' ) . find ( 'ul' ) . append ( '<li>Sorry, we are unable to reach Meetup.com at this time</li>' ) ;
51
- }
84
+ $ ( '#upcomingMeetupsList' )
85
+ . append ( '<ul><li>Sorry, we are unable to reach Meetup.com at this time</li></ul>' ) ;
86
+ } ;
87
+
88
+ return {
89
+ // returns public method
90
+ addUpcomingMeetups : function ( chapterIds ) {
91
+ _makeAjaxRequest ( chapterIds ) ;
92
+ }
93
+ } ;
94
+ } ) ( ) ;
0 commit comments