Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Location endpoint support #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/shopify/adapters/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Shopify.Adapters.HTTP do
@moduledoc false

@behaviour Shopify.Adapters.Base
@options [hackney: [pool: :shopify], ssl: [{:versions, [:'tlsv1.2']}]]
@options [hackney: [pool: :shopify], ssl: [{:versions, [:'tlsv1.2']}], recv_timeout: 60_000]

def get(request) do
HTTPoison.get(request.full_url, request.headers, @options)
Expand All @@ -27,4 +27,9 @@ defmodule Shopify.Adapters.HTTP do
def handle_response({:ok, %HTTPoison.Response{status_code: code, body: body}}, resource) do
Shopify.Response.new(code, body, resource)
end

def handle_response({:error, %HTTPoison.Error{id: id, reason: reason}}, _resource) do
Shopify.Error.new(id, reason)
end

end
14 changes: 14 additions & 0 deletions lib/shopify/error.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule Shopify.Error do
@moduledoc false

alias Shopify.Error

defstruct [
:id,
:reason
]

def new(id, reason) do
{:error, %Error{id: id, reason: reason}}
end
end
38 changes: 38 additions & 0 deletions lib/shopify/resources/location.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
defmodule Shopify.Location do
@derive [Poison.Encoder]
@singular "location"
@plural "locations"

use Shopify.Resource, import: [:find, :all]

alias Shopify.Location

defstruct [
:id,
:name,
:address1,
:address2,
:zip,
:city,
:province,
:country,
:phone,
:created_at,
:updated_at,
:deleted_at,
:country_code,
:country_name,
:province_code
]

@doc false
def empty_resource do
%Location{}
end

@doc false
def find_url(id), do: @plural <> "/#{id}.json"

@doc false
def all_url, do: @plural <> ".json"
end
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Shopify.Mixfile do

