Skip to content

Commit

Permalink
Merge 54d1665 into a8935c2
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanhuntesl committed Nov 28, 2017
2 parents a8935c2 + 54d1665 commit 7e161b5
Show file tree
Hide file tree
Showing 46 changed files with 214 additions and 210 deletions.
16 changes: 8 additions & 8 deletions lib/tentacat.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ defmodule Tentacat do

@user_agent [{"User-agent", "tentacat"}]

@type response :: {integer, any} | :jsx.json_term
@type response :: {:ok, :jsx.json_term, HTTPoison.Response.t } | {integer, any, HTTPoison.Response.t}

@spec process_response_body(binary) :: term
def process_response_body(""), do: nil
def process_response_body(body), do: JSX.decode!(body)

@spec process_response(HTTPoison.Response.t) :: response
def process_response(%HTTPoison.Response{status_code: 200, body: body}), do: body
def process_response(%HTTPoison.Response{status_code: status_code, body: body }), do: { status_code, body }
def process_response(%HTTPoison.Response{status_code: status_code, body: body} = resp), do: { status_code, body,resp}

def delete(path, client, body \\ "") do
_request(:delete, url(client, path), client.auth, body)
Expand Down Expand Up @@ -92,7 +91,7 @@ defmodule Tentacat do
request_with_pagination(method, url, auth, JSX.encode!(body))
|> stream_if_needed(override)
end
defp stream_if_needed(result = {status_code, _}, _) when is_number(status_code), do: result
defp stream_if_needed(result = {status_code, _, _}, _) when is_number(status_code), do: result
defp stream_if_needed({body, nil, _}, _), do: body
defp stream_if_needed({body, _, _}, :one_page), do: body
defp stream_if_needed(initial_results, _) do
Expand All @@ -110,7 +109,7 @@ defmodule Tentacat do
request_with_pagination(:get, next, auth, "")
|> process_stream
end
defp process_stream({items, next, auth}) when is_list(items) do
defp process_stream({{_,items,_}, next, auth}) when is_list(items) do
{items, {[], next, auth}}
end
defp process_stream({item, next, auth}) do
Expand All @@ -121,15 +120,16 @@ defmodule Tentacat do
def request_with_pagination(method, url, auth, body \\ "") do
resp = request!(method, url, JSX.encode!(body), authorization_header(auth, extra_headers() ++ @user_agent), extra_options())
case process_response(resp) do
{status, _} when status in [301, 302, 307] ->
{status, _, _ } when status in [301, 302, 307] ->
request_with_pagination(method, location_header(resp), auth)
x when is_tuple(x) -> x
# never seems to hit this block...
# x when is_tuple(x) -> x
_ -> pagination_tuple(resp, auth)
end
end

@spec pagination_tuple(HTTPoison.Response.t, Client.auth) :: {binary, binary, Client.auth}
defp pagination_tuple(%HTTPoison.Response{headers: headers} = resp, auth) do
defp pagination_tuple(%HTTPoison.Response{:headers => headers} = resp, auth) do
{process_response(resp), next_link(headers), auth}
end

Expand Down
6 changes: 3 additions & 3 deletions lib/tentacat/followers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ defmodule Tentacat.Users.Followers do

defp following_check(following_api_url, client) do
case get following_api_url, client do
{ 204, _ } -> true
{ 404, _ } -> false
{ 204, _ ,resp} -> {204, true, resp}
{ 404, _ ,resp} -> {404,false, resp}
unexpected_response -> unexpected_response
end
end
Expand Down Expand Up @@ -124,7 +124,7 @@ defmodule Tentacat.Users.Followers do

defp follow_response(response) do
case response do
{ 204, _ } -> true
{ 204, _ , resp} -> {204, true, resp}
unexpected_response -> unexpected_response
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/tentacat/starring.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ defmodule Tentacat.Users.Starring do

defp starred_check(starred_api_url, client) do
case get starred_api_url, client do
{ 204, _ } -> true
{ 404, _ } -> false
{ 204, _, resp } -> {204, true, resp}
{ 404, _, resp } -> {404, false,resp}
unexpected_response -> unexpected_response
end
end
Expand Down Expand Up @@ -82,7 +82,7 @@ defmodule Tentacat.Users.Starring do

defp star_response(response) do
case response do
{ 204, _ } -> true
{ 204, _, resp } -> {204, true, resp}
unexpected_response -> unexpected_response
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/app/installations_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ defmodule Tentacat.App.InstallationsTest do

test "list_mine/1" do
use_cassette "app/installations#list_mine" do
assert length(list_mine(@client)) == 1
assert length( elem(list_mine(@client),1) ) == 1
end
end

test "find/2" do
use_cassette "app/installations#find" do
assert find(66216, @client)["id"] == 66216
assert elem(find(66216, @client),1)["id"] == 66216
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/app_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule Tentacat.AppTest do

test "me/1" do
use_cassette "app#me" do
assert me(@client)["name"] == "tentacatty"
assert elem(me(@client),1)["name"] == "tentacatty"
end
end
end
12 changes: 6 additions & 6 deletions test/commits/comments_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ defmodule Tentacat.Commits.CommentsTest do

