<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Acutario - Medium]]></title>
        <description><![CDATA[Medium tech publication of Acutario staff - Medium]]></description>
        <link>https://medium.com/acutario?source=rss----6f054250591c---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Acutario - Medium</title>
            <link>https://medium.com/acutario?source=rss----6f054250591c---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 23:07:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/acutario" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Sending notifications in Elixir with Ravenx]]></title>
            <link>https://medium.com/acutario/sending-notifications-in-elixir-with-ravenx-1f2502a1f272?source=rss----6f054250591c---4</link>
            <guid isPermaLink="false">https://medium.com/p/1f2502a1f272</guid>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[mobile]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[push-notification]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Óscar de Arriba]]></dc:creator>
            <pubDate>Tue, 25 Apr 2017 13:17:03 GMT</pubDate>
            <atom:updated>2017-04-25T14:28:27.067Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DlIi3tZirr1_-AoPipaaRQ.jpeg" /></figure><p>During the development of the new version of <a href="http://www.acutar.io/">Acutario</a> in Elixir, we had some issues related to notification dispatching.</p><p>One of the improvements we are working on is the possibility of sending notifications to multiple services, like e-mail, Slack, push notifications to mobile apps…</p><p>That functionality make us face with the problem of using different strategies depending on a concrete scenario that can be different for each user we want to notify, so we decided to develop a library that help us to manage all that cases. <strong>And we called it </strong><a href="https://github.com/acutario/ravenx"><strong>Ravenx</strong></a><strong>.</strong></p><h3>How Ravenx works</h3><p>The library is made of <strong>strategies</strong>, which define a way of sending a notification (Slack, e-mail, APNS, etc), and they have an <a href="https://github.com/acutario/ravenx/blob/master/lib/ravenx/strategy_behaviour.ex">standard interface</a>, so its internal behaviour is abstracted from the outside.</p><p>Currently, Ravenx has two different strategies:</p><ul><li><strong>Slack:</strong> provides sending slack messages using webhooks URLs.</li><li><strong>E-mail:</strong> provides sending emails using different strategies (the ones included in <a href="https://github.com/thoughtbot/bamboo">bamboo</a>).</li></ul><p>But, as we are going to see at the end of this article, new strategies can be easily implemented to manage sending notifications to other systems and platforms.</p><p>Also, there are several ways of defining <strong>configurations</strong> which the strategies will use to know how to send the notification.</p><p>Configurations are per strategy, so they defines how a notification is sent (for example, Mailgun’s configuration to send e-mails).</p><h4>Global configuration</h4><p>This approach includes two kinds of configuration:</p><ul><li><strong>General Ravenx configuration:</strong> like the custom strategies to use, or the configuration module of our app (we will see this two aspects in a few moments).</li><li><strong>Per-strategy configuration:</strong> <em>strategy-specific</em> configuration (Mailgun’s config, Slack’s webhook URL…)</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2fede8e9b1b04360b66d92f2d7d3490/href">https://medium.com/media/c2fede8e9b1b04360b66d92f2d7d3490/href</a></iframe><h4>Configuration module</h4><p>A module that have a function for each strategy that receives the payload and returns a map with configurations that apply to that specific payload.</p><p>These configuration modules are useful to automatise the configuration generation, for example, if we need to retrieve the slack username of each user in each notification.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4cc7ab426b5aecfc6ac0c879e60f9df7/href">https://medium.com/media/4cc7ab426b5aecfc6ac0c879e60f9df7/href</a></iframe><h4>Particular configuration</h4><p>This is a configuration map sent when a notification is dispatched. Useful to add configuration generated right before sending the notification.</p><pre>Ravenx.dispatch(strategy, payload, %{url: &quot;...&quot;, icon: &quot;:bird:&quot;})</pre><h4>Mixing configurations</h4><p>For a given notification dispatch we can have configurations of the three kinds. In that case, configurations are merged with this priorities:</p><blockquote>Global &lt; Module &lt; Particular</blockquote><p>So a particular configuration key is used even if it’s defined in the configuration returned by a configuration module or by the app configuration.</p><h3>Sending a notification</h3><p><strong>Ravenx</strong> allows to send a notification in a <strong>synchronous or asynchronous</strong> way. In case you want to send a notification in an asynchronous way, a Task will be launched to perform the operation under the hood.</p><p>To send a notification, the only action needed is a call to the library:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c6ef8d41cda35db90a51cb92ad26f75e/href">https://medium.com/media/c6ef8d41cda35db90a51cb92ad26f75e/href</a></iframe><h3>Sending multiple notifications</h3><p>Sending a standalone notification is cool, but no one needs another library for that. <strong>The true functionality in Ravenx is sending a notification using multiple strategies in an easy way.</strong></p><p>To do that, the library defines a macro that can be used to generate <strong>notification modules</strong>, which can be used to define how to send a particular notification using multiple strategies.</p><p>That modules must define a function called <em>get_notifications_config</em> that receives an object and returns a Keyword list with information of which strategies and which payloads will be used:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed83ebc9a48228a3a3fea28085b0092a/href">https://medium.com/media/ed83ebc9a48228a3a3fea28085b0092a/href</a></iframe><p>So that allows to define all the logic behind a notification generation in a particular module.</p><p>That modules can be used (as seen above) to dispatch the notifications in both synchronous and asynchronous way, as when we dispatched simple notifications.</p><h3><strong>Custom strategies</strong></h3><p>Anyone can create custom strategies and integrate them with Ravenx. To do that, the only requirement is to implement the interface needed and tell Ravenx which modules should use as strategies:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7ffffe28425f744b36381a29697359f/href">https://medium.com/media/e7ffffe28425f744b36381a29697359f/href</a></iframe><h3>What Ravenx doesn’t do</h3><p><strong>Ravenx doesn’t take care of dispatching schedule</strong> (limiting the amount of notifications sent simultaneously). As Erlang VM can virtually take care of a lot (<em>really, a lot</em>) of internal processes, this is not a real issue.</p><p>You can send a lot of notifications asynchronously and the Erlang VM will be able to manage them without much problem.</p><h3>Wrapping up</h3><p>Ravenx is not the perfect solution for all the notification scenarios (and it is not intended to be that), but in <a href="http://www.acutar.io/">Acutario</a> we think this can solve some complex scenarios in which you can have to deal with changing configurations, multiple strategies and massive dispatches of notifications.</p><p>Using notification modules alongside with the configuration options, the code related to notifications can be organised and reused, avoiding repetitions in multiple places of your code.</p><p><strong>If you have any question about Ravenx</strong>, you can read its <a href="https://hexdocs.pm/ravenx/">documentation</a> here, ask any question here or just <a href="https://github.com/acutario/ravenx">open an issue</a>! (Pull requests are welcome too :)</p><p>Hope you enjoyed this article and see you soon (in <a href="http://www.elixirconf.eu/">ElixirConf.EU</a> if you assist!)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f2502a1f272" width="1" height="1" alt=""><hr><p><a href="https://medium.com/acutario/sending-notifications-in-elixir-with-ravenx-1f2502a1f272">Sending notifications in Elixir with Ravenx</a> was originally published in <a href="https://medium.com/acutario">Acutario</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ecto Custom Types, a practical case with enumerize (Rails gem)]]></title>
            <link>https://medium.com/acutario/ecto-custom-types-a-practical-case-with-enumerize-rails-gem-b5496c2912ac?source=rss----6f054250591c---4</link>
            <guid isPermaLink="false">https://medium.com/p/b5496c2912ac</guid>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Rubén Sierra]]></dc:creator>
            <pubDate>Mon, 27 Mar 2017 13:22:18 GMT</pubDate>
            <atom:updated>2017-03-27T13:22:18.452Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*j-3jB4efDCERNaTevzEXkw.png" /></figure><p>We keep on track with the migration of our Rails application to Elixir at <a href="https://www.acutar.io/">Acutario</a>, and here is another article about our trip :-).</p><p>We use a lot the awesome Rails gem <a href="https://github.com/brainspec/enumerize">enumerize</a>. I know that there is an enum type in ActiveRecord now, but I think that enumerize has very interesting functionality like an easy internationalization, forms integration or <strong>multiple values selection</strong>.</p><p>For example, we use it for a setting in with you can select a set of week days. In Rails, we have an enumerized array with the numbers of the week days in Ruby and multiple selection enabled.</p><blockquote>WEEK_DAYS = [0,1,2,3,4,5,6].freeze<br>extend Enumerize<br>serialize :week_days, Array<br>enumerize :week_days, in: WEEK_DAYS, multiple: true</blockquote><p>Ok, let’s go then for the Elixir part. While we are on transition between languages, we need to consume some data with Elixir in the same way that we do in Rails. With this particular case we have some interesting issues.</p><p>First of all, enumerize stores this data as an array of the selected days of the week <strong>serialized as a yaml string</strong>. Secondly, <strong>in Ruby (<em>date.wday</em>), the week starts on Sunday (0) and ends on Saturday (6) but in Erlang (<em>:calendar.day_of_the_week(date)</em>), the week starts on Monday (1) and ends on Sunday (7)</strong>.</p><p>So if we have a setting with the weekend days, the raw data in the database is:</p><blockquote>“ — -\\n- ‘0’\\n- ‘7’\\n”</blockquote><p>We want to get an array of Erlang days in our Elixir code:</p><blockquote>[6,7]</blockquote><p>And do the opposite when we save it to the database.</p><h3>Ecto.Type to the rescue</h3><p><a href="https://hexdocs.pm/ecto/Ecto.Type.html"><em>Ecto.Type</em></a> is an Elixir behavior that lets you create a custom Ecto type to define <strong>how to read and write a field from and to the data storage</strong>. And it is the perfect solution for this scenario.</p><p>Lets go and see how to create a custom type. First, you have to create a module, add the <em>Ecto.Type</em> behavior and implement 4 function callbacks.</p><ul><li><strong>type: </strong>returns the native Ecto type of the field in the data storage, like :integer, :string, {:array, :string}…</li></ul><p>In our example, we have the days stored in a yaml in our database, so we need a <em>:string:</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b3641bf9f83ae2021136f8fd333199d1/href">https://medium.com/media/b3641bf9f83ae2021136f8fd333199d1/href</a></iframe><ul><li><strong>cast:</strong> transforms the input data into the right format for the data storage. This function is called when you cast a struct into a changeset or when you pass arguments to an <em>Ecto.Query.</em> For example, if you need to store an <em>Ecto.Date </em>with different formats from the web forms (like “dd/mm/yyyy”), you can create a cast function for each format and when you create the changeset with the params, it will contain the <em>Ecto.Date:</em></li></ul><blockquote>Ecto.Changeset.cast(%Model{}, %{date: “01/01/2017”},~w(date))<br>#Ecto.Changeset&lt;action: nil,<br> changes: %{date: #Ecto.Date&lt;2017–01–01&gt;}, errors: [],<br> data: #Model&lt;&gt;, valid?: true&gt;</blockquote><p>We want to use an integer array until we write the data into database, so we just need to ensure that we accept valid week days:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d4a53d2e08fd673c93ce69c572021f8/href">https://medium.com/media/8d4a53d2e08fd673c93ce69c572021f8/href</a></iframe><ul><li><strong>load:</strong> process raw data (the Ecto native type) when is read from the data storage and transform it to our custom type.</li></ul><p>We have a yaml stored, so we need a function to parse the yaml, create an integer array and translate each value into Erlang week days (replace day 0 with 7). There are some yaml libraries in Erlang/Elixir like <a href="https://github.com/yakaz/yamerl">yamerl</a>, but we always expect the same structure so we can save us a new dependency and do a simple split.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2a0d22479573330dc34aaad5e191ff3d/href">https://medium.com/media/2a0d22479573330dc34aaad5e191ff3d/href</a></iframe><ul><li><strong>dump:</strong> validate the input data (casted data in the data struct) and transform it into the Ecto native type before is written to the data storage.</li></ul><p>So finally, we have to do the opposite process, translate Erlang week days into Rails week days and compose the yaml that will be stored in the database.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec63a5c501e31bee77ff95eee116b3b2/href">https://medium.com/media/ec63a5c501e31bee77ff95eee116b3b2/href</a></iframe><p>Now we can use the type in the Ecto schema declaration:</p><blockquote>schema “settings” do<br> …<br> field :week_days, WeekDays<br> …<br>end</blockquote><p>And this is it, with this new type we can read and write the <em>week_days</em> field, manage it like a normal integer array in the Elixir code and keep the consistence with the Rails application.</p><p>I hope this can be helpful for a similar case or just for a better understanding of <em>Ecto.Type.</em></p><p>See you in another post! 😉</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b5496c2912ac" width="1" height="1" alt=""><hr><p><a href="https://medium.com/acutario/ecto-custom-types-a-practical-case-with-enumerize-rails-gem-b5496c2912ac">Ecto Custom Types, a practical case with enumerize (Rails gem)</a> was originally published in <a href="https://medium.com/acutario">Acutario</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Decorating Elixir]]></title>
            <link>https://medium.com/acutario/decorating-elixir-af08bd46b2fc?source=rss----6f054250591c---4</link>
            <guid isPermaLink="false">https://medium.com/p/af08bd46b2fc</guid>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Rubén Sierra]]></dc:creator>
            <pubDate>Mon, 27 Feb 2017 09:01:18 GMT</pubDate>
            <atom:updated>2017-03-02T09:25:11.680Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*nqbLxlaW89ENzUP3FsvjLQ.jpeg" /></figure><p>In the last months, my fellows at <a href="http://www.acutar.io/">Acutario</a> and I have been working in rewriting some parts of our Rails application into Elixir language. This is not another article about why we decided to change our technology, or a comparison between languages. It is an article about one of the issues we have dealed with when we changed to the new paradigm.</p><p>Maybe this is not a big issue or it could be trivial for other developers or projects, but we want to take special care with our decisions, even with the simple ones, because we are now out of our comfort zone of expertise.</p><p>We have a lot of logic in the core application that depends on calculated data from our models that can’t be obtained just with queries. Actually, these computations are done on demand and they need data from another models and configurations that can change at any moment. With <a href="https://en.wikipedia.org/wiki/Object-oriented_programming">OOP</a>, we have instance methods in the models that we can call wherever we need. But with <a href="https://en.wikipedia.org/wiki/Functional_programming">FP</a> paradigm, we just have structs when we get our models from database and module functions to transform them.</p><p>The first approach was to do <em>“helper”</em> modules to each model, with functions to calculate each data. We had to call these functions multiple times during the life of a model struct to choose some behaviors, calculate another data, add it into views… This looked something laborious for us (coming from OOP) because we need to spread this data between functions if we want to use it again. Also, the structs of model’s data are <em>defstructs</em> created at compilation time so you can’t append new fields on execution. We want a more natural way to get this data and we want to keep using our models <em>defstructs</em> to take advantage of pattern matching in function calls.</p><p>We tried then a different point of view, something similar to a <a href="https://en.wikipedia.org/wiki/Decorator_pattern"><em>Decorator Pattern</em></a>. First of all, we need to add virtual fields into the schema in order to let us fill the data into the model struct. Then, we made some <em>“decorator”</em> modules to load the virtual attributes using the appropriate <em>“helper”</em> functions so we can use these decorations in a model struct and load the data that we need in each moment.</p><p>For example, we have an user model that can be decorated with an <em>ability</em> field to evaluate user rights, and an <em>absences</em> field with the count of the user absence days in a period of time. We just need to load the user data needed for the calculations:</p><blockquote>user = User <br>|&gt; Repo.get(1) <br>|&gt; Repo.preload(:settings) <br>|&gt; Repo.preload(:periods)</blockquote><p>And pipe the decorator to load the fields that we need in each moment. We can decorate all fields or just some, because not all situations will need all data:</p><blockquote>|&gt; UserDecorator.decorate<br>%User{email: …, ability: :manager, absences: 10}<br># or UserDecorator.decorate(:ability)<br># or UserDecorator.decorate([:absences, :ability])</blockquote><p>This function basically calls the helper function and appends the data to the strutc:</p><blockquote>{user | absences: UserHelper.count_absences(user) }</blockquote><p>We can now use <em>user.abilities</em> and <em>user.absences</em> in the rest of the user struct life, with another calculations, pattern matching, views…</p><p>Finally we have moved all this to <strong>an independent package called </strong><a href="https://hex.pm/packages/decoratex"><strong>decoratex</strong></a>, keeping all configuration in the model declaration:</p><pre>defmodule User do<br>  use Ecto.Schema<br>  <strong>use Decoratex<br><br>  decorations do<br>    decorate_field :ability, :string, &amp;UserHelper.get_ability/1<br>    decorate_field :absences, :integer, &amp;UserHelper.count_absences/1<br>    ...<br>  end</strong><br><br>  schema &quot;users&quot; do<br>    has_many :periods, Period, on_delete: :delete_all<br>    belongs_to :setting, Setting</pre><pre>    field :email, :string<br>    ...</pre><pre>    <strong>add_decorations</strong><br>  end<br>end</pre><p>It is a very simple package at the moment but we have some TODOs to improve it, like white and black lists of fields or let pass params through the decoration functions, like:</p><blockquote>User.decorate(user, absences: [year: 2017]})<br>User.decorate(user, except: :absences)</blockquote><p>In an upcoming article we will write an introduction to Elixir packages and what we learn about Elixir macros and meta programming building this one, so stay tuned!</p><p>See you in the future!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=af08bd46b2fc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/acutario/decorating-elixir-af08bd46b2fc">Decorating Elixir</a> was originally published in <a href="https://medium.com/acutario">Acutario</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>