Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
138 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<%= render 'subscriptions/form' %> | ||
<%= render current_user.subscriptions %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.