Skip to content

Commit

Permalink
subscription demo update
Browse files Browse the repository at this point in the history
  • Loading branch information
nov committed Jun 23, 2011
1 parent 55363bb commit 7055af1
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 6 deletions.
31 changes: 29 additions & 2 deletions app/controllers/subscriptions_controller.rb
@@ -1,11 +1,38 @@
class SubscriptionsController < ApplicationController
before_filter :require_authentication, :only => [:index, :create]

def index
end

# for Subscription Verification
def show
render :text => params[:'hub.challenge']
subscription = Subscription.find(params[:id])
if subscription.verify_token == params[:'hub.verify_token']
render :text => params[:'hub.challenge']
else
render :text => 'verify_token invalid', :status => 401
end
end

def create
render :nothing => true, :status => 205
subscription = current_user.subscriptions.build(params[:subscription])
if subscription.save
subscription.subscribe! subscription_url(subscription)
else
flash[:error] = {
:title => 'Invalid',
:message => subscription.errors.full_messages.to_sentence
}
end
redirect_to subscriptions_url
end

# for Change Notifications
def update
subscription = Subscription.find(params[:id])
subscription.history = Array(subscription.history) + Array(params[:entry])
subscription.save
render :nothing => true
end

end
5 changes: 5 additions & 0 deletions app/models/facebook.rb
@@ -1,4 +1,5 @@
class Facebook < ActiveRecord::Base
has_many :subscriptions

def profile
@profile ||= FbGraph::User.me(self.access_token).fetch
Expand All @@ -21,6 +22,10 @@ def config
raise StandardError.new("config/facebook.yml could not be loaded.")
end

def app
FbGraph::Application.new config[:client_id], :secret => config[:client_secret]
end

def auth
FbGraph::Auth.new config[:client_id], config[:client_secret]
end
Expand Down
32 changes: 32 additions & 0 deletions app/models/subscription.rb
@@ -0,0 +1,32 @@
class Subscription < ActiveRecord::Base
belongs_to :facebook

validates :facebook, :object, :fields, :history_json, :verify_token, :presence => true

before_validation :setup, :on => :create

def history
JSON.parse(self.history_json)
end

def history=(history)
self.history_json = history.to_json
end

def subscribe!(callback)
Facebook.app.subscribe!(
:object => self.object,
:fields => self.fields,
:callback_url => callback,
:verify_token => self.verify_token
)
end

private

def setup
self.verify_token = ActiveSupport::SecureRandom.hex(16)
self.history = []
end

end
2 changes: 1 addition & 1 deletion app/views/dashboard/show.html.erb
Expand Up @@ -9,7 +9,7 @@
<p>Show your timeline and update status.</p>
</article>
<article>
<h3><%= link_to 'Subscription', subscription_path %></h3>
<h3><%= link_to 'Subscription', subscriptions_path %></h3>
<p>Subscribe your user's activity.</p>
<p>This functionality is for developers, not for end-users.</p>
<p>This page won't return any content to your browsers, since this endpoint is designed to respond to subscription verification request from facebook.com</p>
Expand Down
12 changes: 12 additions & 0 deletions app/views/subscriptions/_form.html.erb
@@ -0,0 +1,12 @@
<%= form_for Subscription.new, :url => subscriptions_path do |f| %>
<div>
<%= f.label :object %>
<%= f.select :object, [:user, :permissions, :page] %>
</div>
<div>
<%= f.label :fields %>
<%= f.text_field :fields, :placeholder => 'field1, field2, ..' %>
</div>
<%= f.submit %>
<% end %>

11 changes: 11 additions & 0 deletions app/views/subscriptions/_subscription.html.erb
@@ -0,0 +1,11 @@
<article>
<h2><%= subscription.object %></h2>
<p><%= subscription.fields %></p>
<ul>
<% subscription.history.each do |update| %>
<li>
<pre><%= JSON.pretty_generate update %></pre>
</li>
<% end %>
</ul>
</article>
2 changes: 2 additions & 0 deletions app/views/subscriptions/index.html.erb
@@ -0,0 +1,2 @@
<%= render 'subscriptions/form' %>
<%= render current_user.subscriptions %>
2 changes: 1 addition & 1 deletion config/routes.rb
Expand Up @@ -7,7 +7,7 @@
resource :canvas, :only => [:show, :create]
resource :profile, :only => :show
resource :timeline, :only => [:show, :create]
resource :subscription, :only => [:show, :create]
resources :subscriptions, :except => [:new, :edit, :destroy]

root :to => 'top#index'
end
2 changes: 1 addition & 1 deletion db/migrate/20100805043508_create_facebooks.rb
@@ -1,7 +1,7 @@
class CreateFacebooks < ActiveRecord::Migration
def self.up
create_table :facebooks do |t|
t.string :identifier, :unsigned => true, :limit => 20
t.string :identifier, :limit => 20
t.string :access_token
t.timestamps
end
Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20110623075710_create_subscriptions.rb
@@ -0,0 +1,14 @@
class CreateSubscriptions < ActiveRecord::Migration
def self.up
create_table :subscriptions do |t|
t.belongs_to :facebook
t.string :object, :fields, :verify_token
t.text :history_json
t.timestamps
end
end

def self.down
drop_table :subscriptions
end
end
12 changes: 11 additions & 1 deletion db/schema.rb
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20100805043508) do
ActiveRecord::Schema.define(:version => 20110623075710) do

create_table "facebooks", :force => true do |t|
t.string "identifier", :limit => 20
Expand All @@ -19,4 +19,14 @@
t.datetime "updated_at"
end

create_table "subscriptions", :force => true do |t|
t.integer "facebook_id"
t.string "object"
t.string "fields"
t.string "verify_token"
t.text "history_json"
t.datetime "created_at"
t.datetime "updated_at"
end

end
11 changes: 11 additions & 0 deletions test/fixtures/subscriptions.yml
@@ -0,0 +1,11 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
8 changes: 8 additions & 0 deletions test/unit/subscription_test.rb
@@ -0,0 +1,8 @@
require 'test_helper'

class SubscriptionTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
Empty file added tmp/restart.txt
Empty file.

0 comments on commit 7055af1

Please sign in to comment.