Vincent is Coding
Coding is life, and so is sharing. A collection of things I found useful, or some behind the scenes code, from my own projects. Mainly Ruby on Rails and React Native. Totally informal and not too technical. Let's go.
Subscribe via RSS here

Sanitising content before render

I wanted a simple way to sanitise some of the content before it was rendered in Ruby on Rails, when viewing a post with an image in Scribbles. The problem was that any type of attachment in Rails always brings through the following markup on the front-end: <action-text-attachment sgid="123" some_other_data></action-text-attachment>The problem with that is that it exposed the original file that I'm storing — potentially exposing location data if it's an image with GPS meta data. Right now I found no good way to strip just the GPS the meta data from an attachment without also destroying the colour space,...

Easily check webhook signatures

One thing I had to do with all my projects that accept webhook events from my payment provider is to make sure that the incoming request is indeed legitimate. That means, ahead of time, I know the signing secret from the payment provider and then can compare the payload to their signature. If it matches I accept the webhook event, and if it fails... erm, yeah, bye bye. So, in my Rails application I need to check the request headers for any extra data for example `X-Signature` and also the request body. Then I compare the two and see if...

Avoiding N+1 queries on a model with memoization

During the Scribbles request cycle, there are multiple checks to see if a domain exists on a given blog, and if so, tweak the URL of certain links and also posts. The problem I had is that when I render out a post, in a list, it would check if the blog had a valid domain to it. And because I am silly, I didn't think much about it until I've seen the database query where it would just do this check many times — obviously. Here was my initial take on checking if I a blog `has_valid_hostname?`: def has_valid_hostname?...

Adding multiple optional query parameters

One thing I wanted to achieve in Scribbles, and the Tinylytics integration is to dynamically create various query parameter options available with Tinylytics with ease. Right now Scribbles only allows a few options to be set, but I wanted to be able to set other parameters as and when I get around adding them to the page. When you have Tinylytics enabled on your site, the app will check for this and add the following snippet to the head of your site: <% if @blog.tinylytics_site_id.present? %> <script src="https://tinylytics.app/embed/<%= @blog.tinylytics_site_id %>.js<%= @blog.tinylytics_options_query_string(current_user) %>" defer></script> <% end %>`@blog` is always available to...

Unicode Normalise a String in Rails

One strange issue I had on Scribbles, especially with creating a page/post url based on the title, was that sometimes someone would use... wait for it... full-width characters. Huh? What? Yes, I never encountered this... EVER. Here is a full-width page title: About Now that actually is just "About" but as "full-width" characters, and that poses a problem. When Scribbles saves a post or page, it'll go ahead and try and `parameterize` the page/post title. Here is a an example of my method: if self.title.present? url = self.title.parameterize else url = "#{self.published_date.strftime("%Y-%m-%d")}" endThe problem with full-width characters is that they're...

Normalise a value in Rails 7.1

One things I noticed whilst developing Scribbles is that I needed a quick way to just `normalize` some properties as a blog was saved/updated. Scribbles allows you to set up Tinylytics using the special site embed id and also various other 3rd party services like status.lol or shoutouts. One thing I noticed is that sometimes things broke because a space was at the start or end of the pasted value. So you can imagine if you're trying to append that value to a URL, it wouldn't work and not do anything useful. Thankfully Rails 7.1 has a new `normalizes` hook,...

Made by me.

My personal website: vincentritter.com

✌️❤️