Skip to content

Commit

Permalink
Merge f778e94 into 7063dd3
Browse files Browse the repository at this point in the history
  • Loading branch information
johansja committed Sep 22, 2016
2 parents 7063dd3 + f778e94 commit 36ff0da
Show file tree
Hide file tree
Showing 21 changed files with 29 additions and 44 deletions.
1 change: 0 additions & 1 deletion config/config.exs
Expand Up @@ -24,4 +24,3 @@ use Mix.Config
# import_config "#{Mix.env}.exs"
path = __DIR__ |> Path.expand |> Path.join("#{Mix.env}.exs")
if File.exists?(path), do: import_config "#{Mix.env}.exs"

2 changes: 0 additions & 2 deletions config/test.exs
Expand Up @@ -6,5 +6,3 @@ config :gmail, :oauth2, [
client_id: "fake-client-id",
client_secret: "fake-client-secret"
]


2 changes: 1 addition & 1 deletion lib/gmail/base.ex
Expand Up @@ -57,7 +57,7 @@ defmodule Gmail.Base do
{:ok, %{"error" => %{"code" => 404}}} ->
{:error, :not_found}
{:ok, %{"error" => %{"code" => 400, "errors" => errors}}} ->
[%{"message" => error_message}|_rest] = errors
[%{"message" => error_message} | _rest] = errors
{:error, error_message}
{:ok, %{"error" => details}} ->
{:error, details}
Expand Down
4 changes: 2 additions & 2 deletions lib/gmail/body.ex
Expand Up @@ -7,7 +7,7 @@ defmodule Gmail.Body do
alias __MODULE__
alias Gmail.Helper

defstruct size: 0, data: ""
defstruct size: 0, data: "", attachment_id: ""
@type t :: %__MODULE__{}

@doc """
Expand All @@ -16,7 +16,7 @@ defmodule Gmail.Body do
@spec convert(Map.t) :: Body.t
def convert(body) do
{data, body} = body |> Helper.atomise_keys |> Map.pop(:data)
if data, do: body = Map.put(body, :data, decode_body(data))
body = if data, do: Map.put(body, :data, decode_body(data)), else: body
struct(Body, body)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/gmail/helper.ex
Expand Up @@ -40,8 +40,8 @@ defmodule Gmail.Helper do
end

def camelize(str) do
[first|rest] = str |> Macro.camelize |> String.codepoints
[String.downcase(first)|rest] |> Enum.join
[first | rest] = str |> Macro.camelize |> String.codepoints
[String.downcase(first) | rest] |> Enum.join
end

end
3 changes: 1 addition & 2 deletions lib/gmail/message.ex
Expand Up @@ -108,8 +108,7 @@ defmodule Gmail.Message do
|> Helper.atomise_keys
|> Map.pop(:payload)
message = struct(Message, message)
if payload, do: message = Map.put(message, :payload, Payload.convert(payload))
message
if payload, do: Map.put(message, :payload, Payload.convert(payload)), else: message
end

@doc """
Expand Down
1 change: 0 additions & 1 deletion lib/gmail/message/pool_worker.ex
Expand Up @@ -36,4 +36,3 @@ defmodule Gmail.Message.PoolWorker do
end

end

8 changes: 3 additions & 5 deletions lib/gmail/message_attachment.ex
Expand Up @@ -12,7 +12,7 @@ defmodule Gmail.MessageAttachment do
Gmail API documentation: https://developers.google.com/gmail/api/v1/reference/users/messages/attachments
"""
defstruct attachmentId: "",
size: "",
size: 0,
data: ""

@type t :: %__MODULE__{}
Expand All @@ -34,10 +34,8 @@ defmodule Gmail.MessageAttachment do
"""
@spec convert(map) :: MessageAttachment.t
def convert(message) do
attachment = message
|> Helper.atomise_keys

attachment = struct(MessageAttachment, attachment)
attachment = message |> Helper.atomise_keys
struct(MessageAttachment, attachment)
end