test "list_all/3" do
use_cassette "commits/comments#list_all" do
assert list_all("tentatest", "tentacat", @client) == []
assert {200,[],_} = list_all("tentatest", "tentacat", @client)
end
end

test "list/4" do
use_cassette "commits/comments#list" do
assert list("soudqwiggle", "elixir-conspiracy", "b426f957a26cd4d632da3b174372c973ab083523", @client) == []
assert {200,[],_} = list("soudqwiggle", "elixir-conspiracy", "b426f957a26cd4d632da3b174372c973ab083523", @client)
end
end

test "find/4" do
use_cassette "commits/comments#find" do
%{"body" => body} = find("soudqwiggle", "elixir-conspiracy", 15079374, @client)
{_,%{"body" => body},_} = find("soudqwiggle", "elixir-conspiracy", 15079374, @client)
assert body == ":sheep: :lv:"
end
end
Expand All @@ -37,7 +37,7 @@ defmodule Tentacat.Commits.CommentsTest do
"position" => 1
}
use_cassette "commits/comments#create" do
{status_code, _} = create("soudqwiggle", "elixir-conspiracy", "b426f957a26cd4d632da3b174372c973ab083523", body, @client)
{status_code, _, _ } = create("soudqwiggle", "elixir-conspiracy", "b426f957a26cd4d632da3b174372c973ab083523", body, @client)
assert status_code == 201
end
end
Expand All @@ -47,14 +47,14 @@ defmodule Tentacat.Commits.CommentsTest do
"body" => ":sheep: :lv:"
}
use_cassette "commits/comments#update" do
%{"id" => commit_id} = update("soudqwiggle", "elixir-conspiracy", 15079374, body, @client)
{_, %{"id" => commit_id},_ } = update("soudqwiggle", "elixir-conspiracy", 15079374, body, @client)
assert commit_id == 15079374
end
end

test "delete/4" do
use_cassette "commits/comments#delete" do
{status_code, _} = delete("soudqwiggle", "elixir-conspiracy", 15079374, @client)
{status_code, _, _ } = delete("soudqwiggle", "elixir-conspiracy", 15079374, @client)
assert status_code == 204
end
end
Expand Down
14 changes: 7 additions & 7 deletions test/commits_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,33 @@ defmodule Tentacat.CommitsTest do

@client Tentacat.Client.new(%{access_token: "yourtokencomeshere"})

setup_all do
HTTPoison.start
end
# setup_all do
# HTTPoison.start
# end

test "list/3" do
use_cassette "commits#list" do
assert list("soudqwiggle", "elixir-conspiracy", @client) == []
assert {200,[],_} = list("soudqwiggle", "elixir-conspiracy", @client)
end
end

test "filter/4" do
use_cassette "commits#filter" do
[%{"sha" => sha}] = filter("soudqwiggle", "elixir-conspiracy", %{sha: "09fe12ca25d0440f143ab331e4684a8622d6e4e5"}, @client)
{_,[%{"sha" => sha}],_} = filter("soudqwiggle", "elixir-conspiracy", %{sha: "09fe12ca25d0440f143ab331e4684a8622d6e4e5"}, @client)
assert sha == "09fe12ca25d0440f143ab331e4684a8622d6e4e5"
end
end

test "find/4" do
use_cassette "commits#find" do
%{"sha" => sha} = find("09fe12ca25d0440f", "soudqwiggle", "elixir-conspiracy", @client)
{_,%{"sha" => sha},_} = find("09fe12ca25d0440f", "soudqwiggle", "elixir-conspiracy", @client)
assert sha == "09fe12ca25d0440f143ab331e4684a8622d6e4e5"
end
end

test "compare/5" do
use_cassette "commits#compare" do
%{"total_commits" => total_commits} = compare("master", "09fe12ca25d0440f", "soudqwiggle", "elixir-conspiracy", @client)
{_,%{"total_commits" => total_commits},_} = compare("master", "09fe12ca25d0440f", "soudqwiggle", "elixir-conspiracy", @client)
assert total_commits == 0
end
end
Expand Down
10 changes: 5 additions & 5 deletions test/contents_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ defmodule Tentacat.ContentsTest do

test "find/4" do
use_cassette "contents#find" do
[%{"sha" => sha}] = find("elixir-lang", "elixir", "lib", @client)
{_,[%{"sha" => sha}],_} = find("elixir-lang", "elixir", "lib", @client)
assert sha == "c928b5d4c7fa3c0c665cb2d2f591def3bdf29c38"
end
end

