Today I Learned

This project exists to catalogue the sharing & accumulation of knowledge as it happens day-to-day. Posts have a 200-word limit, and posting is open to any Rocketeer as well as selected friends of JetRockets. We hope you enjoy learning along with us.

3 posts by Andrey Morozov @andr_morozov

Use gem Recorder for db log πŸ’ͺ

Gem Recorder tracks changes of your Rails models

For start use this library in Rails 5.2 you need add gem to the Gemfile:

gem 'recorder', github: 'jetrockets/recorder', branch: 'rails_5_2'

Next:

> bundle install
> rails g recorder:install
> rails db:migrate

And final step β€” add to model:

include ::Recorder::Observer
recorder async: false

And it’s all πŸŽ‰

For an example - logs can be issued in the representer:

Recorder::Revision.where(item: object).order(id: :DESC).limit(10).map do |revision|
  {
    revision_id: revision.id,
    userId: revision.user_id,
    userEmail: revision.user&.email,
    change: changes_type(revision)
   }
end 

def changes_type(revision)
  if revision.data['changes'].include?('drive_file_id') && revision.data['changes'].size > 4 && revision.data['changes']['drive_file_id'].present?
    'Object updated & Document generated'
  elsif revision.data['changes'].include?('drive_file_id') && revision.data['changes'].size == 4
    'Document generated'
  elsif revision.data['changes'].include?('number') && revision.data['changes']['number'].nil?
    'Object created'
  else
    'Object updated'
  end
end

But of course this is an abstract example with my data 😏


https://github.com/jetrockets/recorder

Command for create zip archive without gem's πŸ“

class CreateZipCommand
  def call(files)
    # Create temp directory for files 
    tmp_dir = Dir.mktmpdir
    tmp_zip_path = File.join(tmp_dir, "files.zip")
    
    # Move files to the temporary folder you created above
    files.map do |file|
      download_file(file, tmp_dir)
    end
    
    # Go to the folder and archive the entire contents
    `cd #{tmp_dir} && zip #{tmp_zip_path} ./*`
    
    # Return zip path
    tmp_zip_path
  end
end

> CreateZipCommand.new.call(files)
=> "/var/folders/bk/0c864z710654sx555jpdpx9c0000gn/T/d20190126-7447-d27fpl/files.zip")

Most gems for working with archives eat a lot of memory when working with large files. This solution does not have these problems.

Make sure that the zip utility is installed on your computer - it don’t work without it

Webhook integration in development with Ngrok πŸš€

All begin when I’m using the Pipedrive (is a sales management tool designed to help small sales teams manage intricate or lengthy sales processes) webhook.

The solution was easy I just create an ngrok tunnel

> ngrok http 3000 

Session Status                online
Account                       Andrey (Plan: Free)
Version                       2.2.8
Region                        United States (us)
Web Interface                 http://127.0.0.1:4040
Forwarding                    http://b1256cb6.ngrok.io -> localhost:3000
Forwarding                    https://b1256cb6.ngrok.io -> localhost:3000

and then sent the webhook to the generated address and it works. In local development I can user webhook from another api! It’s work πŸš€

To use it you need to do 4 steps:

  1. Register on ngrok
  2. Download ngrok from site
  3. Connect your account
  4. Run it πŸš€

ps

In addition, this service has a ngrok-tunel gem that allows you to fully integrate it with your application. But that’s another story πŸ’Ž