@doc """
Expand Down
2 changes: 1 addition & 1 deletion lib/gmail/oauth2.ex
Expand Up @@ -45,7 +45,7 @@ defmodule Gmail.OAuth2 do
from_config_file |> do_refresh_access_token(refresh_token)
end

defp do_refresh_access_token(%{client_id: client_id, client_secret: client_secret} = config, refresh_token) do
defp do_refresh_access_token(%{client_id: client_id, client_secret: client_secret}, refresh_token) do
payload = %{
client_id: client_id,
client_secret: client_secret,
Expand Down
7 changes: 4 additions & 3 deletions lib/gmail/payload.ex
Expand Up @@ -27,12 +27,13 @@ defmodule Gmail.Payload do
|> Map.pop(:body)
{parts, payload} = Map.pop(payload, :parts)
payload = struct(Payload, payload)
if body, do: payload = Map.put(payload, :body, Body.convert(body))
payload = if body, do: Map.put(payload, :body, Body.convert(body)), else: payload
if parts do
parts = Enum.map(parts, &convert/1)
payload = Map.put(payload, :parts, parts)
Map.put(payload, :parts, parts)
else
payload
end
payload
end

end
19 changes: 9 additions & 10 deletions lib/gmail/user.ex
Expand Up @@ -291,7 +291,7 @@ defmodule Gmail.User do
{:ok, %{"error" => %{"code" => 404}}} ->
{:error, :not_found}
{:ok, %{"error" => %{"code" => 400, "errors" => errors}}} ->
[%{"message" => error_message}|_rest] = errors
[%{"message" => error_message} | _rest] = errors
{:error, error_message}
{:ok, %{"error" => details}} ->
{:error, details}
Expand All @@ -310,7 +310,7 @@ defmodule Gmail.User do
{:ok, %{"error" => %{"code" => 404}}} ->
{:error, :not_found}
{:ok, %{"error" => %{"code" => 400, "errors" => errors}}} ->
[%{"message" => error_message}|_rest] = errors
[%{"message" => error_message} | _rest] = errors
{:error, error_message}
:ok ->
:ok
Expand Down Expand Up @@ -638,14 +638,13 @@ defmodule Gmail.User do
"""
@spec http_execute({atom, String.t, String.t} | {atom, String.t, String.t, map}, map) :: atom | {atom, map | String.t}
def http_execute(action, %{refresh_token: refresh_token, user_id: user_id} = state) do
state = cond do
OAuth2.access_token_expired?(state) ->
Logger.debug "Refreshing access token for #{user_id}"
{access_token, expires_at} = OAuth2.refresh_access_token(refresh_token)
GenServer.cast(String.to_atom(user_id), {:update_access_token, access_token, expires_at})
%{state | access_token: access_token}
true ->
state
state = if OAuth2.access_token_expired?(state) do
Logger.debug "Refreshing access token for #{user_id}"
{access_token, expires_at} = OAuth2.refresh_access_token(refresh_token)
GenServer.cast(String.to_atom(user_id), {:update_access_token, access_token, expires_at})
%{state | access_token: access_token}
else
state
end
HTTP.execute(action, state)
end
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Expand Up @@ -28,7 +28,8 @@ defmodule Gmail.Mixfile do
{:dialyxir, "~> 0.3", only: :dev},
{:credo, "~> 0.3", only: :dev},
{:bypass, "~> 0.1", only: :test},
{:inch_ex, "~> 0.5", only: :docs}
{:inch_ex, "~> 0.5", only: :docs},
{:mix_test_watch, "~> 0.2", only: :dev}
]
end

