changeset 15:207917e41964

Add tweets~ Feature is now on par with the thing it's replacing.
author edogawaconan <me@myconan.net>
date Fri, 05 Sep 2014 20:37:25 +0900
parents aad3b253ffd5
children fff788625214
files app/controllers/tweets_controller.rb app/helpers/application_helper.rb app/models/tweet.rb app/views/tweets/show.atom.builder config/routes.rb test/controllers/tweets_controller_test.rb test/models/tweet_test.rb
diffstat 7 files changed, 59 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/controllers/tweets_controller.rb	Fri Sep 05 20:37:25 2014 +0900
@@ -0,0 +1,6 @@
+class TweetsController < ApplicationController
+  def show
+    @tweets = Tweet.new(params[:id]).timeline
+    @user = @tweets.first.user
+  end
+end
--- a/app/helpers/application_helper.rb	Fri Sep 05 19:03:15 2014 +0900
+++ b/app/helpers/application_helper.rb	Fri Sep 05 20:37:25 2014 +0900
@@ -1,2 +1,3 @@
 module ApplicationHelper
+  include Twitter::Autolink
 end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/models/tweet.rb	Fri Sep 05 20:37:25 2014 +0900
@@ -0,0 +1,21 @@
+class Tweet
+  CONFIG = {
+    :consumer_key => ENV["RT_CONSUMER_KEY"],
+    :consumer_secret => ENV["RT_CONSUMER_SECRET"],
+    :access_token => ENV["RT_OAUTH_TOKEN"],
+    :access_token_secret => ENV["RT_OAUTH_TOKEN_SECRET"]
+  }
+
+  def timeline
+    @timeline ||= @client.user_timeline(@twitter_id)
+  end
+
+  def initialize(twitter_id)
+    @client = Twitter::REST::Client.new do |config|
+                CONFIG.each do |cfg_key, cfg_value|
+                  config.public_send("#{cfg_key}=", cfg_value)
+                end
+              end
+    @twitter_id = twitter_id
+  end
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/views/tweets/show.atom.builder	Fri Sep 05 20:37:25 2014 +0900
@@ -0,0 +1,14 @@
+atom_feed do |feed|
+  feed.title "#{@user.name} (@#{@user.screen_name})"
+  feed.updated @tweets.first.created_at
+
+  @tweets.each do |tweet|
+    feed.entry tweet, :url => tweet.uri, :updated => tweet.created_at do |entry|
+      entry.title tweet.created_at.rfc2822
+      entry.content auto_link(tweet.text), :type => "html"
+      entry.author do |author|
+        author.name tweet.user.screen_name
+      end
+    end
+  end
+end
--- a/config/routes.rb	Fri Sep 05 19:03:15 2014 +0900
+++ b/config/routes.rb	Fri Sep 05 20:37:25 2014 +0900
@@ -5,6 +5,8 @@
   # You can have the root of your site routed with "root"
   root 'static#index'
 
+  get "*id" => "tweets#show", :defaults => { :format => :atom }
+
   # Example of regular route:
   #   get 'products/:id' => 'catalog#view'
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/controllers/tweets_controller_test.rb	Fri Sep 05 20:37:25 2014 +0900
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class TweetsControllerTest < ActionController::TestCase
+  test "should get show" do
+    get :show, :id => "edogawa_test", :format => :atom
+    assert_response :success
+  end
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/models/tweet_test.rb	Fri Sep 05 20:37:25 2014 +0900
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class TweetTest < ActiveSupport::TestCase
+  test "loads a tweet" do
+    assert_not_nil Tweet.new("edogawa_test")
+  end
+end