test "find_in/5" do
use_cassette "contents#find_in" do
[%{"sha" => sha}] = find_in("elixir-lang", "elixir", "lib", "master", @client)
{_,[%{"sha" => sha}],_} = find_in("elixir-lang", "elixir", "lib", "master", @client)
assert sha == "c928b5d4c7fa3c0c665cb2d2f591def3bdf29c38"
end
end
Expand All @@ -36,7 +36,7 @@ defmodule Tentacat.ContentsTest do
"branch"=> "master"
}
use_cassette "contents#create" do
{status_code, _} = create("soudqwiggle", "elixir-conspiracy", "README.md", body, @client)
{status_code,_,_} = create("soudqwiggle", "elixir-conspiracy", "README.md", body, @client)
assert status_code == 201
end
end
Expand All @@ -53,7 +53,7 @@ defmodule Tentacat.ContentsTest do
"branch"=> "master"
}
use_cassette "contents#update" do
%{"commit" => %{"sha" => sha}} = update("soudqwiggle", "elixir-conspiracy", "README.md", body, @client)
{_,%{"commit" => %{"sha" => sha}},_} = update("soudqwiggle", "elixir-conspiracy", "README.md", body, @client)
assert sha == "bf7c4ab53fe7503b82b9654d5979ebe8c24ea009"
end
end
Expand All @@ -69,7 +69,7 @@ defmodule Tentacat.ContentsTest do
"branch"=> "master"
}
use_cassette "contents#remove" do
%{"commit"=>%{"sha"=>sha}} = remove("soudqwiggle", "elixir-conspiracy", "README.md", body, @client)
{_,%{"commit"=>%{"sha"=>sha}},_} = remove("soudqwiggle", "elixir-conspiracy", "README.md", body, @client)
assert sha == "2ff6f7942773c268dc9ab0e11e9dffad402c1860"
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
[
{
"request": {
"body": "\"\\\"\\\"\"",
"headers": {
"User-agent": "tentacat"
"User-agent": "tentacat",
"Authorization": "token yourtokencomeshere"
},
"method": "get",
"options": [],
"request_body": "",
"url": "https://api.github.com/users/jeffweiss/repos"
},
"response": {
Expand Down Expand Up @@ -42,13 +41,12 @@
},
{
"request": {
"body": "\"\"",
"headers": {
"User-agent": "tentacat"
"User-agent": "tentacat",
"Authorization": "token yourtokencomeshere"
},
"method": "get",
"options": [],
"request_body": "",
"url": "https://api.github.com/user/818053/repos?page=2"
},
"response": {
Expand Down
2 changes: 0 additions & 2 deletions test/fixture/vcr_cassettes/teams/repositories#list.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
[
{
"request": {
"body": "\"\\\"\\\"\"",
"headers": {
"User-agent": "tentacat",
"Authorization": "token yourtokencomeshere"
},
"method": "get",
"options": [],
"request_body": "",
"url": "https://api.github.com/teams/1500000/repos"
},
"response": {
Expand Down
16 changes: 8 additions & 8 deletions test/followers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,50 @@ defmodule Tentacat.FollowersTest do

test "following/1" do
use_cassette "followers#following/1" do
assert following(@client) == []
assert elem(following(@client),1) == []
end
end

test "following/2" do
use_cassette "followers#following/2" do
assert following("duksis", @client) == []
assert elem(following("duksis", @client),1) == []
end
end

test "followers/1" do
use_cassette "followers#followers/1" do
[%{"login" => username}] = followers(@client)
{_,[%{"login" => username}],_} = followers(@client)
assert username == "duksis"
end
end

test "followers/2" do
use_cassette "followers#followers/2" do
assert followers("duksis", @client) == []
assert elem(followers("duksis", @client),1) == []
end
end

test "following?/2" do
use_cassette "followers#following_/2" do
assert following?("duksis", @client) == true
assert elem(following?("duksis", @client),1) == true
end
end

test "following?/3" do
use_cassette "followers#following_/3" do
assert following?("torvalds", "duksis", @client) == false
assert elem(following?("torvalds", "duksis", @client),1) == false
end
end

test "follow/2" do
use_cassette "followers#follow" do
assert follow("duksis", @client) == true
assert elem(follow("duksis", @client),1) == true
end
end

test "unfollow/2" do
use_cassette "followers#unfollow" do
assert unfollow("duksis", @client) == true
assert elem(unfollow("duksis", @client),1) == true
end
end

Expand Down
5 changes: 3 additions & 2 deletions test/git/blobs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ defmodule Tentacat.Git.BlobsTest do
"content" => "Woop!",
"encoding" => "utf-8"
}
{ 201, %{"sha" => sha}} = create("soudqwiggle", "elixir-conspiracy", body, @client)
{201, %{"sha" => sha},_} = create("soudqwiggle", "elixir-conspiracy", body, @client)
assert sha == "d69a46d50f10c479a19bd56a066c55671180ceec"
end
end

test "get/4" do
use_cassette "git/blob#get" do
sha = "d69a46d50f10c479a19bd56a066c55671180ceec"
assert get("soudqwiggle", "elixir-conspiracy", sha, @client) |> Map.get("sha") == sha
{200,res,_} = get("soudqwiggle", "elixir-conspiracy", sha, @client)
assert res |> Map.get("sha") == sha
end
end
end
Loading

0 comments on commit 7e161b5

Please sign in to comment.