<?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[Stories by Jasiek Matusz on Medium]]></title>
        <description><![CDATA[Stories by Jasiek Matusz on Medium]]></description>
        <link>https://medium.com/@marahin?source=rss-bb5536ac71d2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*RMGwzcUVQfAm67Cwot6yiQ.jpeg</url>
            <title>Stories by Jasiek Matusz on Medium</title>
            <link>https://medium.com/@marahin?source=rss-bb5536ac71d2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 03:14:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@marahin/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Ruby Quirks]]></title>
            <link>https://medium.com/visualitypl/ruby-quirks-8aa62c96b142?source=rss-bb5536ac71d2------2</link>
            <guid isPermaLink="false">https://medium.com/p/8aa62c96b142</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[ruby-quirks]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Jasiek Matusz]]></dc:creator>
            <pubDate>Wed, 04 Sep 2019 13:10:11 GMT</pubDate>
            <atom:updated>2019-09-10T19:37:16.614Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UWMXgXAMDn5gQU3k3NQI1w.jpeg" /></figure><p>Ruby is a beautiful language to work with. It provides tons of amenities for developers.</p><p>Like every other language, though, Ruby has things that you may not be aware of, or may not fully understand — and these quirks are the subject of today&#39;s blog post.</p><h3>? character literal notation</h3><p>It is a simple notation that allows to create a string out of a character (in Ruby &gt; 2):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/de684bc6f8df6efdf16f3ce1183f35d1/href">https://medium.com/media/de684bc6f8df6efdf16f3ce1183f35d1/href</a></iframe><p><a href="http://docs.ruby-lang.org/">docs.ruby-lang.org</a> does not provide too many details about it:</p><blockquote>There is also a character literal notation to represent single character strings, which syntax is a question mark (?) followed by a single character or escape sequence that corresponds to a single codepoint in the script encoding.</blockquote><p>A person named Josh Cheek <a href="https://www.rubyguides.com/2017/02/weird-ruby/">commented about this in the rubyguides.com article</a>:</p><blockquote>Also, their existence is historical rather than practical, back when chars were still int literals (1.8, IIRC), it was useful to have a syntax for going from the char to the ordinal, which is what this was for. Then when chars became single-letter strings, they made the syntax resolve to that rather than deprecating it and breaking lots of code.</blockquote><h3>__END__ &amp; DATA</h3><p>These keywords hold a very specific functionality, that is not often used — at least in the web development specter of Ruby use cases. Probably it can be used in scripting / CLI tools.</p><h4>__END__ specifies… well, the end of a Ruby program</h4><p>When executing this keyword, nothing past it will be interpreted. Consider this code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/246b5212d921cdfdf82d2fa2b2cbe1c7/href">https://medium.com/media/246b5212d921cdfdf82d2fa2b2cbe1c7/href</a></iframe><p>And then we run it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1aa31b6fca56eb3f5fcbd461524216ce/href">https://medium.com/media/1aa31b6fca56eb3f5fcbd461524216ce/href</a></iframe><h4>DATA</h4><p>DATA allows you to get access to whatever is past __END__. If we slightly modify the code above:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5aa914a9270f2ee51a2b2f8b0d1eecf3/href">https://medium.com/media/5aa914a9270f2ee51a2b2f8b0d1eecf3/href</a></iframe><p>And then execute it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/665c164f0d1189231617df07e288f927/href">https://medium.com/media/665c164f0d1189231617df07e288f927/href</a></iframe><p><a href="https://gist.github.com/Marahin/aef7e934953abb7914af87706f76e677">This gist</a> also should give you an idea of how you can use it.</p><h3>::method</h3><p>Usually, when you see code samples in Ruby all methods are invoked with a . — such as 12.to_s, “1”.to_i, User.create! etc.</p><p>Having said that, it is perfectly valid to call these methods using :::</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5aa3558c0a373c2601ee48eed1ffcd0/href">https://medium.com/media/e5aa3558c0a373c2601ee48eed1ffcd0/href</a></iframe><p>It is not considered as a good practice — the recommended way is obviously to call methods through a . operator.</p><p>However, if you want code obfuscation, you can go crazy using ::. For instance, adding two integers in Ruby is nothing else than calling a #+ method on one of these, and then passing the other one as an argument:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05c1acb8ffd78d4910b44fd56a66cb57/href">https://medium.com/media/05c1acb8ffd78d4910b44fd56a66cb57/href</a></iframe><p>This means…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/974758612d84b89fbbf364a6d0b743f1/href">https://medium.com/media/974758612d84b89fbbf364a6d0b743f1/href</a></iframe><h3>Shovel method on strings — &lt;&lt;</h3><p>Shovel method can be used on instances of String class. It can be provided with both an integer representation of the character (ASCII code) and another string:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/629518639016a3c9d8f9caabf2fd0dd9/href">https://medium.com/media/629518639016a3c9d8f9caabf2fd0dd9/href</a></iframe><h3>redo and retry</h3><p>These methods are similar in their functionality. redo allows you to, well, re-do the current iteration block, where retry repeats whole block.</p><p>Consider this code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/40e7337fe534c9e826e4491232ef152c/href">https://medium.com/media/40e7337fe534c9e826e4491232ef152c/href</a></iframe><p>It does a couple of things:</p><ol><li>Defines theretry_on_raise function that accepts a block.</li><li>Then, if the block raises any exceptions, it is going to retry the block once.</li><li>Exactly 3 times it executes retry_on_raise.</li><li>It randomly passes raise into the block so the method would have a chance to retry.</li><li>Also it redos the iteration if the upcoming index is 2.</li></ol><p>Whether you should use these two — it is hard to say. retry is something I have seen multiple times in Rails applications. I think that when you have a good reason to use it, it is fine — but then redo makes it a bit more complex.<br>The developer would have to mind that it is no longer the whole block being called again, but a single iteration step.</p><p>In other words — I think it really depends on the case.</p><p><a href="https://rubyquicktips.com/post/1122838559/redo-vs-retry">Ruby Quicktips</a> also has quite a few good examples worth checking out.</p><h3>Literals</h3><h4>Numbers</h4><p>Binary 1111is 15decimal. Ruby supports specifying values in different number systems by passing a proper prefix:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f40600c7cbd3789c409b0a8e39dd1fe/href">https://medium.com/media/6f40600c7cbd3789c409b0a8e39dd1fe/href</a></iframe><p>d or D stands for decimal, x or X for hexadecimal, 0 or 0o stands for octal, binary b. The alphabetic part of the prefix is case insensitive, hence it can but does not have to be capitalized.</p><p>Another interesting part in number literals is the underscore separator. The documentation does not specify any explicit usage for it, except for human readability improvements.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6425819c8d300a410f7f01ecd165aedf/href">https://medium.com/media/6425819c8d300a410f7f01ecd165aedf/href</a></iframe><p>It is super nice to use it when you deal with constants that specify a big number. It makes it harder to do human errors such as missing a single 0 in a big number.</p><h4>Ranges</h4><p>You must have used some kind of range at a given point while working with Ruby. Maybe using rand(1..10) or an iteration of some kind.</p><p>But do you know the difference between ... and .. range? Its pretty simple: 1..10 is an inclusive range (starts from 1, ends at 10), and 1...10 is an end-exclusive range (ends at 9).</p><h4>Symbols</h4><p>While it may be common knowledge for most of you, I have experienced that some people do not realize you can actually put string interpolation into symbols.</p><p>In the wrong hands, it may be a terrible idea, since symbols are not garbage collected — but sometimes it is really useful.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb6112d80669095d1afdecfebdbb171d/href">https://medium.com/media/cb6112d80669095d1afdecfebdbb171d/href</a></iframe><p>Ruby has pretty good documentation for literals, so if you are interested in seeing more, head over to <a href="https://docs.ruby-lang.org/en/2.0.0/syntax/literals_rdoc.html">docs.ruby-lang.org</a>!</p><h3>Proc &amp; .[]()</h3><p>In <a href="https://engineering.culturehq.com/posts/2019-02-14-weird-ruby">CultureHQ’s Weird Ruby</a> I had stomped upon something that I did not know myself.</p><p>Usually when handling Procs, I just called them by .call method — seems verbose, explicit and understandable to any developer that might come after me.</p><p>Yet it seems that you can also call Proc by square brackets method ([something]).</p><p>It made me think so I dug into Class: Proc documentation, and found out there are several ways to call a Proc instance:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99d952fc9f7275a9aa07b18cd458e926/href">https://medium.com/media/99d952fc9f7275a9aa07b18cd458e926/href</a></iframe><p>In general, it is a good practice to write code that can be understood by others — be it your team members or developers that may take over the project eventually. Hence I would say that .call or .yield would be the way to go here, as personally I would have had no idea on what [“Thomas”, 23] really does.</p><p>I think that there is a good chance I am not the only one and .call would make it way more explicit.</p><p>I hope this article has raised at least a couple of eyebrows. <br>Ruby is an awesome tool — but like any other, it has its weird moments. <br>Feel free to share your thoughts in the comments section!</p><p>If you’re interested in more blog posts like this please visit our <a href="http://www.visuality.pl/posts">Visuality Blog</a> or our <a href="https://medium.com/visualitypl">Medium Page</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8aa62c96b142" width="1" height="1" alt=""><hr><p><a href="https://medium.com/visualitypl/ruby-quirks-8aa62c96b142">Ruby Quirks</a> was originally published in <a href="https://medium.com/visualitypl">Visualitypl</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JSON:API consumption in Rails]]></title>
            <link>https://medium.com/visualitypl/json-api-consumption-in-rails-8809511247a7?source=rss-bb5536ac71d2------2</link>
            <guid isPermaLink="false">https://medium.com/p/8809511247a7</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[json-api]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Jasiek Matusz]]></dc:creator>
            <pubDate>Mon, 06 May 2019 12:39:23 GMT</pubDate>
            <atom:updated>2019-05-06T12:39:23.577Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1TygqUccnplUo7Hzow1Z3w.jpeg" /></figure><p>In the world of modern web applications you cannot avoid becoming a heavy user of APIs. Consumption of different interfaces brings the experience, and experience brings knowledge.</p><p>One of the many things we learned at <a href="http://www.visuality.pl/">Visuality</a> is that its not only okay to try new things, but it is also even better if other people tried these and have some insights on how to actually do it the <em>good way</em>. Over time we researched and developed ways to introduce JSON:API into our Rails applications, so that we could be compliant with this specification, thanks to which the development process would be more effective and time efficient.</p><p>If you would like to learn more about JSON:API consider checking out Nadia’s article about <a href="http://www.visuality.pl/posts/json-api-versus-the-nih-syndrome">JSON:API and NIH</a> syndrome.</p><p>Let’s get down to business now, and as a backend engineer at Visuality, I will be your guide today!</p><h4>Shortcoming</h4><p>There are many implementations of this standard (you can find a comprehensive list <a href="https://jsonapi.org/implementations/">here</a>), but what we recognised is that most of them usually work one-way: to serialise the data we have in the Rails application so it can be served to the API consumer. The major shortcoming was that there was no go-to solution for receiving and understanding the input the consumer sends to our application.</p><p>The ones that supposedly were to support input deserialisation either did not work 100%, were abandonware or weren’t supporting JSON:API specification in its entirety.</p><p>It was a dead end.</p><h3>Innovation</h3><p>It lead us to a point where we decided that maybe it is a good idea to create something of our own. We had a simple goal: <strong>our Rails application must read JSON:API compliant input and handle it</strong>. It would be the best if it did not require huge configuration overhead and if it didn’t force people to learn and write code in a very different (compared to what is acknowledged as standard, Rails-way) convention.</p><p>The input that JSON:API specification describes is pretty simple. Take a look at a sample:</p><pre><strong>POST /photos HTTP/1.1</strong><br>Content-Type: application/vnd.api+json<br>Accept: application/vnd.api+json<br><br>{<br>  &quot;data&quot;: {<br>    &quot;type&quot;: &quot;photos&quot;,<br>    &quot;attributes&quot;: {<br>      &quot;title&quot;: &quot;Ember Hamster&quot;,<br>      &quot;src&quot;: &quot;http://example.com/images/productivity.png&quot;<br>    },<br>    &quot;relationships&quot;: {<br>      &quot;photographer&quot;: {<br>        &quot;data&quot;: { &quot;type&quot;: &quot;people&quot;, &quot;id&quot;: &quot;9&quot; }<br>      }<br>    }<br>  }<br>}</pre><p>Lets break down and note a few things:</p><ul><li><strong>Content-Type — </strong>JSON:API has its own mime type registered: application/vnd.api+json, and specification requires all requests to provide it,</li><li>Request <strong>body</strong>. We can see that the main key is <strong>data</strong>, which holds resources informations.</li><li><strong>Resource</strong> — it is identified by a <em>type</em>, <em>attributes</em> and optional <em>relationships</em> (which, again, are resources).</li></ul><p>This input will not work with what you would typically write inside a Rails controller to handle parameters. Instead, this is how it would look like:</p><pre>{<br> &quot;photo_parameters&quot;: {<br>  &quot;title&quot;: &quot;Ember Hamster&quot;,<br>  &quot;src&quot;: &quot;<a href="http://example.com/images/productivity.png">http://example.com/images/productivity.png</a>&quot;,<br>  &quot;photographer&quot;: { &quot;id&quot;: 9 }<br> }<br>}</pre><p>For a person that had been working with Rails over the years, but not with JSON:API, I would not be surprised if you were in favour of the latter example. However, JSON:API provides lots of great functionalities and features that would not be so easy to achieve on your own (and then, you would have to educate your consumers on how to use these).</p><p>At Visuality the choice was made that JSON:API is the way we want to go for now — as it was either that, or the implemented-here solutions that varied in features between different projects. JSON:API gave us a good, unified solution that had every scenario that we needed. Returning data in this format was easy, as you had ActiveModelSerializers or fast_jsonapi from Netflix, but there was no way to easily understand the input client would send us. <br>But since we already could compare the JSON:API compliant input and the Rails expected one, why not get the best out of two worlds? Why should not we let Rails developers keep using their parameters as they always had done, while allowing JSON:API consumers to actually consume and feed the server with JSON:API data?</p><h3>JsonApi::Parameters</h3><p>That is why we brought a new library to life. Our gem aims to support Rails developers in consuming JSON:API input. It is mostly just a translator, that transforms standard compliant input to what Rails (and Rails developers) expect. Considering a sample params call in a controller:</p><pre>params.require(:photo).permit(photographer: [:id])</pre><p>The only thing you need to consume the JSON:API version of it is prepend <em>require with </em>from_jsonapi:</p><pre>params.from_jsonapi.require(:photo).permit(photographer: [:id])</pre><p>What this function does is literally translating the JSON:API input into a new instance of ActionController::Parameters that has a form of standard Rails model parameters.<br>The specification has things that may be considered quirks, especially when it comes to creating new related resources — where you have <em>relationships</em> key, and <em>included</em> key (one holds the resource identifier and its type, second holds the resources attributes object). We have done our best to handle these as well.</p><p>We did not want to limit our users to Rails only, so you can also use this gem in vanilla Ruby.</p><p>Additionally, if you remember from the things we have noted down above, there was this specific JSON:API <em>Content-Type. </em><strong>JsonApi::Parameters </strong>registers this mime type for you, in a way that should not break your Rails app (obviously you can unregister the Mime Type yourself, if you would like to).</p><p>So far we have been using this library in production for a couple of months now and it has proven handy, so we are looking forward to maintain and extend it. Actually, we have already had a couple of feature requests (one being the <em>Mime::Type </em>registration process) that we were happy to handle and implement, so definitely share your insights with us in the <a href="https://github.com/visualitypl/jsonapi_parameters">Github repository</a>!</p><p>If you’re interested in more blog posts like this please visit our <a href="http://www.visuality.pl/posts">Visuality Blog</a> or our <a href="https://medium.com/visualitypl">Medium Page</a></p><h3>Resources</h3><ul><li><a href="https://jsonapi.org/">jsonapi.org</a> — JSON:API specification</li><li><a href="https://rubygems.org/gems/jsonapi_parameters">jsonapi_parameters</a> gem</li><li><a href="http://www.visuality.pl/posts/json-api-versus-the-nih-syndrome">JSON:API versus the NIH syndrome</a></li><li><a href="https://jsonapi.org/implementations/">list of JSON:API implementations</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8809511247a7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/visualitypl/json-api-consumption-in-rails-8809511247a7">JSON:API consumption in Rails</a> was originally published in <a href="https://medium.com/visualitypl">Visualitypl</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ruby’s (not so) constants]]></title>
            <link>https://medium.com/cyfrowy-humanizm/rubys-not-so-constants-1025357947d9?source=rss-bb5536ac71d2------2</link>
            <guid isPermaLink="false">https://medium.com/p/1025357947d9</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[development]]></category>
            <dc:creator><![CDATA[Jasiek Matusz]]></dc:creator>
            <pubDate>Tue, 06 Nov 2018 13:10:59 GMT</pubDate>
            <atom:updated>2018-12-06T22:44:14.935Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-sa/4.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vsq8MsXwxwVkQkWOKwT3zA.png" /><figcaption>Alexander Grothendieck, standing at the blackboard, during a lesson in the 1960s at IHES (High Scientific Studies Institute) near Paris. (High Scientific Studies Institute via AP) (source: <a href="https://www.washingtonpost.com/national/health-science/alexander-grothendieck-mathematical-genius-who-went-into-self-exile-dies-at-86/2014/11/15/1e65f69a-6cf2-11e4-9fb4-a622dae742a2_story.html?noredirect=on&amp;utm_term=.6439af63689e">Washington Post</a>)</figcaption></figure><p>Ruby’s constants are somewhat of an entertaining surprise if you have never dug into this subject before, or seen Ruby at all. See, unlike many other languages, constants in Ruby are not really enforced to stay constant during the program runtime. <em>Wait, what?</em><br>If you try to re-assign the constant, you will get a warning though:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0612af2c666b3810ed27da72964ab94c/href">https://medium.com/media/0612af2c666b3810ed27da72964ab94c/href</a></iframe><p>But, as you can see, A ‘s value has not remained constant, instead of [1], we get [2]. <br>Even for the warning, there’s a <em>workaround</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c4aabab8b32acc3fd553488682be1ea2/href">https://medium.com/media/c4aabab8b32acc3fd553488682be1ea2/href</a></iframe><p>The two operations are different though, as in the first gist we are assigning entirely new object to the constant A, while in the latter example we are pushing new object to an existing array. We are not going to cover locking constant names to a certain object, thus blocking them from any re-assignments, we’ll focus on object changes instead.</p><p>The solution will give you the <strong><em>chills</em></strong>.</p><h4>Say hi to .freeze</h4><p>.freeze allows you to enforce immutability on mutable objects. Since constant doesn’t force immutability on its value, you need to declare it explicitly, like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a4a955bdb03c8224db6330a5c795d063/href">https://medium.com/media/a4a955bdb03c8224db6330a5c795d063/href</a></iframe><h4>Okay, but what does it mean?</h4><p>Freezing means enforcing immutability. I think that the easiest example is the array one.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bac8f2b7041e3c0c66578c046301b2de/href">https://medium.com/media/bac8f2b7041e3c0c66578c046301b2de/href</a></iframe><p>Consider having an Array object under the constant named A. You are still able to modify value of this constant without any warnings, using the <a href="http://ruby-doc.org/core-2.5.1/Array.html#method-i-3C-3C">append method</a> ( &lt;&lt; ).<br>This works because technically the A constant has not been re-assigned. It is the content of the object that has changed, but not the object itself. A proof of this is the object_id:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6793e1ee6c2e53f2d323740c6dc630b8/href">https://medium.com/media/6793e1ee6c2e53f2d323740c6dc630b8/href</a></iframe><p>Immutability allows us to lock a certain object from any changes. On attempts of changing content of a frozen object, Ruby will throw a FrozenError:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c1073c0c3d31ca08e3d3a2c1f918bcbd/href">https://medium.com/media/c1073c0c3d31ca08e3d3a2c1f918bcbd/href</a></iframe><h4>Freeze all the things!</h4><p>Yes. See, freezing everything around is not really a bad thing. Actually it makes constants closer to what we would expect — a named value that should not / can not be changed like any other ordinary variable.</p><p>If you develop web applications in Rails, and you have some configuration, constants in models or controllers, or just plain constants in your Ruby scripts — please, .freeze them! It is going to make it easier for you and others to maintain the code, and it will keep you sane.</p><p>There are some caveats that you are going to encounter later on, also, there will be situations where you will be unsure if you should freeze something or not. Lately, during a code review for my colleague (but before I started reading about freezing and immutability), I encountered a code that was similar to this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fb06ff6166d43b37415dbfc4429466f9/href">https://medium.com/media/fb06ff6166d43b37415dbfc4429466f9/href</a></iframe><p>By default, when I see a constant declaration, I type “Consider .freeze -ing the value there”, or something like that, in the review suggestions. Not only this was tiresome for me, even <a href="https://github.com/amatsuda">amatsuda (Ruby/Rails committer)</a> has stated that this is an ugly practice. Outcome of this was <a href="https://bugs.ruby-lang.org/issues/8976">a plead to reconsider the magic comment</a>. I will come back to this subject later.</p><p>Anyways, when I pushed the request for changes, response was a question: <em>“isn’t a constant made of two other frozen constants frozen by default?”</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65e6915068208dc9e97b5bdcbbfffc47/href">https://medium.com/media/65e6915068208dc9e97b5bdcbbfffc47/href</a></iframe><p>If you think for a moment, it would make sense for it to be frozen by default, without having to explicitly declare the immutability. However, if you think just a bit harder, you’ll realise that’s not how it’s gonna work.</p><p>After all, when you do A + B, you are running #+(arg) method on A constant. While its value is frozen indeed, and cannot be changed, it still can be read. #+ result will be an entirely new object, with a new object_id, that is mutable (again), even though it was created from two frozen (immutable) values.</p><p>Furthermore, if you have an array, with some contents, are those contents, elements, also frozen?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/756a4d6cfa39dbaeed3212bcd7726c55/href">https://medium.com/media/756a4d6cfa39dbaeed3212bcd7726c55/href</a></iframe><p>Since we are freezing the array, we are not allowed to put any more objects into it. However, as strings are mutable objects, we are still allowed to operate on the object itself, as it is not frozen.</p><p>In order to make an entirely frozen array of strings, you would have to do the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97b053706c003dd4ce6ddfef3e293e9b/href">https://medium.com/media/97b053706c003dd4ce6ddfef3e293e9b/href</a></iframe><p>Which is not the perfect solution — it’s neither pretty or handy for the programmer. Imagine having a nested array, or a Hash that was created from a JSON response. <strong>This is why other Rubyists brought you<em> </em></strong><a href="https://github.com/dkubb/ice_nine"><strong><em>ice_nine</em></strong></a><strong> and</strong><a href="https://gist.github.com/steakknife/1a37057b3b8539f4aca3"><strong> this nice gist from <em>steakknife</em></strong></a><strong><em>.</em></strong></p><h4>Fin!</h4><p>But before…<br>I mentioned that there happened to be a plead to include <em>a magic comment. </em>Eventually this is supported, and you can force all string literals to be frozen by default, by putting that line of code at the top of your Ruby script:</p><pre> # frozen_string_literal: true</pre><p><a href="https://stackoverflow.com/a/37799399">This answer on StackOverflow</a> also gives a bit of an insight on how that works.</p><p>What is even better, is that in Ruby3.0 all string literals will be frozen by default, without need of having to put the <em>magic comment</em> at the top of the file.</p><p>I recommend giving <a href="https://www.rubypigeon.com/posts/a-review-of-immutability-in-ruby/">“A Review Of Immutability In Ruby” from RubyPigeon.com</a> a read, as it nicely describes some of the issues you may encounter while discovering freezing, cloning/duplicating objects along with finding nice pre-existing solutions to those problems.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1025357947d9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cyfrowy-humanizm/rubys-not-so-constants-1025357947d9">Ruby’s (not so) constants</a> was originally published in <a href="https://medium.com/cyfrowy-humanizm">marahin</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why am I dumping dedicated servers]]></title>
            <link>https://medium.com/cyfrowy-humanizm/why-am-i-dumping-dedicated-servers-bac709e37e62?source=rss-bb5536ac71d2------2</link>
            <guid isPermaLink="false">https://medium.com/p/bac709e37e62</guid>
            <category><![CDATA[self-hosted]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[web-hosting]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[efficiency]]></category>
            <dc:creator><![CDATA[Jasiek Matusz]]></dc:creator>
            <pubDate>Sat, 10 Mar 2018 13:44:25 GMT</pubDate>
            <atom:updated>2018-05-09T12:18:50.468Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<h4>and why should you — part 1</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JVmZNEVK0iaczg9q3VGvxw.jpeg" /><figcaption>Being mobile was difficult back in the day (yes, completely unrelated).</figcaption></figure><iframe src="https://cdn.embedly.com/widgets/media.html?type=text%2Fhtml&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;schema=instagram&amp;url=https%3A//www.instagram.com/p/BUXLNkRFQJP/%3Fhl%3Dpl&amp;image=https%3A//i.embed.ly/1/image%3Furl%3Dhttps%253A%252F%252Fscontent-iad3-1.cdninstagram.com%252Fvp%252F8f78e60f3e9e18ca3e46611081ef285f%252F5B323AB9%252Ft51.2885-15%252Fs640x640%252Fsh0.08%252Fe35%252F18580667_271843766611476_5872162094060666880_n.jpg%26key%3Da19fcc184b9711e1b4764040d3dc5c07" width="640" height="640" frameborder="0" scrolling="no"><a href="https://medium.com/media/199fffcdc299d30907d85e3d1d0e10b0/href">https://medium.com/media/199fffcdc299d30907d85e3d1d0e10b0/href</a></iframe><p>For the past few years I loved tinkering with servers. I liked having a machine, far, far away that has a huge performance potential to use.</p><p>I liked the fact that any service that can be self-hosted is in my reach. <br>When I discovered <a href="http://sentry.io/">Sentry</a>, first thing I checked is if they allow self hosting free of cost. And they did! So since then I was doing that constantly to any new software that I discovered.</p><p>Eventually, I’ve ended up with a one huge dedicated server (<em>huge </em>assuming that it was entirely for pet project purposes<em>) </em>that had stacked multiple self hosted services on top of it. To make stuff more sophisticated, the server was divided into virtual machines; one per programming language / purpose. We (as I shared it with a group of friends — a “userspace” that we call <a href="http://3lab.re/">3lab.re</a>) have had a Ruby machine (running Ruby scripts, Rails apps, Sinatra apps, …), web (which contained both PHP, static and Dockerized websites), Gitlab, Gitlab CI, Sentry and so on, and so on…</p><h4>It was great to have an alternative</h4><p>Don’t get me wrong. I really enjoyed having an alternative when I had to choose between Github and Gitlab. I really liked the fact that I had my own Nextcloud instance that I could push all the files to, and then share them to my friends.</p><p>Being in control feels and <em>is </em>awesome, <strong>as long as everything<em> works. </em></strong>But there are times when things break, and then it gets ugly.</p><h4>Breaking point</h4><p>For the first few months the stack was in a phase where things were working, better or worse. It was obvious for me that I had to spend some time each week tinkering with it, optimising it, learning how to do stuff better and how to handle multiple configurations using Ansible Playbooks sysadmin friend had provided.</p><p>But the thing is that this phase never stopped. It’s been around a year now, and I still have to do some maintenance each week. I have to update the virtual machines; I sometimes feel the urge to check if the Rails app didn’t crash. I have to check if systemd units haven’t failed me when it comes to autorestarting. I feel obliged to make sure that there’s enough disk space for the log files; I’m afraid that the Postgres might deadlock because it’s being used in some old Rails app that had not closed the transactions if Rack middleware aborted the request handling.</p><p>As a developer I try to constantly learn new things. I tend to have bilions of ideas each day, and some of those I feel very eager to at least take a shot and implement. It’s the idea of pushing forward: experiencing attempts and failures, until you succeed.</p><p>I realized three things while working with my self-hosted stack:</p><ul><li>I wasted time during the weekends / after work to make sure everything was working properly (e.g. server was OK, virtual machine did not run out of memory, etc.),</li><li>I wasted time trying to fix the way applications are running or are managed, instead of fixing the applications!,</li><li>I paid money and still did not feel safe about things I do there,</li><li>I felt guilt if something went down.</li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?type=text%2Fhtml&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;schema=instagram&amp;url=https%3A//www.instagram.com/p/BVM44eiFlOj/%3Fhl%3Dpl&amp;image=https%3A//i.embed.ly/1/image%3Furl%3Dhttps%253A%252F%252Fscontent-iad3-1.cdninstagram.com%252Fvp%252F7e8f9114fac3c738540eaf610b7690a4%252F5B400168%252Ft51.2885-15%252Fs640x640%252Fsh0.08%252Fe35%252F19051382_273485283059375_7708250129085497344_n.jpg%26key%3Da19fcc184b9711e1b4764040d3dc5c07" width="640" height="640" frameborder="0" scrolling="no"><a href="https://medium.com/media/a0fdf15c49487a4e185794d3b9be01e5/href">https://medium.com/media/a0fdf15c49487a4e185794d3b9be01e5/href</a></iframe><h3>Decision</h3><p>In my opinion, when you have an idea that you pursue — your focus should be on making as many attempts to implement it as possible. Bringing external disruptors, making stuff more sophisticated only slows you down and forces you to spend time on fixing stuff that is not even related to the whole project.</p><p>Self hosting was killing my productivity. I was worrying about things that could be handled easier and way more professionally.</p><p>In the next part that I’ll publish (eventually) I’ll explain how did I approach migrating each service that I’ve had in the stack. I’ll also explain how did I cut costs. <strong>If you liked this story, please consider leaving a follow so you don’t miss the sequel!</strong></p><blockquote>Thanks to Agnieszka (<a href="https://www.instagram.com/chuffek/?hl=pl">Chuffi</a>) for various food pictures.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bac709e37e62" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cyfrowy-humanizm/why-am-i-dumping-dedicated-servers-bac709e37e62">Why am I dumping dedicated servers</a> was originally published in <a href="https://medium.com/cyfrowy-humanizm">marahin</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ruby — Convert Array to Hash]]></title>
            <link>https://medium.com/cyfrowy-humanizm/ruby-array-to-hash-74fd7501b011?source=rss-bb5536ac71d2------2</link>
            <guid isPermaLink="false">https://medium.com/p/74fd7501b011</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Jasiek Matusz]]></dc:creator>
            <pubDate>Fri, 22 Sep 2017 14:14:34 GMT</pubDate>
            <atom:updated>2017-09-22T14:40:20.321Z</atom:updated>
            <cc:license>http://creativecommons.org/publicdomain/zero/1.0/</cc:license>
            <content:encoded><![CDATA[<h4>with Splat operator</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*onqZycyLDDVfxclqLhq7Jg.jpeg" /><figcaption><a href="http://searcharchives.vancouver.ca/b-c-telephone-operator">B.C. Telephone OPERATOR (pun intended! sorry) (Public Domain)</a></figcaption></figure><h3>The magic * operator</h3><h4>Here be TLDR; a simple explanation is down below.</h4><p>Hash[*[&#39;key_1&#39;, &#39;value_for_key_1&#39;, &#39;key_2&#39;, &#39;value_for_key_2&#39;]]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*HFaO0DeMswx2siYmRnMEyw.jpeg" /></figure><h4>wat</h4><p>Even though <a href="http://www.monkeyandcrow.com/blog/the_strange_ruby_splat/">it can be used in many ways</a>, the only real example of a great use case for me was mapping array to a hash.</p><p>For instance, let’s assume that you’re fetching some API data — some kind of currency service that will return information about few currencies and per dollar ratio:</p><pre>[<br>  { currency: &#39;USD&#39;, per_dollar: &#39;1&#39; },<br>  { currency: &#39;PLN&#39;, per_dollar: &#39;4.23&#39;},<br>  ...<br>]</pre><p>As a Ruby developer it would be easier for you to create a simple look-up Hash from that kind of data. You’d like to have access to something like this in your code:</p><pre>{ &#39;USD&#39; =&gt; &#39;1&#39;, &#39;PLN&#39; =&gt; &#39;4.23&#39;, ... }</pre><p>One might say that the following code would do the job:</p><pre>our_data = Hash.new<br>received_data.each do |data_el| <br>  our_data[data_el[:currency]] = data_el[:per_dollar]<br>end</pre><pre>[19] pry(main)&gt; our_data<br>=&gt; {&quot;USD&quot;=&gt;&quot;1&quot;, &quot;PLN&quot;=&gt;&quot;4.23&quot;}</pre><p>Splat operator would make it way easier:</p><pre>Hash[* received_data.map(&amp;:values).flatten]<br>=&gt; {&quot;USD&quot;=&gt;&quot;1&quot;, &quot;PLN&quot;=&gt;&quot;4.23&quot;}</pre><p>So what’s happening here, is:</p><ul><li>you create an Array out of received_data ‘s elements — an Array of values (for instance, [&#39;PLN&#39;, &#39;4.23&#39;] ), this makes up an array of (two-element) arrays [[&#39;PLN&#39;, &#39;4.23&#39;], [&#39;USD&#39;, &#39;1.0&#39;], ...] ,</li><li>then, you flatten it (so [[&#39;PLN&#39;, &#39;4.23&#39;], [&#39;USD&#39;, &#39;1.0&#39;]] becomes [&#39;PLN&#39;, &#39;4.23&#39;, &#39;USD&#39;, &#39;1.0&#39;]) ,</li><li>now Splat operator happens. Without the splat operator the notation would be Hash[&#39;PLN&#39;, &#39;4.23&#39;, &#39;USD&#39;, &#39;1.0&#39;] (if you do not trust me, try it in your irb or pry — it will work!). It is required though, because it makes the Array we have to be passed as a list of arguments, and not a single Array argument. Without the splat operator Hash[array] would translate to Hash[[&#39;PLN&#39;, &#39;4.23&#39;, &#39;USD&#39;, &#39;1.0&#39;]], so Hash would understand that as a single argument, and not a list of arguments (which we need for it’s initialization).</li></ul><h3>Should you use it?</h3><p>Conversion ( Array to Hash ) wise — yes, I think it’s a good choice. If coders knew how it works it would make stuff cleaner and easier, less code is always nice. And it’s easier to use so too, it’s cool.</p><p>However in other use cases (mentioned in <a href="http://www.monkeyandcrow.com/blog/the_strange_ruby_splat/"><em>The Strange Ruby Splat</em></a> that I’ve linked to once already) I am not so sure. Using it to put a String in Array doesn’t seem nice ( a = *&quot;Blah&quot; is equal to a = [&quot;Blah&quot;] — now, what would be more readable?). I guess it’s always <strong>up to you and your needs.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=74fd7501b011" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cyfrowy-humanizm/ruby-array-to-hash-74fd7501b011">Ruby — Convert Array to Hash</a> was originally published in <a href="https://medium.com/cyfrowy-humanizm">marahin</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to reset TeamSpeak’s query password]]></title>
            <link>https://medium.com/cyfrowy-humanizm/how-to-reset-teamspeaks-query-password-e017ff182eb5?source=rss-bb5536ac71d2------2</link>
            <guid isPermaLink="false">https://medium.com/p/e017ff182eb5</guid>
            <category><![CDATA[server-query]]></category>
            <category><![CDATA[teamspeak-admin]]></category>
            <category><![CDATA[teamspeak]]></category>
            <category><![CDATA[telnet]]></category>
            <category><![CDATA[teamspeak-privilege]]></category>
            <dc:creator><![CDATA[Jasiek Matusz]]></dc:creator>
            <pubDate>Wed, 26 Jul 2017 08:31:01 GMT</pubDate>
            <atom:updated>2017-09-22T14:19:50.319Z</atom:updated>
            <cc:license>http://creativecommons.org/publicdomain/zero/1.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/547/1*bM-Jnmc1pbJmX9dShD6Tiw.png" /></figure><p>I often forget my query password. Especially when I reset my desktop computer, format a drive or do anything related to data loss.</p><p>This is supposed to be a quick walkthrough on how to approach the issue and retrieve full privileged group.</p><ol><li><strong>If you have access to server query (over telnet), go to point 6.</strong></li><li>If you have the password, but not the login, remember that default telnet username is <em>serveradmin</em>.</li><li>Edit file named <em>ts3server_startscript.sh</em>, find “<em>COMMANDLINE_PARAMETERS</em>”, and append “<em>=$2</em>” to it.</li><li>Stop the server, and start it with following command: “<em>./ts3server_startscript.sh start serveradmin_password=”YOUR_PASSWORD_HERE”</em>. Voila, query password has been reset.</li><li>Revert changes made at point 3.</li><li>Login using telnet:</li></ol><pre>telnet server.address.com 10011<br>login client_login_name=serveradmin client_login_password=YOUR_PASSWORD_HERE</pre><p>If you get error id=1538 msg=invalid\sparameter then try with:<br>login client_login_name=serveradmin client_login_password=PASSWORD<br>- this one should work, but this has changed over versions and may vary.</p><h3>We can now proceed to add a unique, one-time use token that will grant all privileges to the user that uses it.</h3><pre>tokenadd tokentype=0 tokenid1=<strong>X</strong> tokenid2=0</pre><p><strong>X </strong>is a group number that you want to create the token for. When token is used, user will be granted this exact group. For <em>Server Query Admin</em> (full root privilege) it is <strong>2 </strong>(however this may vary through versions). <em>Server Admin </em>(“silver shield”) is <strong>6</strong>.</p><p>You should receive something like this as a response:</p><pre>token=<em>(...)</em></pre><p>Which you should input in “Privilege key” section in TeamSpeak’s client.</p><p>And that is all, folks! Enjoy your cool TeamSpeak privilege.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e017ff182eb5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cyfrowy-humanizm/how-to-reset-teamspeaks-query-password-e017ff182eb5">How to reset TeamSpeak’s query password</a> was originally published in <a href="https://medium.com/cyfrowy-humanizm">marahin</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Golang’s Robotgo library — handle sequential mouse click]]></title>
            <link>https://medium.com/cyfrowy-humanizm/golangs-robotgo-library-handle-sequential-mouse-click-6e476225887c?source=rss-bb5536ac71d2------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e476225887c</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[robotgo-mouse]]></category>
            <category><![CDATA[go-mouse-click]]></category>
            <category><![CDATA[robotgo]]></category>
            <category><![CDATA[emulation]]></category>
            <dc:creator><![CDATA[Jasiek Matusz]]></dc:creator>
            <pubDate>Tue, 25 Jul 2017 13:21:12 GMT</pubDate>
            <atom:updated>2017-07-25T13:21:12.382Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/491/1*jLsiafBsj4Jietv7utXcww.png" /></figure><p>If you are interested in Golang’s development, you most likely heard about <a href="https://github.com/go-vgo/robotgo">go-vgo/robotgo</a> — a very useful library that allows you to automate tasks and handle various desktop / window / event specific actions with Go.</p><p>I personally used it couple of times for a very repetitive tasks, such as… well, video games; for instance in a game where you have to cast spells to get your magic skill going. I made go cast the spells for me.</p><p>But this post is not about the library in general. I just wanted to share something that I find not really intuitive — handling a sequential mouse click.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*wwByRftor2HIPUsZDPr1Ig.gif" /></figure><p>I had a case where I wanted the program to be usable for both me and my significant other — it was coded on a MacBook Pro 13&quot;, but had to be usable on Windows PCs with several full HD monitors. <br>The input was several points on the screen, and after providing them the program should have started emulating clicks on all of them + do some other stuff that I won’t explain in this post.</p><h4>Go is fast. Very fast. It was certainly faster than I expected.</h4><p>Using the code provided in the documentation I was able to handle one mouse click and get position for it. However whenever I added one, two, or N number of clicks afterwards (<em>sequentially</em>, we’re not talking about a loop here but literally copy paste approach) the behavior would not change — it always expected one and only click.</p><p>So one click would be able to be recognized as all of those sequential clicks by Go; and I suppose that is because Go is just so fast. I have not researched it though, I did not care enough to dig in and I am definitely <strong>not</strong> a golang specialist. I hope to learn more about it, and I am able to code stuff in it, but my knowledge is not enough to understand what exactly happened there. Sorry.</p><h4>What do we do if stuff is too fast? We slow it down. Barbaric.</h4><p>When I encountered the issue first thing I did was to literally throw a time.Sleep in there to see if that would help. And guess what, it actually did. Sad thing is that I did not find any other way to make it hand-able, so currently in above mentioned case this is the function that I use for handling a left mouse button click:</p><pre>func getLMouseClick() (int, int) {<br>    mleft := robotgo.AddEvent(&quot;mleft&quot;)<br>    if mleft == 0 { }<br>    time.Sleep(100 * time.Millisecond)<br>    return robotgo.GetMousePos()<br>}</pre><h3>This is how I felt:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/364/1*6VP8iHqhQ5seAg3eNamGeA.png" /></figure><p>But as this was not a priority for me, and 100ms isn’t anything “noticable” in initialization stage that happens once per runtime, I did not care enough to dig more. It works.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e476225887c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cyfrowy-humanizm/golangs-robotgo-library-handle-sequential-mouse-click-6e476225887c">Golang’s Robotgo library — handle sequential mouse click</a> was originally published in <a href="https://medium.com/cyfrowy-humanizm">marahin</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Golang and Postgres auto-reconnect]]></title>
            <link>https://medium.com/cyfrowy-humanizm/golang-postgres-auto-reconnect-4b5e5cc09a39?source=rss-bb5536ac71d2------2</link>
            <guid isPermaLink="false">https://medium.com/p/4b5e5cc09a39</guid>
            <category><![CDATA[database]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Jasiek Matusz]]></dc:creator>
            <pubDate>Thu, 15 Jun 2017 16:18:10 GMT</pubDate>
            <atom:updated>2017-06-15T16:18:46.843Z</atom:updated>
            <content:encoded><![CDATA[<p>When you write software that is supposed to be running constantly (24/7), and it also makes use of Postgres database, <strong>you want to make sure that it won’t fail randomly if the connection was dropped</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*b5dd75H-fnjCCOMXlfTdcw.png" /></figure><p>Make it a Postgres fault, ISP, any of the sides — if your connection goes down, you don’t want to have to restart the program to make it connect to the database again.</p><p>Usual flow that I’ve been using is to create a simple function that will make sure the connection exists (if it does not — start it and check again), and return it. It’s kinda Ruby way, where you pop a function name and then methods on it, basicly operating on an object, but non-explicitly defined.</p><p>This also allows us to very easily scale single connection program to a program with pool of connections, which I find great, as you never know when ye’r gonna need those connections with goroutines.</p><p>In this case I used <a href="https://github.com/jackc/pgx">pgx</a>, a Go implementation of Postgres client. The code is very simple:</p><pre>var connectionInstance *pgx.Conn // this is &quot;internal&quot;, as in: we should NOT use this directly</pre><pre>func connectToDB() *pgx.Conn {<br>  conn, err := pgx.Connect(Config.Database())<br>  if err != nil {<br>    log.Print(err)<br>    log.Print(&quot;Could not connect to Postgres.&quot;)<br>    return &amp;pgx.Conn{}<br>   }</pre><pre>  return conn<br>}</pre><pre>func Database() *pgx.Conn {<br>  if connectionInstance == nil { // during startup - if it does not exist, create it<br>    connectionInstance = connectToDB()<br>  }<br>  connected := connectionInstance.IsAlive()<br>  for connected != true { // reconnect if we lost connection<br>    log.Print(&quot;Connection to Postgres was lost. Waiting for 5s...&quot;)<br>    connectionInstance.Close()<br>    time.Sleep(5 * time.Second)<br>    log.Print(&quot;Reconnecting...&quot;)<br>    connectionInstance = connectToDb()<br>    connected = connectionInstance.IsAlive()<br>   }</pre><pre>  return connectionInstance<br>}</pre><p>Keep in mind that this is a single connection example. In above code we have connectToDB() function that, well, connects to the database. If it succeeds that’s great, if not — it will return an empty &amp;pgx.Conn . <br>Function Database() however does couple of things:</p><ul><li>it checks if we already initialized a connection,</li><li>[if not] it initializes it,</li><li>then, until connection is alive, attempts to reconnect with 5 second intervals,</li><li>as soon as the connection is alive, it returns connection instance.</li></ul><p>This way you can always use it in a way Database().Query(blah) and be sure that nothing will throw an error because of a connection issue; instead it will wait until the connection is established properly.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4b5e5cc09a39" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cyfrowy-humanizm/golang-postgres-auto-reconnect-4b5e5cc09a39">Golang and Postgres auto-reconnect</a> was originally published in <a href="https://medium.com/cyfrowy-humanizm">marahin</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Migration to Medium]]></title>
            <link>https://medium.com/cyfrowy-humanizm/migration-to-medium-444baf1b8d0?source=rss-bb5536ac71d2------2</link>
            <guid isPermaLink="false">https://medium.com/p/444baf1b8d0</guid>
            <category><![CDATA[blogging]]></category>
            <category><![CDATA[it]]></category>
            <category><![CDATA[wordpress]]></category>
            <category><![CDATA[migration]]></category>
            <category><![CDATA[medium]]></category>
            <dc:creator><![CDATA[Jasiek Matusz]]></dc:creator>
            <pubDate>Thu, 15 Jun 2017 15:45:42 GMT</pubDate>
            <atom:updated>2017-06-15T15:45:42.257Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*Yk0evcUMBM-ozsJKaZUSiA.png" /></figure><p>You might not notice it at a first glance, but <strong>my blog is now on Medium</strong>. I was thinking about this for a long time; and finally decided that it’s time.</p><p>There were many disadvantages that I considered. First of all, Medium cannot be self-hosted. There are a few open-source <a href="http://alternativeto.net/software/medium/?platform=self-hosted">clones</a> that do that, but sadly none look <em>that</em> good and none have the same feeling that Medium does.<br>Which is understandable, because, well, <strong>Medium</strong> is getting <em>‘dem munies</em> for what they do. And they do it well.</p><p>For a long time (years, now) my blog was running on a <a href="https://wordpress.org/">WordPress</a> instance. WordPress is great; nowadays it’s used in variety of cases, such as e-commerce, landing pages, blog pages, company pages, vCards and stuff like that. It gives you a possibility to self host an instance, or to use their shared hosting on *.wordpress.com. And, just like at Medium, you can also hook your own custom domain to the instance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MdwF5P-InEMtGi-YuWK0_A.png" /><figcaption>WordPress.org is cool! Really.</figcaption></figure><p>I used WordPress for a personal site — a blog <em>and</em> a vCard. That’s because I needed to have <em>my</em> part of the Internet somewhere; and that space should contain information about me, who I am and what I do, alongside with my opinions, case studies or whatever I wanted to write about.</p><p>The reason I moved to <strong>Medium</strong> might shock you at first. The real cause is that <strong>I did not want to have that much freedom</strong>. Yeah. By <em>freedom</em> I mean possibilities to change whatever I want, tinker with stuff, edit things and improve stuff that I do not like. And holy hell, there was <em>a lot</em> of stuff that I did not enjoy at WordPress. I tried <strong>Ghost</strong>, but neither that made me feel satisfaction at my personal page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ndde-cRUB062QTHan-yB-A.png" /></figure><p>I came to the conclusion that I don’t want<em> </em><strong><em>that kind of freedom </em></strong>when I noticed how regularly I try to write, actually begin, and then at some point stop and edit theme code or tinker with WordPress settings. It usually happened after I wrote a few paragraphs, saved it and checked the draft — it annoyed me how much stuff could look or feel better.</p><p>At that point I understood: having the access to all that stuff, and knowledge that <em>I am the person to fix it</em> is really discouraging. I tried to improve the site (or the theme so I could <em>feel </em>the whole page), but I lacked time and patience to research it and actually do all the things I wanted to.</p><p>So when I read about Medium, tinkered with it a little, I decided that it’s time. It’s time to split my personal vCard and my blog; move the blog to a platform that actually provide small customisation possibilities, but one that feels great out of the box. One, that would allow me to focus on writing, and not on the aesthetics.</p><p>If anyone feels like me, I really encourage you to do the same. It really feels better seeing how it looks and feels good, and not actually caring on the layout. <em>It’s not my fault how it looks, go whine about Medium!</em> (and you can do that on Medium too).</p><p>Remember to follow me if you liked what you’ve read. Peace out, yo!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=444baf1b8d0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cyfrowy-humanizm/migration-to-medium-444baf1b8d0">Migration to Medium</a> was originally published in <a href="https://medium.com/cyfrowy-humanizm">marahin</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Wowapi gem — first World of Warcraft library]]></title>
            <link>https://medium.com/cyfrowy-humanizm/rubystories-3-wowapi-gem-first-world-of-warcraft-library-8047f291beb3?source=rss-bb5536ac71d2------2</link>
            <guid isPermaLink="false">https://medium.com/p/8047f291beb3</guid>
            <category><![CDATA[rubystories]]></category>
            <category><![CDATA[worldofwarcraft]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[games]]></category>
            <dc:creator><![CDATA[Jasiek Matusz]]></dc:creator>
            <pubDate>Fri, 30 Sep 2016 14:18:42 GMT</pubDate>
            <atom:updated>2017-06-17T11:28:43.859Z</atom:updated>
            <content:encoded><![CDATA[<h3>Rubystories #3: Wowapi gem — first World of Warcraft library</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GmE-EQy5nAZjbqs_Qb1nLw.jpeg" /><figcaption>In the picture you can see… that you are not prepared.</figcaption></figure><p>You <strong>know World of Warcraft</strong>. I mean, like, it’s one of those games that has huge marketing, huge story, and is widely known around the globe. Milions of players worldwide, huge roleplaying community, huge YouTube scene. Basicly, <strong>you have to know World of Warcraft</strong>. As a programmer and a player, I have my own guild in the game; <em>The Aspects</em> based at Argent Dawn realm. We have a funky website which has been running for years now, with cool stuff that shows newsfeed &amp; live members roster online. However, our lovely Blizzard has changed the API, and so — after years of programming, this time as a professional, I decided to go on another level with delivering our guild homepage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/332/1*dzHYRTBu-KQ8Eqg4lyC2LA.png" /><figcaption>Ruby is awesome!</figcaption></figure><p>Back in the day having an API key (thus application registration) was <strong>not required. </strong>You basicly sent GET request to a domain with proper parameters, and you would receive some JSON data corresponding to your query.</p><p>The basic flow of Aspects website back in the day looked like this:</p><ul><li>try to refresh news and guild roster (Sidekiq worker ran by Clockwork queueing)</li><li>if data received (“newer”), set it up in redis under some predefined keys</li><li>controller assigns data to variables, properly parses redis data;</li><li>views display variables assigned by controller</li></ul><p>So basicly we had some refreshing -&gt; caching (redis) -&gt; displaying. Very simple, as a Ruby beginner would do.</p><p>Well, Blizzards’ API changed, and it is now called <strong>Blizzard Community API.</strong> It covers API for World of Warcraft, Heroes of the Storm, Heartstone and so on and so on, but I as a World of Warcraft player more than any other game, decided to focus only on this particular product.</p><p>Currently required steps (to gain access to Blizzard’s Community API) is:</p><p>- register at <a href="https://dev.battle.net/">https://dev.battle.net</a><br>- register an application<br>- generate a pair of public/secret keys<br>- read the docs, Harry!</p><p>So what I decided to do is to create a library, so that if ever Blizzard API changes it’ll be easy to maintain and change. Also it would make our Rails application tidier, using variable that holds `Wowapi` class Object, making all the calls only through that particular variable.</p><p>At the time of writing, it currently has some funky stuff in it (including meta programming). But, if you are reading this perhaps after couple months of time, I suggest checking out the repository with up-to-date information on how the library works and how to get it: <a href="https://git.3lab.re/marahin/wowapi">https://git.3lab.re/marahin/wowapi</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8047f291beb3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cyfrowy-humanizm/rubystories-3-wowapi-gem-first-world-of-warcraft-library-8047f291beb3">Wowapi gem — first World of Warcraft library</a> was originally published in <a href="https://medium.com/cyfrowy-humanizm">marahin</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>