Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ For more information about changelogs, check

* [FEATURE] Added `Fb::User`
* [FEATURE] Added `Fb::Page`
* [FEATURE] Added `Fb::Page#posts`
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,25 @@ user.email # => 'john.smith@example.com'
Fb::User#pages
--------------

Given an user access token with the `manage_pages` scope, you can get the list of Facebook pages managed by the user by calling:
Given an user access token with the `pages_show_list` scope, you can get the list of Facebook pages managed by the user by calling:

```ruby
user = Fb::User.new access_token: '--valid-access-token--'
user.pages
# => [#<Fb::Page: id="1234", name="sample1">, #<Fb::Page: id="5678", name="sample2">]
```

Fb::Page#posts
--------------

Given a page with posts, you can get the posts on the page since creation by calling:

```ruby
user = Fb::User.new access_token: '--valid-access-token--'
user.pages.first.posts
# => [#<Fb::Post: id="1234", type="sample1">, #<Fb::Post: id="5678", type="sample2">]
```

## Development

To run tests, obtain a long-term access token for a Facebook user who manages
Expand Down
3 changes: 2 additions & 1 deletion lib/fb/core.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require 'fb/support'
require 'fb/page'
require 'fb/user'

require 'fb/post'
require 'fb/data_helper'
# An object-oriented Ruby client for the Facebook Graph API.
# @see http://www.rubydoc.info/gems/fb-core/
module Fb
Expand Down
2 changes: 1 addition & 1 deletion lib/fb/core/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module Fb
class Core
# @return [String] the SemVer-compatible gem version.
# @see http://semver.org
VERSION = '1.0.0.alpha2'
VERSION = '1.0.0.alpha3'
end
end
10 changes: 10 additions & 0 deletions lib/fb/data_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Fb
# @private
class DataHelper
def self.symbolize_keys(hash)
{}.tap do |new_hash|
hash.each_key{|key| new_hash[key.to_sym] = hash[key]}
end
end
end
end
38 changes: 38 additions & 0 deletions lib/fb/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,47 @@ def initialize(options = {})
@category = options[:category]
end

# @return [Array<Fb::Post>] a collection of posts.
def posts
@posts ||= begin
fetch_posts.map do |post_data|
Fb::Post.new DataHelper.symbolize_keys(post_data)
end
end
end

# @return [String] the representation of the page.
def to_s
%Q(#<#{self.class.name} #{@id} "#{@name}">)
end

private

def fetch_posts
result = []
response = HTTPRequest.new(path: "/v2.9/#{@id}/posts",
params: posts_params).run.body
loop do
if response["paging"].key?("next")
result << response["data"]
next_page = response["paging"]["cursors"]["after"]
paging_params = posts_params.merge(after: next_page)
response = HTTPRequest.new(path: "/v2.9/#{@id}/posts",
params: paging_params).run.body
else
result << response["data"]
break
end
end
result.flatten
end

def posts_params
{
fields: 'type,created_time,message,story,permalink_url',
limit: 100,
access_token: ENV['FB_TEST_ACCESS_TOKEN']
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@claudiofullscreen how do we take care of access_token?

}
end
end
end
34 changes: 34 additions & 0 deletions lib/fb/post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Ruby client to authenticate a Facebook user.
# @see http://www.rubydoc.info/gems/Fb/
module Fb
# Fb::Post reprensents a Facebook post. Post provides getters for:
# :id, :title, :url, :created_time, and :type.
class Post
attr_reader :id, :title, :url, :created_time, :type

# @param [Hash] options the options to initialize an instance of Fb::Post.
# @option [String] :id The post id.
# @option [String] :title The status message in the post or post story.
# @option [String] :url URL to the permalink page of the post.
# @option [String] :created_time The time the post was initially published.
# @option [String] :type A string indicating the object type of this post.
def initialize(options = {})
@id = options[:id]
@url = options[:permalink_url]
@created_time = options[:created_time]
@type = options[:type]
@title = options[:message] || options[:story] || build_title
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes neither is available, so I give it a title.

end

# @return [String] the representation of the post.
def to_s
%Q(#<#{self.class.name} #{@id} "#{@type}">)
end

private

def build_title
"#{@type} posted #{@created_time} available at #{@url}"
end
end
end
10 changes: 1 addition & 9 deletions lib/fb/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,9 @@ def pages
params = {access_token: @access_token}
request = HTTPRequest.new path: '/me/accounts', params: params
request.run.body['data'].map do |page_data|
Page.new symbolize_keys(page_data)
Page.new DataHelper.symbolize_keys(page_data)
end
end
end

private

def symbolize_keys(hash)
{}.tap do |new_hash|
hash.each_key{|key| new_hash[key.to_sym] = hash[key]}
end
end
end
end
17 changes: 17 additions & 0 deletions spec/page/posts_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'spec_helper'

RSpec.describe 'Fb::Page#posts' do
context 'given valid options and an existing page with over 100 posts' do
let(:posts) { Fb::Page.new(id: '221406534569729').posts }

it 'returns an array of posts' do
expect(posts).to be_a(Array)
expect(posts).to all (be_a Fb::Post)
expect(posts.map &:id).to all(be_a String)
expect(posts.map &:url).to all(be_a String)
expect(posts.map &:type).to all(be_a String)
expect(posts.map &:created_time).to all(be_a String)
expect(posts.map &:title).to all(be_a String)
end
end
end
9 changes: 9 additions & 0 deletions spec/post/to_s_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'spec_helper'

RSpec.describe 'Fb::Post#to_s' do
let(:post) { Fb::Post.new id: 123, type: 'video' }

it 'returns a pretty string representation' do
expect(post.to_s).to eq '#<Fb::Post 123 "video">'
end
end