social_current 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in github_activity.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Tristan O'Neil
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # Social Current
2
+
3
+ Makes it easy to integrate a social activity stream into your application from third party APIs. Social Current currently only supports GitHub and Twitter but feel free to contribute additional integrations.
4
+
5
+ ![Social Current Example](http://f.cl.ly/items/3V2J2E0F1q3N401H1w3x/social_current.png)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'social_current'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install social_current
20
+
21
+ ## Usage
22
+
23
+ You may use Social Current directly within your view like.
24
+
25
+ ```ruby
26
+ <% SocialCurrent::Stream.new({ :twitter => "tristanoneil", :github => "tristanoneil" }).each do |item| %>
27
+ <%= item[:message] >
28
+ <% end %>
29
+ ```
30
+
31
+ Though it's probably a better idea to add a method to a class or model like.
32
+
33
+ ```ruby
34
+ class User < ActiveRecord::Base
35
+ def stream
36
+ SocialCurrent::Stream.new({ :twitter => twitter_username, :github => github_username })
37
+ end
38
+ end
39
+ ```
40
+
41
+ Then you can easily access this from within your view like.
42
+
43
+ ```ruby
44
+ <% @user.stream.each do |item| %>
45
+ <%= item[:message] >
46
+ <% end %>
47
+ ```
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ end
8
+
9
+ desc "Run tests"
10
+ task :default => :test
@@ -0,0 +1,35 @@
1
+ module SocialCurrent
2
+ class Github < Service
3
+ include HTTParty
4
+ base_uri "https://api.github.com"
5
+
6
+ def raw_user
7
+ @raw_user ||= fetch("/users/#{@user}", "#{@user}_github_raw_user.json")
8
+ end
9
+
10
+ def raw_stream
11
+ @raw_stream ||= fetch("/users/#{@user}/events", "#{@user}_github_raw_stream.json")
12
+ end
13
+
14
+ def stream
15
+ @stream ||= raw_stream.collect do |s|
16
+ { :message => format_message(s), :created_at => Time.parse(s["created_at"]).utc }
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def format_message(event)
23
+ case event["type"]
24
+ when "PushEvent"
25
+ return "#{self.raw_user["name"]} pushed to <a href='http://github.com/#{event["repo"]["name"]}'>#{event["repo"]["name"]}</a>."
26
+ when "FollowEvent"
27
+ return "#{self.raw_user["name"]} followed <a href='#{event["payload"]["target"]["html_url"]}'>#{event["payload"]["target"]["name"]}</a>."
28
+ when "CreateEvent"
29
+ return "#{self.raw_user["name"]} created <a href='http://github.com/#{event["repo"]["name"]}'>#{event["repo"]["name"]}</a>."
30
+ when "WatchEvent"
31
+ return "#{self.raw_user["name"]} started watching <a href='http://github.com/#{event["repo"]["name"]}'>#{event["repo"]["name"]}</a>."
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,28 @@
1
+ module SocialCurrent
2
+ class Service
3
+ def initialize(user)
4
+ @user = user
5
+ end
6
+
7
+ def fetch(remote, local)
8
+ @contents = if cache_valid?("/tmp/#{local}")
9
+ JSON.parse(File.read("/tmp/#{local}"))
10
+ else
11
+ write_cache(local, JSON.parse(self.class.get(remote).body))
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def write_cache(local, contents)
18
+ File.open("/tmp/#{local}", "w") do |f|
19
+ f.write(contents.to_json)
20
+ end
21
+ contents
22
+ end
23
+
24
+ def cache_valid?(cache)
25
+ File.exists?(cache) && File.new(cache).mtime > Time.now - 3000
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ module SocialCurrent
2
+ class Stream
3
+ def initialize(options)
4
+ @options = options.reject { |k, v| !SUPPORTED_INTEGRATIONS.include?(k) }
5
+ end
6
+
7
+ def build
8
+ @stream ||= @options.collect do |k, v|
9
+ eval("SocialCurrent::" + k.to_s.capitalize).new(v).stream
10
+ end.flatten.sort_by { |k, v| k[:created_at] }.reject { |k, v| k[:message].nil? }.reverse
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,26 @@
1
+ module SocialCurrent
2
+ class Twitter < Service
3
+ include HTTParty
4
+ base_uri "https://api.twitter.com/1"
5
+
6
+ def raw_user
7
+ @raw_user ||= fetch("/users/show.json?screen_name=#{@user}", "#{@user}_twitter_raw_user.json")
8
+ end
9
+
10
+ def raw_stream
11
+ @raw_stream ||= fetch("/statuses/user_timeline.json?&screen_name=#{@user}", "#{@user}_twitter_raw_stream.json")
12
+ end
13
+
14
+ def stream
15
+ @stream ||= raw_stream.collect do |s|
16
+ { :message => format_message(s), :created_at => Time.parse(s["created_at"]).utc }
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def format_message(status)
23
+ "#{raw_user["name"]} said \"#{status["text"]}\""
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,4 @@
1
+ module SocialCurrent
2
+ VERSION = "0.0.1"
3
+ SUPPORTED_INTEGRATIONS = [:github, :twitter]
4
+ end
@@ -0,0 +1,8 @@
1
+ require "httparty"
2
+ require "json"
3
+
4
+ require "social_current/version"
5
+ require "social_current/service"
6
+ require "social_current/github"
7
+ require "social_current/twitter"
8
+ require "social_current/stream"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/social_current/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Tristan O'Neil"]
6
+ gem.email = ["tristanoneil@gmail.com"]
7
+ gem.description = %q{Makes it easy to integrate the Github activity stream into your application.}
8
+ gem.summary = %q{Makes it easy to integrate the Github activity stream into your application.}
9
+
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
+ gem.name = "social_current"
14
+ gem.require_paths = ["lib"]
15
+ gem.version = SocialCurrent::VERSION
16
+
17
+ gem.add_runtime_dependency "httparty", "~> 0.8.3"
18
+ gem.add_development_dependency "fakeweb", "~> 1.3.0"
19
+ end