From 4befaaee82d04ccb78924463ed1659f143b5a8d7 Mon Sep 17 00:00:00 2001 From: nov Date: Mon, 1 Sep 2014 13:04:28 +0900 Subject: [PATCH] page spec --- lib/fb_graph2/collection.rb | 3 +- spec/fb_graph2/page_spec.rb | 61 +++++++++++++++++++ spec/mock_json/page/blocked.json | 12 ++-- spec/mock_json/page/with_optional_fields.json | 42 +++++++++++++ 4 files changed, 110 insertions(+), 8 deletions(-) create mode 100644 spec/fb_graph2/page_spec.rb create mode 100644 spec/mock_json/page/with_optional_fields.json diff --git a/lib/fb_graph2/collection.rb b/lib/fb_graph2/collection.rb index 011c760..c162586 100644 --- a/lib/fb_graph2/collection.rb +++ b/lib/fb_graph2/collection.rb @@ -1,6 +1,6 @@ module FbGraph2 class Collection < Array - attr_accessor :before, :after, :next, :previous, :order, :total_count + attr_accessor :before, :after, :next, :previous, :order, :total_count, :summary def initialize(collection = []) collection = normalize collection @@ -34,6 +34,7 @@ module FbGraph2 end def summarize(summary) + self.summary = summary self.order = summary.try(:[], :order) self.total_count = summary.try(:[], :total_count) end diff --git a/spec/fb_graph2/page_spec.rb b/spec/fb_graph2/page_spec.rb new file mode 100644 index 0000000..33ac439 --- /dev/null +++ b/spec/fb_graph2/page_spec.rb @@ -0,0 +1,61 @@ +require 'spec_helper' + +describe FbGraph2::Page do + context 'with optional fields' do + subject { page } + let(:page) do + attributes = mock_json 'page/with_optional_fields' + FbGraph2::Page.new attributes[:id], attributes + end + + describe '#context' do + subject { page.context } + it { should be_instance_of FbGraph2::Struct::Context::PageContext } + + describe 'friends_who_like' do + subject { page.context.friends_who_like } + it { should be_instance_of FbGraph2::Collection } + it { should be_blank } + its(:total_count) { should == 14 } + its(:summary) { should include :social_sentence } + end + end + + describe '#parking' do + subject { page.parking } + [:street, :lot, :valet].each do |key| + its(key) { should == false } + end + end + + describe '#restaurant_services' do + subject { page.restaurant_services } + [:kids, :delivery, :catering, :waiter, :outdoor, :takeout].each do |key| + its(key) { should == false } + end + [:groups, :reserve, :walkins].each do |key| + its(key) { should == true } + end + end + + describe '#restaurant_specialties' do + subject { page.restaurant_specialties } + [:coffee, :drinks, :breakfast, :lunch].each do |key| + its(key) { should == false } + end + [:dinner].each do |key| + its(key) { should == true } + end + end + + describe '#payment_options' do + subject { page.payment_options } + [:cash_only, :discover].each do |key| + its(key) { should == false } + end + [:amex, :mastercard, :visa].each do |key| + its(key) { should == true } + end + end + end +end \ No newline at end of file diff --git a/spec/mock_json/page/blocked.json b/spec/mock_json/page/blocked.json index a875a08..2f9299f 100644 --- a/spec/mock_json/page/blocked.json +++ b/spec/mock_json/page/blocked.json @@ -1,11 +1,9 @@ { - "data": [ - { - "name": "Jr Nov", - "id": "1575327134" - } - ], + "data": [{ + "name": "Jr Nov", + "id": "1575327134" + }], "paging": { "next": "https://graph.facebook.com/v2.0/117513961602338/blocked?limit=5000&offset=5000&__after_id=enc_AewU3hSo5zqbS5KA0i0lJYmPrLn57SMSo645aYB7BTPg1PSawPvlM9mRI_3Y_dcpeDI" } -} \ No newline at end of file +} diff --git a/spec/mock_json/page/with_optional_fields.json b/spec/mock_json/page/with_optional_fields.json new file mode 100644 index 0000000..8d585cb --- /dev/null +++ b/spec/mock_json/page/with_optional_fields.json @@ -0,0 +1,42 @@ +{ + "context": { + "friends_who_like": { + "data": [], + "summary": { + "social_sentence": "14 of your friends like this.", + "total_count": 14 + } + } + }, + "parking": { + "lot": 0, + "street": 0, + "valet": 0 + }, + "restaurant_services": { + "delivery": 0, + "catering": 0, + "groups": 1, + "kids": 0, + "outdoor": 0, + "reserve": 1, + "takeout": 0, + "waiter": 0, + "walkins": 1 + }, + "restaurant_specialties": { + "breakfast": 0, + "coffee": 0, + "dinner": 1, + "drinks": 0, + "lunch": 0 + }, + "payment_options": { + "amex": 1, + "cash_only": 0, + "discover": 0, + "mastercard": 1, + "visa": 1 + }, + "id": "139296106095829" +}