Skip to content

Commit 78347bf

Browse files
committed
Add implementation of facebook api
1 parent f246a2e commit 78347bf

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

lib/statistics/client.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,30 @@ def fetch_events(group_id:, since_at: @default_since, until_at: @default_until)
101101
events
102102
end
103103
end
104+
105+
class Facebook
106+
def initialize
107+
@client = Koala::Facebook::API.new(ENV.fetch('FACEBOOK_ACCESS_TOKEN'))
108+
end
109+
110+
def fetch_events(group_id:, since_at: nil, until_at: nil)
111+
params = {
112+
fields: %i(attending_count start_time owner),
113+
limit: 100,
114+
since: since_at,
115+
until: until_at
116+
}.compact!
117+
118+
events = []
119+
120+
collection = @client.get_object("#{group_id}/events", params)
121+
events.push(*collection.to_a)
122+
while collection.paging['next'] do
123+
events.push(*collection.next_page.to_a)
124+
end
125+
126+
events
127+
end
128+
end
104129
end
105130
end

spec/lib/statistics/client_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,19 @@
5656
end
5757
end
5858
end
59+
60+
context 'Facebook' do
61+
include_context 'Use stubs for Facebook'
62+
63+
describe '#fetch_events' do
64+
subject { Statistics::Client::Facebook.new.fetch_events(group_id: 123451234512345) }
65+
66+
it do
67+
expect(subject).to be_instance_of(Array)
68+
expect(subject.size).to eq 1
69+
expect(subject.first['id']).to eq '125500978166443'
70+
expect(subject.first.dig('owner', 'id')).to eq '123451234512345'
71+
end
72+
end
73+
end
5974
end

spec/support/shared_contexts/statistics.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,24 @@
4141
]
4242
end
4343
end
44+
45+
RSpec.shared_context 'Use stubs for Facebook' do
46+
let(:facebook_response) do
47+
resp = OpenStruct.new(data: {
48+
"data" => [
49+
{"attending_count"=>1, "start_time"=>"2017-10-29T13:00:00+0900", "owner"=>{"name"=>"CoderDojo ひばりヶ丘", "id"=>"123451234512345"}, "id"=>"125500978166443"}
50+
],
51+
"paging" => {
52+
"cursors" => {
53+
"before" => "QVFIUjVOd2tKSmZA6S01fR0NFNWN2aFJlc01JUnpqRW5aMFFkeHdBS3NTcUt1b3JfazUzM3FtVGhCYlN6bE1OS1lxZAzQ0YjVSNWRRVWRfd182SXh3LUN6VXZAB",
54+
"after" => "QVFIUmZA1cEk5QlV0VFc2Ri1BT3JEOWl3M1gzemRZAZAkpkaFdjNTEwUDdtaERLdFpwYV9CejVuX3hLV2kyVm5Gem9KSTAzTGg0dUd4SjNLXzBlSTZAJMVAtdmln"
55+
}
56+
}
57+
})
58+
Koala::Facebook::API::GraphCollection.new(resp, nil)
59+
end
60+
61+
before do
62+
allow_any_instance_of(Koala::Facebook::API).to receive(:get_object).and_return(facebook_response)
63+
end
64+
end

0 commit comments

Comments
 (0)