Skip to content

Commit

Permalink
Simplify configuration handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sixFingers committed Aug 26, 2016
1 parent a6914fe commit e0958a0
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 57 deletions.
49 changes: 2 additions & 47 deletions lib/streamex/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,6 @@ defmodule Streamex.Config do
@default_version 1.0
@default_region ""

@doc """
Configures the client with the values set in the
configuration file. Returns `:ok`.
"""
def configure() do
configure(
Application.get_env(:streamex, :key, ""),
Application.get_env(:streamex, :secret, ""),
region: Application.get_env(:streamex, :region, "")
)
end

@doc """
Configures the client with the specified `key`,
`secret` and `options`. Returns `:ok`.
Available options are:
- `region` - Currently supported values are `us-east`, `us-west` and `eu-west`
- `timeout` - Number of milliseconds before a request is discarded. `3000` by default
- `version` - API version number. Defaults to 1.0
## Examples
iex> Streamex.Config.configure("my_key", "my_secret")
:ok
iex> Streamex.Config.configure("my_key", [region: "us-central"])
:ok
"""
def configure(key, secret, opts \\ []) do
Application.put_env(:streamex, :key, key)
Application.put_env(:streamex, :secret, secret)

region = Keyword.get(opts, :region, @default_region)
Application.put_env(:streamex, :region, region)

timeout = Keyword.get(opts, :timeout, @default_timeout)
Application.put_env(:streamex, :timeout, timeout)

version = Keyword.get(opts, :version, @default_version)
Application.put_env(:streamex, :version, version)
end

@doc """
Returns the current client's base api url.
"""
Expand All @@ -77,12 +32,12 @@ defmodule Streamex.Config do
@doc """
Returns the current client's api region.
"""
def region, do: Application.get_env(:streamex, :region)
def region, do: Application.get_env(:streamex, :region, @default_region)

@doc """
Returns the current client's api version.
"""
def version, do: Application.get_env(:streamex, :version)
def version, do: Application.get_env(:streamex, :version, @default_version)

@doc """
Returns the current client's timeout setting.
Expand Down
3 changes: 1 addition & 2 deletions test/activities_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ defmodule ActivityTest do
use ExUnit.Case, async: false
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
import Streamex.Feed
alias Streamex.{Config, Feed, Activity, Activities, ErrorNotFound, ErrorInput}
alias Streamex.{Feed, Activity, Activities, ErrorNotFound, ErrorInput}

doctest Streamex

setup_all do
Config.configure()
ExVCR.Config.cassette_library_dir("fixture/vcr_cassettes")
end

Expand Down
4 changes: 0 additions & 4 deletions test/client_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ defmodule ClientTest do

doctest Streamex

setup_all do
Config.configure()
end

test "client builds requests with correct full url" do
Application.put_env(:streamex, :region, "eu-west")
req = %Request{} |> Request.with_params(%{"parameter" => "value"}) |> Client.prepare_request
Expand Down
4 changes: 2 additions & 2 deletions test/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ defmodule ConfigTest do
doctest Streamex

test "Configuration handles empty region" do
Config.configure(Config.key, Config.secret, region: "")
Application.put_env(:streamex, :region, "")
assert Config.base_url == "https://api.getstream.io/api/1.0"
end

test "Configuration handles region" do
Config.configure(Config.key, Config.secret, region: "us-west")
Application.put_env(:streamex, :region, "us-west")
assert Config.base_url == "https://us-west-api.getstream.io/api/1.0"
end
end
3 changes: 1 addition & 2 deletions test/feed_test.exs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
defmodule FeedTest do
use ExUnit.Case, async: false
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
alias Streamex.{Config, Feed, Follow, ErrorInput, ErrorFeedNotFound}
alias Streamex.{Feed, Follow, ErrorInput, ErrorFeedNotFound}

doctest Streamex

setup_all do
Config.configure()
ExVCR.Config.cassette_library_dir("fixture/vcr_cassettes")
end

Expand Down

0 comments on commit e0958a0

Please sign in to comment.