Expand Down
2 changes: 2 additions & 0 deletions mix.lock
Expand Up @@ -10,6 +10,7 @@
"ex_doc": {:hex, :ex_doc, "0.12.0", "b774aabfede4af31c0301aece12371cbd25995a21bb3d71d66f5c2fe074c603f", [:mix], [{:earmark, "~> 0.2", [hex: :earmark, optional: false]}]},
"excoveralls": {:hex, :excoveralls, "0.5.6", "35a903f6f78619ee7f951448dddfbef094b3a0d8581657afaf66465bc930468e", [:mix], [{:exjsx, "~> 3.0", [hex: :exjsx, optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, optional: false]}]},
"exjsx": {:hex, :exjsx, "3.2.0", "7136cc739ace295fc74c378f33699e5145bead4fdc1b4799822d0287489136fb", [:mix], [{:jsx, "~> 2.6.2", [hex: :jsx, optional: false]}]},
"fs": {:hex, :fs, "0.9.2", "ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59", [:rebar], []},
"gettext": {:hex, :gettext, "0.11.0", "80c1dd42d270482418fa158ec5ba073d2980e3718bacad86f3d4ad71d5667679", [:mix], []},
"hackney": {:hex, :hackney, "1.6.1", "ddd22d42db2b50e6a155439c8811b8f6df61a4395de10509714ad2751c6da817", [:rebar3], [{:certifi, "0.4.0", [hex: :certifi, optional: false]}, {:idna, "1.2.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.0", [hex: :ssl_verify_fun, optional: false]}]},
"httpoison": {:hex, :httpoison, "0.9.1", "6c2b4eaf2588a6f3ef29663d28c992531ca3f0bc832a97e0359bc822978e1c5d", [:mix], [{:hackney, "~> 1.6.0", [hex: :hackney, optional: false]}]},
Expand All @@ -20,6 +21,7 @@
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},
"mime": {:hex, :mime, "1.0.1", "05c393850524767d13a53627df71beeebb016205eb43bfbd92d14d24ec7a1b51", [:mix], []},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
"mix_test_watch": {:hex, :mix_test_watch, "0.2.6", "9fcc2b1b89d1594c4a8300959c19d50da2f0ff13642c8f681692a6e507f92cab", [:mix], [{:fs, "~> 0.9.1", [hex: :fs, optional: false]}]},
"mock": {:hex, :mock, "0.1.3", "657937b03f88fce89b3f7d6becc9f1ec1ac19c71081aeb32117db9bc4d9b3980", [:mix], [{:meck, "~> 0.8.2", [hex: :meck, optional: false]}]},
"plug": {:hex, :plug, "1.2.0", "496bef96634a49d7803ab2671482f0c5ce9ce0b7b9bc25bc0ae8e09859dd2004", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}, {:mime, "~> 1.0", [hex: :mime, optional: false]}]},
"poison": {:hex, :poison, "2.2.0", "4763b69a8a77bd77d26f477d196428b741261a761257ff1cf92753a0d4d24a63", [:mix], []},
Expand Down
1 change: 0 additions & 1 deletion test/gmail/draft_test.exs
Expand Up @@ -207,4 +207,3 @@ defmodule Gmail.DraftTest do
end

end

1 change: 0 additions & 1 deletion test/gmail/history_test.exs
Expand Up @@ -63,4 +63,3 @@ defmodule Gmail.HistoryTest do
end

end

2 changes: 0 additions & 2 deletions test/gmail/label_test.exs
Expand Up @@ -183,7 +183,6 @@ defmodule Gmail.LabelTest do
bypass: bypass,
label_id: label_id,
four_hundred_error: four_hundred_error,
four_hundred_error_content: four_hundred_error_content,
user_id: user_id,
error_message_1: error_message_1
} do
Expand Down Expand Up @@ -279,7 +278,6 @@ defmodule Gmail.LabelTest do
test "handles an error when patching a label", %{
bypass: bypass,
four_hundred_error: four_hundred_error,
four_hundred_error_content: four_hundred_error_content,
label_id: label_id,
user_id: user_id,
error_message_1: error_message_1
Expand Down
2 changes: 1 addition & 1 deletion test/gmail/message_attachment_test.exs
Expand Up @@ -16,7 +16,7 @@ defmodule Gmail.MessageAttachmentTest do
end

{
:ok,
:ok,
access_token: access_token,
bypass: bypass,
user_id: user_id,
Expand Down
4 changes: 1 addition & 3 deletions test/gmail/message_test.exs
Expand Up @@ -87,8 +87,7 @@ defmodule Gmail.MessageTest do
test "handles no messages being returned", %{
bypass: bypass,
access_token: access_token,
user_id: user_id,
expected_search_result: expected_search_result
user_id: user_id
} do
Bypass.expect bypass, fn conn ->
assert "/gmail/v1/users/#{user_id}/messages" == conn.request_path
Expand Down Expand Up @@ -309,4 +308,3 @@ defmodule Gmail.MessageTest do
end

end

2 changes: 0 additions & 2 deletions test/gmail/oauth2_test.exs
Expand Up @@ -9,7 +9,6 @@ defmodule Gmail.OAuth2Test do

test "refreshes an expired access token" do
expires_in = 10
expires_at = (:os.system_time(:seconds) + expires_in)
access_token = "fake_access_token"
fake_query = 'fake_query'
opts = %{
Expand All @@ -26,4 +25,3 @@ defmodule Gmail.OAuth2Test do
end

end

1 change: 0 additions & 1 deletion test/gmail/thread/worker_test.exs
Expand Up @@ -3,7 +3,6 @@ ExUnit.start
defmodule Gmail.Thread.WorkerTest do

use ExUnit.Case
import Mock

test "dummy" do
assert 1 == 1
Expand Down
2 changes: 0 additions & 2 deletions test/gmail/thread_test.exs
Expand Up @@ -427,5 +427,3 @@ defmodule Gmail.ThreadTest do
end

end


0 comments on commit 36ff0da

Please sign in to comment.