Skip to content

Commit

Permalink
NEW: add get_object and get_object_edge (#65)
Browse files Browse the repository at this point in the history
Add two new functions to work with generic objects from the Facebook Graph.

This is joint work with @bugduino
  • Loading branch information
Matteo Centenaro authored and mweibel committed Jul 3, 2018
1 parent caf4435 commit dddc227
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 12 deletions.
67 changes: 55 additions & 12 deletions lib/facebook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,59 @@ defmodule Facebook do
page(page_id, access_token, ["fan_count"])
end

@doc """
Basic Graph object information by object ID
## Example
iex> Facebook.get_object("1234567", "<Access Token>")
{:ok, %{"id" => id}}
"""
@spec get_object(object_id :: String.t, access_token) :: resp
def get_object(object_id, access_token) do
get_object(object_id, access_token, [])
end

@doc """
Get Graph object information for the specified params for the provided object ID
## Example
iex> Facebook.get_object("1234567", "<Access Token>", [fields: "id,name"])
{:ok, %{"id" => id, "name" => name}
See: https://developers.facebook.com/docs/graph-api/reference/page
"""
@spec get_object(object_id, access_token, params) :: resp
def get_object(object_id, access_token, params) do
params = params
|> add_app_secret(access_token)
|> add_access_token(access_token)

~s(/#{object_id})
|> GraphAPI.get([], params: params)
|> ResponseFormatter.format_response
end

@doc """
Gets an object edge for a specific object ID
## Examples
iex> Facebook.get_object_edge(:adlabels, "act_12345", "<Access Token>")
iex> Facebook.page_feed(:leads, "1223344332", "<Access Token>", [fields: "created_time,id,ad_id,form_id,field_data"])
{:ok, %{"data" => [...]}}
"""
# credo:disable-for-lines:1 Credo.Check.Readability.MaxLineLength
@spec get_object_edge(edge :: atom | String.t, object_id :: String.t, access_token, params) :: resp
def get_object_edge(edge, object_id, access_token, params \\ []) do
params = params
|> add_app_secret(access_token)
|> add_access_token(access_token)

~s(/#{object_id}/#{edge})
|> GraphAPI.get([], params: params)
|> ResponseFormatter.format_response
end

@doc """
Basic page information for the provided `t:page_id/0`
Expand All @@ -398,12 +451,7 @@ defmodule Facebook do
@spec page(page_id, access_token, fields) :: resp
def page(page_id, access_token, fields) do
params = [fields: Enum.join(fields, ",")]
|> add_app_secret(access_token)
|> add_access_token(access_token)

~s(/#{page_id})
|> GraphAPI.get([], params: params)
|> ResponseFormatter.format_response
get_object(page_id, access_token, params)
end

@doc """
Expand Down Expand Up @@ -435,12 +483,7 @@ defmodule Facebook do
@spec page_feed(scope, page_id, access_token, limit, fields :: String.t) :: resp
def page_feed(scope, page_id, access_token, limit \\ 25, fields \\ "") when limit <= 100 do
params = [limit: limit, fields: fields]
|> add_app_secret(access_token)
|> add_access_token(access_token)

~s(/#{page_id}/#{scope})
|> GraphAPI.get([], params: params)
|> ResponseFormatter.format_response
get_object_edge(scope, page_id, access_token, params)
end

@doc """
Expand Down
81 changes: 81 additions & 0 deletions test/facebook_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,87 @@ defmodule FacebookTest do
end
end

describe "get_object" do
test "success", %{app_access_token: app_access_token} do
with_mock :hackney, GraphMock.mock_options(
fn(_) -> GraphMock.get_object(:success) end
) do
assert {:ok, %{
"id" => id
}} = Facebook.get_object("1234567", app_access_token)

assert(String.length(id) > 0)
end
end

test "error", %{invalid_access_token: invalid_access_token} do
with_mock :hackney, GraphMock.mock_options(
fn(_) -> GraphMock.error() end
) do
assert {:error, _} = Facebook.get_object("1234567", invalid_access_token)
end
end
end

describe "get_object with fields" do
test "success", %{app_access_token: app_access_token} do
with_mock :hackney, GraphMock.mock_options(
fn(_) -> GraphMock.get_object(:success, :with_fields) end
) do
assert {:ok, %{
"id" => id,
"name" => name
}} = Facebook.get_object("1234567", app_access_token, [fields: "name"])

assert(String.length(id) > 0)
assert(String.length(name) > 0)
end
end

test "error", %{invalid_access_token: invalid_access_token} do
with_mock :hackney, GraphMock.mock_options(
fn(_) -> GraphMock.error() end
) do
assert {:error, _} = Facebook.get_object(
"1234567",
invalid_access_token,
[fields: "name"]
)
end
end
end

describe "get_object_edge" do
test "success", %{app_access_token: app_access_token} do
with_mock :hackney, GraphMock.mock_options(
fn(_) -> GraphMock.get_object_edge(:success) end
) do
assert {:ok, %{"data" => [data | _]}} = Facebook.get_object_edge(
:adlabels,
"act_123456",
app_access_token
)

assert %{
"id" => _,
"name" => _
} = data
end
end

test "error", %{invalid_access_token: invalid_access_token} do
with_mock :hackney, GraphMock.mock_options(
fn(_) -> GraphMock.error() end
) do
assert {:error, _} = Facebook.get_object_edge(
:adlabels,
"act_123456",
invalid_access_token
)
end
end
end

describe "page" do
test "success", %{app_access_token: app_access_token} do
with_mock :hackney, GraphMock.mock_options(
Expand Down
19 changes: 19 additions & 0 deletions test/graph_mock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ defmodule Facebook.GraphMock do
})
end

def get_object(:success) do
JSON.encode(%{
"id": "1234567"
})
end

def get_object(:success, :with_fields) do
JSON.encode(%{
"id": "1234567",
"name": "name"
})
end

def get_object_edge(:success) do
JSON.encode(%{"data": [
%{"id": "1234567", "name": "label1"}
]})
end

def page(:success) do
JSON.encode(%{
"id": "19292868552", "name": "Facebook for Developers"
Expand Down

0 comments on commit dddc227

Please sign in to comment.