def project do
[app: :shopify,
version: "0.1.6",
version: "0.1.7",
elixir: "~> 1.4-rc",
description: description(),
package: package(),
Expand Down Expand Up @@ -51,7 +51,7 @@ defmodule Shopify.Mixfile do
# Type "mix help deps" for more examples and options
defp deps do
[
{:httpoison, "~> 0.10.0"},
{:httpoison, "~> 0.13.0"},
{:poison, "~> 3.0"},
{:ex_doc, ">= 0.0.0", only: :dev}
]
Expand Down
11 changes: 6 additions & 5 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
%{"bypass": {:hex, :bypass, "0.5.1", "cf3e8a4d376ee1dcd89bf362dfaf1f4bf4a6e19895f52fdc2bafbd8207ce435f", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: false]}, {:plug, "~> 1.0", [hex: :plug, optional: false]}]},
"certifi": {:hex, :certifi, "0.7.0", "861a57f3808f7eb0c2d1802afeaae0fa5de813b0df0979153cbafcd853ababaf", [:rebar3], []},
"certifi": {:hex, :certifi, "2.0.0", "a0c0e475107135f76b8c1d5bc7efb33cd3815cb3cf3dea7aefdd174dabead064", [], [], "hexpm"},
"cowboy": {:hex, :cowboy, "1.0.4", "a324a8df9f2316c833a470d918aaf73ae894278b8aa6226ce7a9bf699388f878", [:make, :rebar], [{:cowlib, "~> 1.0.0", [hex: :cowlib, optional: false]}, {:ranch, "~> 1.0", [hex: :ranch, optional: false]}]},
"cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [:make], []},
"dialyxir": {:hex, :dialyxir, "0.4.3", "a4daeebd0107de10d3bbae2ccb6b8905e69544db1ed5fe9148ad27cd4cb2c0cd", [:mix], []},
"earmark": {:hex, :earmark, "1.0.3", "89bdbaf2aca8bbb5c97d8b3b55c5dd0cff517ecc78d417e87f1d0982e514557b", [:mix], []},
"ex_doc": {:hex, :ex_doc, "0.14.5", "c0433c8117e948404d93ca69411dd575ec6be39b47802e81ca8d91017a0cf83c", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, optional: false]}]},
"hackney": {:hex, :hackney, "1.6.5", "8c025ee397ac94a184b0743c73b33b96465e85f90a02e210e86df6cbafaa5065", [:rebar3], [{:certifi, "0.7.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.1", [hex: :ssl_verify_fun, optional: false]}]},
"httpoison": {:hex, :httpoison, "0.10.0", "4727b3a5e57e9a4ff168a3c2883e20f1208103a41bccc4754f15a9366f49b676", [:mix], [{:hackney, "~> 1.6.3", [hex: :hackney, optional: false]}]},
"hackney": {:hex, :hackney, "1.10.1", "c38d0ca52ea80254936a32c45bb7eb414e7a96a521b4ce76d00a69753b157f21", [], [{:certifi, "2.0.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
"httpoison": {:hex, :httpoison, "0.13.0", "bfaf44d9f133a6599886720f3937a7699466d23bb0cd7a88b6ba011f53c6f562", [], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
"httpotion": {:hex, :httpotion, "3.0.2", "525b9bfeb592c914a61a8ee31fdde3871e1861dfe805f8ee5f711f9f11a93483", [:mix], [{:ibrowse, "~> 4.2", [hex: :ibrowse, optional: false]}]},
"ibrowse": {:hex, :ibrowse, "4.2.2", "b32b5bafcc77b7277eff030ed32e1acc3f610c64e9f6aea19822abcadf681b4b", [:rebar3], []},
"idna": {:hex, :idna, "1.2.0", "ac62ee99da068f43c50dc69acf700e03a62a348360126260e87f2b54eced86b2", [:rebar3], []},
"idna": {:hex, :idna, "5.1.0", "d72b4effeb324ad5da3cab1767cb16b17939004e789d8c0ad5b70f3cea20c89a", [], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},
"mime": {:hex, :mime, "1.0.1", "05c393850524767d13a53627df71beeebb016205eb43bfbd92d14d24ec7a1b51", [:mix], []},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
"plug": {:hex, :plug, "1.3.0", "6e2b01afc5db3fd011ca4a16efd9cb424528c157c30a44a0186bcc92c7b2e8f3", [:mix], [{:cowboy, "~> 1.0.1 or ~> 1.1", [hex: :cowboy, optional: true]}, {:mime, "~> 1.0", [hex: :mime, optional: false]}]},
"poison": {:hex, :poison, "3.0.0", "625ebd64d33ae2e65201c2c14d6c85c27cc8b68f2d0dd37828fde9c6920dd131", [:mix], []},
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], []},
"ranch": {:hex, :ranch, "1.2.1", "a6fb992c10f2187b46ffd17ce398ddf8a54f691b81768f9ef5f461ea7e28c762", [:make], []},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []}}
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.3.1", "a1f612a7b512638634a603c8f401892afbf99b8ce93a45041f8aaca99cadb85e", [], [], "hexpm"}}
21 changes: 21 additions & 0 deletions test/fixtures/locations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"locations": [
{
"id": 1034478814,
"name": "Berlin Store",
"deleted_at": null,
"address1": null,
"address2": null,
"city": null,
"zip": null,
"province": null,
"country": "DE",
"phone": null,
"created_at": "2017-01-05T15:36:16-05:00",
"updated_at": "2017-01-05T15:36:16-05:00",
"country_code": "DE",
"country_name": "Germany",
"province_code": null
}
]
}
19 changes: 19 additions & 0 deletions test/fixtures/locations/1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"location": {
"id": 487838322,
"name": "Fifth Avenue AppleStore",
"deleted_at": null,
"address1": null,
"address2": null,
"city": null,
"zip": null,
"province": null,
"country": "US",
"phone": null,
"created_at": "2017-01-05T15:36:16-05:00",
"updated_at": "2017-01-05T15:36:16-05:00",
"country_code": "US",
"country_name": "United States",
"province_code": null
}
}
21 changes: 21 additions & 0 deletions test/location_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule Shopify.LocationTest do
use ExUnit.Case, async: true

alias Shopify.Location

test "client can request a single location" do
assert {:ok, response} = Shopify.session |> Location.find(1)
assert %Shopify.Response{} = response
assert 200 == response.code
fixture = Fixture.load("../test/fixtures/locations/1.json", "location", Location.empty_resource())
assert fixture == response.data
end

test "client can request all locations" do
assert {:ok, response} = Shopify.session |> Location.all
assert %Shopify.Response{} = response
assert 200 == response.code
fixture = Fixture.load("../test/fixtures/locations.json", "locations", [Location.empty_resource()])
assert fixture == response.data
end
end