Packages
An Elixir library for searching and fetching controlled vocabulary authority terms
Current section
38 Versions
Jump to
Current section
38 Versions
Compare versions
3
files changed
+32
additions
-46
deletions
| @@ -19,7 +19,8 @@ | |
| 19 19 | <<"lib/authoritex/mock.ex">>,<<"lib/authoritex/getty">>, |
| 20 20 | <<"lib/authoritex/getty/ulan.ex">>,<<"lib/authoritex/getty/tgn.ex">>, |
| 21 21 | <<"lib/authoritex/getty/base.ex">>,<<"lib/authoritex/getty/aat.ex">>, |
| 22 | - <<"lib/authoritex.ex">>,<<"mix.exs">>,<<"README.md">>,<<"LICENSE.md">>]}. |
| 22 | + <<"lib/mix">>,<<"lib/authoritex.ex">>,<<"mix.exs">>,<<"README.md">>, |
| 23 | + <<"LICENSE.md">>]}. |
| 23 24 | {<<"licenses">>,[<<"MIT">>]}. |
| 24 25 | {<<"links">>,[{<<"GitHub">>,<<"https://github.com/nulib/authoritex">>}]}. |
| 25 26 | {<<"name">>,<<"authoritex">>}. |
| @@ -28,12 +29,12 @@ | |
| 28 29 | {<<"name">>,<<"httpoison">>}, |
| 29 30 | {<<"optional">>,false}, |
| 30 31 | {<<"repository">>,<<"hexpm">>}, |
| 31 | - {<<"requirement">>,<<"~> 1.6.2">>}], |
| 32 | + {<<"requirement">>,<<"~> 1.7.0">>}], |
| 32 33 | [{<<"app">>,<<"httpoison_retry">>}, |
| 33 34 | {<<"name">>,<<"httpoison_retry">>}, |
| 34 35 | {<<"optional">>,false}, |
| 35 36 | {<<"repository">>,<<"hexpm">>}, |
| 36 | - {<<"requirement">>,<<"~> 1.0.0">>}], |
| 37 | + {<<"requirement">>,<<"~> 1.1.0">>}], |
| 37 38 | [{<<"app">>,<<"jason">>}, |
| 38 39 | {<<"name">>,<<"jason">>}, |
| 39 40 | {<<"optional">>,false}, |
| @@ -44,4 +45,4 @@ | |
| 44 45 | {<<"optional">>,false}, |
| 45 46 | {<<"repository">>,<<"hexpm">>}, |
| 46 47 | {<<"requirement">>,<<"~> 0.6">>}]]}. |
| 47 | - {<<"version">>,<<"0.5.0">>}. |
| 48 | + {<<"version">>,<<"0.6.0">>}. |
| @@ -45,51 +45,36 @@ defmodule Authoritex.LOC.Base do | |
| 45 45 | do: fetch(unquote(http_uri) <> "/" <> rest) |
| 46 46 | |
| 47 47 | def fetch(id) do |
| 48 | - with url <- String.replace(id, ~r/^http:/, "https:") do |
| 49 | - request = |
| 50 | - HTTPoison.get(url <> ".rdf") |
| 51 | - |> autoretry() |
| 48 | + with url <- String.replace(id, ~r/^http:/, "https:"), |
| 49 | + response <- HTTPoison.head!(url) do |
| 50 | + case {response.headers |> Enum.into(%{}), response.status_code} do |
| 51 | + {%{"X-PrefLabel" => label, "X-URI" => uri}, code} when code in [200, 303] -> |
| 52 | + {:ok, make_fetch_result(uri, label)} |
| 52 53 | |
| 53 | - case request do |
| 54 | - {:ok, response} -> |
| 55 | - parse_fetch_result(response) |
| 54 | + {%{"Location" => location}, code} when code in 301..302 -> |
| 55 | + fetch(location) |
| 56 56 | |
| 57 | - {:error, error} -> |
| 58 | - {:error, error} |
| 57 | + {_, 303} -> |
| 58 | + {:error, 404} |
| 59 | + |
| 60 | + {_, 200} -> |
| 61 | + {:error, {:bad_response, :missing_label}} |
| 62 | + |
| 63 | + {_, status_code} -> |
| 64 | + {:error, status_code} |
| 59 65 | end |
| 60 66 | end |
| 61 67 | end |
| 62 68 | |
| 63 | - defp parse_fetch_result(%{body: response, status_code: 200}) do |
| 64 | - with doc <- SweetXml.parse(response) do |
| 65 | - case doc |> SweetXml.xpath(~x"/rdf:RDF") do |
| 66 | - nil -> |
| 67 | - {:error, {:bad_response, response}} |
| 68 | - |
| 69 | - rdf -> |
| 70 | - {:ok, |
| 71 | - SweetXml.xpath(rdf, ~x"./madsrdf:*", |
| 72 | - id: ~x"./@rdf:about"s, |
| 73 | - label: ~x"./madsrdf:authoritativeLabel[1]/text()"s, |
| 74 | - qualified_label: ~x"./madsrdf:authoritativeLabel[1]/text()"s, |
| 75 | - hint: ~x"./no_hint/text()" |
| 76 | - )} |
| 77 | - end |
| 78 | - end |
| 79 | - rescue |
| 80 | - _ -> {:error, {:bad_response, response}} |
| 69 | + defp make_fetch_result(uri, label) do |
| 70 | + %{ |
| 71 | + id: uri, |
| 72 | + label: label, |
| 73 | + qualified_label: label, |
| 74 | + hint: nil |
| 75 | + } |
| 81 76 | end |
| 82 77 | |
| 83 | - defp parse_fetch_result(%{status_code: code} = response) when code in 300..399 do |
| 84 | - response.headers |
| 85 | - |> Enum.into(%{}) |
| 86 | - |> Map.get("Location") |
| 87 | - |> String.replace(~r"\.rdf$", "") |
| 88 | - |> fetch() |
| 89 | - end |
| 90 | - |
| 91 | - defp parse_fetch_result(%{status_code: status_code}), do: {:error, status_code} |
| 92 | - |
| 93 78 | @impl Authoritex |
| 94 79 | def search(query, max_results \\ 30) do |
| 95 80 | query_params = [{:q, query} | unquote(query_filter)] |
| @@ -1,7 +1,7 @@ | |
| 1 1 | defmodule Authoritex.MixProject do |
| 2 2 | use Mix.Project |
| 3 3 | |
| 4 | - @version "0.5.0" |
| 4 | + @version "0.6.0" |
| 5 5 | @url "https://github.com/nulib/authoritex" |
| 6 6 | |
| 7 7 | def project do |
| @@ -47,12 +47,12 @@ defmodule Authoritex.MixProject do | |
| 47 47 | # Run "mix help deps" to learn about dependencies. |
| 48 48 | defp deps do |
| 49 49 | [ |
| 50 | - {:credo, "~> 1.3.0", only: [:dev, :test]}, |
| 50 | + {:credo, "~> 1.5.0", only: [:dev, :test]}, |
| 51 51 | {:ex_doc, "~> 0.19", only: [:dev, :docs]}, |
| 52 | - {:excoveralls, "~> 0.12.3", only: [:dev, :test]}, |
| 52 | + {:excoveralls, "~> 0.13.1", only: [:dev, :test]}, |
| 53 53 | {:exvcr, "~> 0.11", only: :test}, |
| 54 | - {:httpoison, "~> 1.6.2"}, |
| 55 | - {:httpoison_retry, "~> 1.0.0"}, |
| 54 | + {:httpoison, "~> 1.7.0"}, |
| 55 | + {:httpoison_retry, "~> 1.1.0"}, |
| 56 56 | {:jason, "~> 1.2.1"}, |
| 57 57 | {:sweet_xml, "~> 0.6"} |
| 58 58 | ] |