elixir

Are there things Elixir can do that Erlang cannot, or vice versa?

為{幸葍}努か 提交于 2019-12-18 09:58:53
问题 This question is in the context of the Beam VM and the capabilities that it provides, not in the general context of what a Turing complete language can do. I want to invest some time to learn either pure Erlang or Elixir. I get the basic differences between the two and I am leaning towards Elixir because of the macros, better syntax and faster development of the language this day. My question is: if I choose Elixir, will I stumble on something that I cannot do in it, but can do in Erlang? Can

Can I get Ecto to log raw SQL?

只愿长相守 提交于 2019-12-18 09:16:49
问题 I am building an Ecto query like this: from item in query, where: like(item.description, ^"%#{text}%") I'm concerned that this allows SQL injection in text . Before trying to fix that, I want to see how the query is actually sent to the database. If I inspect the query or look at what is logged, I see some SQL, but it's not valid. For instance, inspecting the query shows me this: {"SELECT i0.\"id\", i0.\"store_id\", i0.\"title\", i0.\"description\" FROM \"items\" AS i0 WHERE (i0.\"description

Render static html page in a controller

 ̄綄美尐妖づ 提交于 2019-12-18 09:06:51
问题 Is there a way to read and render a static html file located at another part on server in the controller ? I am not looking to redirect or serve this page via static pages functionality. 回答1: You should use Plug.Conn.send_file/5 for this. This function will send the contents of the file more efficiently than reading the whole file into memory and then sending it using Phoenix.Controller.html/2 : conn |> put_resp_header("content-type", "text/html; charset=utf-8") |> Plug.Conn.send_file(200, "

Connection closed - strange error, unable to connect from erlang VM to certain host

。_饼干妹妹 提交于 2019-12-18 08:29:26
问题 IMPORTANT Proved that migration from 18.3 to 18.3.4 causes this issue, and migration back to 18.3 removes it. Everything worked until one moment. Then it just stoped to work. def work do HTTPotion.get("https://ssl-third-party.com", ibrowse: [ is_ssl: true, ssl_options: [ certfile: Path.join(File.cwd!, "cert_dev.pem"), password: "pass" |> to_charlist, ] ]) end Which responds with: %HTTPotion.ErrorResponse{message: "closed"} I tried several erlang libs without elixir wrapping(hackney + ibrowse)

Specify arity using only or except when importing function on Elixir

ⅰ亾dé卋堺 提交于 2019-12-18 07:38:09
问题 I'm studying Elixir and when I use only or except operators when importing functions from a module I need to specify an arity number. Why? e.g. import :math, only: [sqrt: 1] or import :math, except: [sin: 1, cos: 1] 回答1: Across the Erlang ecosystem functions are identified by name + arity. In most other languages you can overload functions by name. In other words, in the Erlang world foo/1 (that is, foo(one_arg)) is a completely different function than foo/2 (as in, foo(one_arg, two_arg)),

Get console user input as typed, char by char

谁说我不能喝 提交于 2019-12-18 07:36:15
问题 I have a console application in Elixir. I need to interpret user’s input on by keypress basis. For instance, I need to treat “q” as a command to end the session, without user to explicitly press ⏎ a.k.a. “carriage return.” IO.getn/2 surprisingly waits for the ⏎ to be pressed, buffering an input (I am nearly sure, that this buffering is done by console itself, but man stty does not provide any help/flag to turn buffering off.) Mix.Utils use the infinite loop to hide user input (basically

What is the “|>” symbol's purpose in Elixir?

大城市里の小女人 提交于 2019-12-18 04:35:18
问题 I've searched the Elixir and Phoenix docs, as well as a few other sites like Learn Elixir with no luck. Here is what it looks like: defp update_positions(item_ids) do item_ids = String.split(item_ids, ",") |> Enum.map fn item_id -> String.to_integer(item_id) end items = Repo.all(Item |> where([item], item.id in array(^item_ids, :integer))) item_hash = Enum.reduce items, %{}, fn item, map -> Map.put(map, item.id, item) end item_ids |> Stream.with_index |> Enum.each fn {item_id, index} -> item

Turn postgres date representation into ISO 8601 string

吃可爱长大的小学妹 提交于 2019-12-17 23:27:25
问题 I'm trying to format a Postgres date representation into a ISO 8601 string. I'm assuming that there is a Postgres function that can do it, but I found the documentation short on examples. My query is SELECT now()::timestamp which returns [{{2016, 8, 9}, {3, 56, 55, 754181}}] I'm trying to get the date into a format that looks more like 2016-8-9T03:56:55+00:00 . What changes do I need to make to my query to make that happen? Thanks for your help. 回答1: I think I found a way to do the formatting

How to use raw sql with ecto Repo

岁酱吖の 提交于 2019-12-17 22:18:07
问题 I have an upsert requirement, so I need to call a postgres stored procedure or use a common table expression. I also use the pgcrypto exgtension for passwords and would like to use postgres functions (such as "crypt" to encode/decode passwords). But I can not find a way to get Ecto to play with raw sql in part or whole, is it intended that ecto will only support the elixir dsl and not allow shelling out to raw sql when the dsl is not sufficient? I've found that I can query via the adapter

Phoenix - controller with multiple render

馋奶兔 提交于 2019-12-17 22:13:17
问题 Trying to create an app with Elixir + Phoenix, that would be able to handle both "browser" and "api" requests to handle its resources. Is it possible to do it without having to do something like that : scope "/", App do pipe_through :browser resources "/users", UserController end scope "/api", App.API as: :api do pipe_through :api resources "/users", UserController end which would mean having to create two controllers, which might have the same behavior, except that it will render HTML with