-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from Fullscreen/add-facebook
Add facebook
- Loading branch information
Showing
15 changed files
with
296 additions
and
2 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
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
require "net/version" | ||
require "net/twitter" | ||
require "net/instagram" | ||
require "net/facebook" | ||
|
||
module Net | ||
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 @@ | ||
require 'net/facebook/config' | ||
require 'net/facebook/models' | ||
require 'net/facebook/errors' | ||
|
||
module Net | ||
module Facebook | ||
extend Config | ||
include Errors | ||
include Models | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module Net | ||
module Facebook | ||
module Api | ||
class Configuration | ||
attr_accessor :client_id, :client_secret | ||
|
||
def initialize | ||
@client_id = ENV['FACEBOOK_CLIENT_ID'] | ||
@client_secret = ENV['FACEBOOK_CLIENT_SECRET'] | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
require 'net/facebook/errors/response_error' | ||
require 'active_support' | ||
require 'active_support/core_ext' | ||
|
||
module Net | ||
module Facebook | ||
module Api | ||
class Request | ||
def initialize(attrs = {}) | ||
@host = 'graph.facebook.com' | ||
@query = attrs[:params] if attrs[:params] | ||
@path = attrs.fetch :path, "/v2.3/#{@query}" | ||
@method = attrs.fetch :method, :get | ||
end | ||
|
||
def run | ||
case response = run_http_request | ||
when Net::HTTPOK | ||
JSON.parse(response.body) | ||
else | ||
raise Errors::ResponseError, response | ||
end | ||
end | ||
|
||
private | ||
def run_http_request | ||
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| | ||
http.request http_request | ||
end | ||
end | ||
|
||
def http_request | ||
http_class = "Net::HTTP::#{@method.capitalize}".constantize | ||
@http_request ||= http_class.new(uri.request_uri) | ||
end | ||
|
||
def uri | ||
@uri ||= URI::HTTPS.build host: @host, path: @path, query: query | ||
end | ||
|
||
def query | ||
{}.tap do |query| | ||
query.merge! access_token: "#{Net::Facebook.configuration.client_id}|#{Net::Facebook.configuration.client_secret}" | ||
end.to_param | ||
end | ||
|
||
def as_curl | ||
'curl'.tap do |curl| | ||
curl << " -X #{http_request.method}" | ||
http_request.each_header do |name, value| | ||
curl << %Q{ -H "#{name}: #{value}"} | ||
end | ||
curl << %Q{ -d '#{http_request.body}'} if http_request.body | ||
curl << %Q{ "#{@uri.to_s}"} | ||
end | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'net/facebook/api/configuration' | ||
|
||
module Net | ||
module Facebook | ||
module Config | ||
def configure | ||
yield configuration if block_given? | ||
end | ||
|
||
def configuration | ||
@configuration ||= Api::Configuration.new | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require 'net/facebook/errors/response_error' | ||
require 'net/facebook/errors/unknown_user' |
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 @@ | ||
module Net | ||
module Facebook | ||
module Errors | ||
class ResponseError < StandardError | ||
attr_reader :response | ||
|
||
def initialize(response = {}) | ||
@response = response | ||
super response | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Net | ||
module Facebook | ||
module Errors | ||
class UnknownUser < StandardError | ||
def message | ||
'Unknown user' | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'net/facebook/models/page' |
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,55 @@ | ||
require 'net/facebook/api/request' | ||
require 'net/facebook/errors' | ||
|
||
module Net | ||
module Facebook | ||
module Models | ||
class Page | ||
attr_reader :username, :likes | ||
|
||
def initialize(attrs = {}) | ||
@username = attrs['username'] | ||
@likes = attrs['likes'] | ||
end | ||
|
||
|
||
# Returns the existing Facebook page matching the provided attributes or | ||
# nil when the page is not found. | ||
# | ||
# @return [Net::Facebook::Models::Page] the Facebook page. | ||
# @ return [nil] when the page cannot be found. | ||
# @param [Hash] params the attributes to find a page by. | ||
# @option params [String] :username The Facebook page’s username | ||
# (case-insensitive). | ||
def self.find_by(params = {}) | ||
find_by! params | ||
rescue Errors::UnknownUser | ||
nil | ||
end | ||
|
||
# Returns the existing Facebook page matching the provided attributes or | ||
# raises an error when the page is not found. | ||
# | ||
# @return [Net::Facebook::Models::Page] the Facebook page. | ||
# @param [Hash] params the attributes to find a page by. | ||
# @option params [String] :username The Facebook page’s username | ||
# (case-insensitive). | ||
# @raise [Net::Errors::UnknownUser] if the page cannot be found. | ||
def self.find_by!(params = {}) | ||
find_by_username! params[:username] | ||
end | ||
|
||
private | ||
|
||
def self.find_by_username!(username) | ||
request = Api::Request.new params: username | ||
new request.run | ||
rescue Errors::ResponseError => error | ||
case error.response | ||
when Net::HTTPNotFound then raise Errors::UnknownUser | ||
end | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Net | ||
VERSION = "0.2.1" | ||
VERSION = "0.2.2" | ||
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,42 @@ | ||
require 'spec_helper' | ||
|
||
describe Net::Facebook::Page do | ||
let(:unknown_username) { 'qeqwe09qlkmhkjh' } | ||
let(:existing_username) { 'Fullscreeninc' } | ||
|
||
describe '.find_by' do | ||
subject(:page) { Net::Facebook::Page.find_by username: username } | ||
|
||
context 'given an existing (case-insensitive) username' do | ||
let (:username) { existing_username } | ||
|
||
it 'returns an object representing the page for that user' do | ||
expect(page.username).to eq 'fullscreeninc' | ||
expect(page.likes).to be_an Integer | ||
end | ||
end | ||
|
||
context 'given an unknown username' do | ||
let(:username) { unknown_username } | ||
it { expect(page).to be_nil } | ||
end | ||
end | ||
|
||
describe '.find_by!' do | ||
subject(:page) { Net::Facebook::Page.find_by! username: username } | ||
|
||
context 'given an existing (case-insensitive) username' do | ||
let(:username) { existing_username } | ||
|
||
it 'returns an object representing the page for that user' do | ||
expect(page.username).to eq 'fullscreeninc' | ||
expect(page.likes).to be_an Integer | ||
end | ||
end | ||
|
||
context 'given an unknown username' do | ||
let(:username) { unknown_username } | ||
it { expect{page}.to raise_error Net::Facebook::UnknownUser } | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,5 @@ | |
interaction.response.headers['X-Rate-Limit-Reset'].first | ||
end | ||
end | ||
c.ignore_hosts 'graph.facebook.com' | ||
end |