added a fb_status model & parser method
This commit is contained in:
parent
82b99decbb
commit
bb58678556
4 changed files with 161 additions and 0 deletions
30
app/models/fb_status.rb
Normal file
30
app/models/fb_status.rb
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
# Copyright (c) 2010, Diaspora Inc. This file is
|
||||
# licensed under the Affero General Public License version 3. See
|
||||
# the COPYRIGHT file.
|
||||
|
||||
|
||||
class FbStatus
|
||||
include MongoMapper::Document
|
||||
|
||||
key :graph_id, String
|
||||
key :author_id, String
|
||||
key :author_name, String
|
||||
key :message, String
|
||||
key :updated_time, DateTime
|
||||
|
||||
timestamps!
|
||||
|
||||
validates_presence_of :graph_id,:author_id,:author_name,:message,:updated_time
|
||||
|
||||
def self.from_api(json)
|
||||
hash = JSON.parse(json)
|
||||
self.create(
|
||||
:graph_id => hash['id'],
|
||||
:author_id => hash['from']['id']
|
||||
:author_name => hash['from']['name'],
|
||||
:message => hash['message']
|
||||
:updated_time => Time.parse(hash['updated_time']
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
|
@ -69,3 +69,11 @@ Factory.define :photo do |p|
|
|||
end
|
||||
|
||||
Factory.define(:comment) {}
|
||||
|
||||
Factory.define :fb_status do |s|
|
||||
s.graph_id "367501354973"
|
||||
s.author_name "Bret Taylor"
|
||||
s.author_id "220439"
|
||||
s.message "Pigs run from our house in fear. Tonight, I am wrapping the pork tenderloin in bacon and putting pancetta in the corn."
|
||||
s.updated_time Time.parse "2010-03-06T02:57:48+0000"
|
||||
end
|
||||
|
|
|
|||
79
spec/fixtures/fb_status
vendored
Normal file
79
spec/fixtures/fb_status
vendored
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
{
|
||||
"id": "367501354973",
|
||||
"from": {
|
||||
"name": "Bret Taylor",
|
||||
"id": "220439"
|
||||
},
|
||||
"message": "Pigs run from our house in fear. Tonight, I am wrapping the pork tenderloin in bacon and putting pancetta in the corn.",
|
||||
"updated_time": "2010-03-06T02:57:48+0000",
|
||||
"likes": {
|
||||
"data": [
|
||||
{
|
||||
"id": "29906278",
|
||||
"name": "Ross Miller"
|
||||
}
|
||||
]
|
||||
},
|
||||
"comments": {
|
||||
"data": [
|
||||
{
|
||||
"id": "367501354973_12216733",
|
||||
"from": {
|
||||
"name": "Doug Edwards",
|
||||
"id": "628675309"
|
||||
},
|
||||
"message": "Make sure you don't, as they say, go whole hog...\nhttp://www.youtube.com/watch?v=U4wTFuaV8VQ",
|
||||
"created_time": "2010-03-06T03:24:46+0000"
|
||||
},
|
||||
{
|
||||
"id": "367501354973_12249673",
|
||||
"from": {
|
||||
"name": "Tom Taylor",
|
||||
"id": "1249191863"
|
||||
},
|
||||
"message": "Are you and Karen gonna, as they say, pig out?",
|
||||
"created_time": "2010-03-06T21:05:21+0000"
|
||||
},
|
||||
{
|
||||
"id": "367501354973_12249857",
|
||||
"from": {
|
||||
"name": "Sheila Taylor",
|
||||
"id": "1315606682"
|
||||
},
|
||||
"message": "how did it turn out? Sounds nummy!\n",
|
||||
"created_time": "2010-03-06T21:10:30+0000"
|
||||
},
|
||||
{
|
||||
"id": "367501354973_12250973",
|
||||
"from": {
|
||||
"name": "Bret Taylor",
|
||||
"id": "220439"
|
||||
},
|
||||
"message": "Mom: turned out well. Sauce was good as well: pan sauce with mustard, balsamic, onion, and maple syrup. Surprisingly good in the end, and not too sweet, which was my concern. ",
|
||||
"created_time": "2010-03-06T21:44:53+0000"
|
||||
},
|
||||
{
|
||||
"id": "367501354973_12251276",
|
||||
"from": {
|
||||
"name": "Sheila Taylor",
|
||||
"id": "1315606682"
|
||||
},
|
||||
"message": "Sounds delicious! I probably would have skipped the maple syrup, but I am not married to a Canadian :)\n\nSheila Taylor\n(925) 818-7795\nP.O. Box 938\nGlen Ellen, CA 95442\nwww.winecountrytrekking.com",
|
||||
"created_time": "2010-03-06T21:55:12+0000"
|
||||
},
|
||||
{
|
||||
"id": "367501354973_12264435",
|
||||
"from": {
|
||||
"name": "Amelia Ann Arapoff",
|
||||
"id": "1580390378"
|
||||
},
|
||||
"message": "Bacon is always in our refrigerator, in case of need, somehow there is always need.",
|
||||
"created_time": "2010-03-07T05:11:52+0000"
|
||||
}
|
||||
],
|
||||
"paging": {
|
||||
"previous": "https://graph.facebook.com/367501354973/comments?access_token=2227470867%7C2.wNlupL2HPsOtrlYFBF56NA__.3600.1285354800-100001548997697%7C8lZ_pdgNDvSHYS4o1OkqhdQu6mA&limit=25&since=2010-03-07T05%3A11%3A52%2B0000",
|
||||
"next": "https://graph.facebook.com/367501354973/comments?access_token=2227470867%7C2.wNlupL2HPsOtrlYFBF56NA__.3600.1285354800-100001548997697%7C8lZ_pdgNDvSHYS4o1OkqhdQu6mA&limit=25&until=2010-03-06T03%3A24%3A45%2B0000"
|
||||
}
|
||||
}
|
||||
}
|
||||
44
spec/models/fb_status_spec.rb
Normal file
44
spec/models/fb_status_spec.rb
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
# Copyright (c) 2010, Diaspora Inc. This file is
|
||||
# licensed under the Affero General Public License version 3. See
|
||||
# the COPYRIGHT file.
|
||||
|
||||
|
||||
|
||||
require File.dirname(__FILE__) + '/../spec_helper'
|
||||
require 'json'
|
||||
|
||||
describe FbStatus do
|
||||
|
||||
let(:fb_status) { Factory.create :fb_status }
|
||||
|
||||
it 'is valid' do
|
||||
fb_status.should be_valid
|
||||
end
|
||||
|
||||
describe '#from_api' do
|
||||
let(:json_string) {File.open(File.dirname(__FILE__) + '/../fixtures/fb_status').read}
|
||||
let!(:json_object) { JSON.parse(json_string) }
|
||||
let!(:status_from_json) {FbStatus.from_api(json_string)}
|
||||
|
||||
it 'has graph_id' do
|
||||
status_from_json.graph_id.should == json_object['id']
|
||||
end
|
||||
|
||||
it 'has author_id' do
|
||||
status_from_json.graph_id.should == json_object['from']['id']
|
||||
end
|
||||
|
||||
it 'has author_name' do
|
||||
status_from_json.graph_id.should == json_object['from']['name']
|
||||
end
|
||||
|
||||
it 'has message' do
|
||||
status_from_json.message.should == json_object['message']
|
||||
end
|
||||
|
||||
it 'has author_name' do
|
||||
status_from_json.updated_time.should == Time.parse(json_object['updated_time'])
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
Loading…
Reference in a new issue