Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 100 lines (86 sloc) 2.761 kB
cf0c4a9 @tubbo fixed up config.ru
authored
1 # = StatusExchange
9d98a58 @tubbo pushing up
authored
2 #
cf0c4a9 @tubbo fixed up config.ru
authored
3 # A Rack app that provides an aggregate status message feed from a number of
4 # social networks. Status messages from Facebook, Twitter, Soundcloud and
5 # GitHub are sorted by date into a single JSON array which is both simple
6 # and efficient to parse from a JavaScript client. It was designed for use
7 # with the jQuery.ticker plugin in use on the frontend of +psychedeli.ca+.
8 #
9 # == Supported services
9d98a58 @tubbo pushing up
authored
10 #
11 # - Twitter
a0a957d @tubbo i think i get rack now
authored
12 # - Facebook
9d98a58 @tubbo pushing up
authored
13 # - GitHub
14 #
cf0c4a9 @tubbo fixed up config.ru
authored
15 # == Configuration
16 #
17 # Reads from cfg/status_exchange.yml
18 #
9d98a58 @tubbo pushing up
authored
19 # Author:: Tom Scott
20 # Homepage:: http://psychedeli.ca/
21
22 require 'twitter'
23 require 'mogli'
cf0c4a9 @tubbo fixed up config.ru
authored
24 require 'soundcloud'
f66a8e7 @tubbo finished status.json server
authored
25 require 'atom'
cf0c4a9 @tubbo fixed up config.ru
authored
26 require 'date'
27 require 'json'
9d98a58 @tubbo pushing up
authored
28
a0a957d @tubbo i think i get rack now
authored
29 class StatusExchange
cf0c4a9 @tubbo fixed up config.ru
authored
30 def initialize(application, options)
31 @config = options[:config]
32 @mount = options[:url]
33 @app = application
34
35 @twitter = Struct.new(tweets: Twitter.user_timeline(@config['twitter']['user_name']))
f66a8e7 @tubbo finished status.json server
authored
36
cf0c4a9 @tubbo fixed up config.ru
authored
37 fb_client = Mogli::Client.new(@config['facebook']['access_token'])
f66a8e7 @tubbo finished status.json server
authored
38 @facebook = Mogli::User.find('me', facebook)
39
cf0c4a9 @tubbo fixed up config.ru
authored
40 sc_client = Soundcloud.new(:client_id => @config['soundcloud']['client_id'])
41 @soundcloud = sc_client.get('/resolve', url: "http://soundcloud.com/#{@config['soundcloud']['user_name']}")
f66a8e7 @tubbo finished status.json server
authored
42
cf0c4a9 @tubbo fixed up config.ru
authored
43 @github = Atom::Feed.load_feed(URI.parse("https://github.com/#{@config['github']['user_name']}.atom"))
f66a8e7 @tubbo finished status.json server
authored
44 end
45
a0a957d @tubbo i think i get rack now
authored
46 def call(env)
f66a8e7 @tubbo finished status.json server
authored
47 @tweets.each do |tweet|
48 statuses << {
49 message: tweet.text,
50 date: DateTime.parse(tweet.created_at),
51 link: "https://twitter.com/tubbo/status/#{tweet.id}",
52 type: 'twitter'
53 }
54 end
55
56 @facebook.posts.each do |post|
57 statuses << {
58 message: post.story,
59 date: DateTime.parse(post.created_time),
60 link: post.link,
61 type: 'facebook'
62 }
a0a957d @tubbo i think i get rack now
authored
63 end
f66a8e7 @tubbo finished status.json server
authored
64
65 @github.each_entry do |entry|
66 statuses << {
67 message: entry.title,
68 date: DateTime.parse(entry.updated),
69 link: entry.link,
70 type: 'github'
71 }
72 end
73
74 @soundcloud.tracks.each do |track|
75 statuses << {
76 message: "posted #{track.title} on Soundcloud.",
77 date: DateTime.parse(track.created_at),
78 link: track.permalink_url,
79 type: 'soundcloud'
80 }
81 end
82
83 @soundcloud.comments.each do |comment|
84 commented_track = @sc_client.get('/track', id: comment.track_id)
85 statuses << {
86 message: "commented on #{commented_track.title}",
87 date: DateTime.parse(comment.created_at),
88 link: commented_track.permalink_url,
89 type: 'soundcloud'
90 }
91 end
92
cf0c4a9 @tubbo fixed up config.ru
authored
93 # sort by date
f66a8e7 @tubbo finished status.json server
authored
94 statuses.sort {|this_status,next_status| this_status.date <=> next_status.date }
95
cf0c4a9 @tubbo fixed up config.ru
authored
96 # return as JSON
f66a8e7 @tubbo finished status.json server
authored
97 [ 200, {'Content-Type' => 'application/json'}, statuses.to_json ]
9d98a58 @tubbo pushing up
authored
98 end
f66a8e7 @tubbo finished status.json server
authored
99 end
Something went wrong with that request. Please try again.