Inline QR Codes

Co-authored-by: Stamates <stamates@hotmail.com>
pull/232/head
Tim Mecklem 7 years ago
parent ea89902120
commit 882f09abcd
  1. 15
      apps/explorer_web/lib/explorer_web/controllers/address_qr_code_controller.ex
  2. 15
      apps/explorer_web/lib/explorer_web/controllers/transaction_qr_code_controller.ex
  3. 3
      apps/explorer_web/lib/explorer_web/router.ex
  4. 2
      apps/explorer_web/lib/explorer_web/templates/address/overview.html.eex
  5. 2
      apps/explorer_web/lib/explorer_web/templates/transaction/overview.html.eex
  6. 7
      apps/explorer_web/lib/explorer_web/views/address_view.ex
  7. 7
      apps/explorer_web/lib/explorer_web/views/transaction_view.ex
  8. 19
      apps/explorer_web/test/explorer_web/controllers/address_qr_code_controller_test.exs
  9. 19
      apps/explorer_web/test/explorer_web/controllers/transaction_qr_code_controller_test.exs
  10. 7
      apps/explorer_web/test/explorer_web/views/address_view_test.exs
  11. 7
      apps/explorer_web/test/explorer_web/views/transaction_view_test.exs

@ -1,15 +0,0 @@
defmodule ExplorerWeb.AddressQRCodeController do
use ExplorerWeb, :controller
alias Explorer.Chain.Hash.Truncated
def index(conn, %{"address_id" => id}) do
case Truncated.cast(id) do
{:ok, _} ->
send_download(conn, {:binary, QRCode.to_png(id)}, "content-type": "image/png", filename: "#{id}.png")
_ ->
send_resp(conn, :not_found, "")
end
end
end

@ -1,15 +0,0 @@
defmodule ExplorerWeb.TransactionQRCodeController do
use ExplorerWeb, :controller
alias Explorer.Chain.Hash.Full
def index(conn, %{"transaction_id" => id}) do
case Full.cast(id) do
{:ok, _} ->
send_download(conn, {:binary, QRCode.to_png(id)}, "content-type": "image/png", filename: "#{id}.png")
_ ->
send_resp(conn, :not_found, "")
end
end
end

@ -62,7 +62,6 @@ defmodule ExplorerWeb.Router do
)
resources("/logs", TransactionLogController, only: [:index], as: :log)
resources("/qrcode.png", TransactionQRCodeController, only: [:index], as: :qr_code)
end
resources "/addresses", AddressController, only: [:show] do
@ -81,8 +80,6 @@ defmodule ExplorerWeb.Router do
only: [:index],
as: :contract
)
resources("/qrcode.png", AddressQRCodeController, only: [:index], as: :qr_code)
end
get("/search", ChainController, :search)

@ -3,7 +3,7 @@
<div class="col-md-2 col-lg-1 order-last order-sm-first">
<div class="card mt-3 mt-sm-0">
<div class="card-body px-2 py-2">
<%= img_tag(address_qr_code_path(@conn, :index, @conn.assigns.locale, @address), alt: "qr_code", class: "qr-code") %>
<img src="data:image/png;base64, <%= qr_code(@address) %>" class="qr-code" alt="qr_code" title="<%= @address %>" />
</div>
</div>
</div>

@ -3,7 +3,7 @@
<div class="col-md-2 col-lg-1 order-last order-sm-first">
<div class="card mt-3 mt-sm-0">
<div class="card-body px-2 py-2">
<%= img_tag(transaction_qr_code_path(@conn, :index, @conn.assigns.locale, @transaction), alt: "qr_code", class: "qr-code") %>
<img src="data:image/png;base64, <%= qr_code(@transaction) %>" class="qr-code" alt="qr_code" title="<%= @transaction %>" />
</div>
</div>
</div>

@ -44,4 +44,11 @@ defmodule ExplorerWeb.AddressView do
def hash(%Address{hash: hash}) do
to_string(hash)
end
def qr_code(%Address{hash: hash}) do
hash
|> to_string()
|> QRCode.to_png()
|> Base.encode64()
end
end

@ -32,6 +32,13 @@ defmodule ExplorerWeb.TransactionView do
end
end
def qr_code(%Transaction{hash: hash}) do
hash
|> to_string()
|> QRCode.to_png()
|> Base.encode64()
end
defp fee_to_currency({fee_type, fee}, denomination: denomination) do
{fee_type, format_wei_value(Wei.from(fee, :wei), denomination)}
end

@ -1,19 +0,0 @@
defmodule ExplorerWeb.AddressQRCodeControllerTest do
use ExplorerWeb.ConnCase
import ExplorerWeb.Router.Helpers, only: [address_qr_code_path: 4]
describe "GET index/3" do
test "with valid address hash returns a QR code", %{conn: conn} do
conn = get(conn, address_qr_code_path(conn, :index, :en, address_hash()))
assert response(conn, 200)
end
test "with invalid address hash returns 404", %{conn: conn} do
conn = get(conn, address_qr_code_path(conn, :index, :en, "0xhaha"))
assert response(conn, 404)
end
end
end

@ -1,19 +0,0 @@
defmodule ExplorerWeb.TransactionQRCodeControllerTest do
use ExplorerWeb.ConnCase
import ExplorerWeb.Router.Helpers, only: [transaction_qr_code_path: 4]
describe "GET index/3" do
test "with valid `Hash.Full` returns a QR code", %{conn: conn} do
conn = get(conn, transaction_qr_code_path(conn, :index, :en, transaction_hash()))
assert response(conn, 200)
end
test "with invalid address hash returns 404", %{conn: conn} do
conn = get(conn, transaction_qr_code_path(conn, :index, :en, "0xhaha"))
assert response(conn, 404)
end
end
end

@ -23,4 +23,11 @@ defmodule ExplorerWeb.AddressViewTest do
assert "$0.000005 USD" == AddressView.formatted_usd(address, token)
end
end
describe "qr_code/1" do
test "it returns an encoded value" do
address = build(:address)
assert {:ok, _} = Base.decode64(AddressView.qr_code(address))
end
end
end

@ -83,4 +83,11 @@ defmodule ExplorerWeb.TransactionViewTest do
assert TransactionView.formatted_status(transaction) == "Success"
end
end
describe "qr_code/1" do
test "it returns an encoded value" do
transaction = build(:transaction)
assert {:ok, _} = Base.decode64(TransactionView.qr_code(transaction))
end
end
end

Loading…
Cancel
Save