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.

5 posts about #ruby

Use hash or case-statement in Ruby?

Often, when we need to get a value based on the other one, we’re using a case-statement. Like this

def realizing_trade_type(realizable_trade_type)
  case realizable_trade_type
  when 'buy'
    'sell'
  when 'short'
    'cover'
  when 'buy_contract'
    'sell_contract'
  when 'short_contract'
    'cover_contract'
  end
end

But, if the conditions and the results are simple values, why don’t we use hash for this? We can :)

REALIZING_TRADE_TYPES = {
  'buy'            => 'sell',
  'short'          => 'cover',
  'buy_contract'   => 'sell_contract',
  'short_contract' => 'cover_contract'
}.freeze

Here is the benchmark of both options, executed 10_000_000 times. It shows that a hash is faster in times for such the kind of usage.

>> require 'benchmark'
true
>> Benchmark.bm(15) do |x|
  x.report('hash') { 10_000_000.times { REALIZING_TRADE_TYPES['buy'] } }
  x.report('case-statement') { 10_000_000.times { realizing_trade_type 'buy' } }
  x.report('empty') { 10_000_000.times {} }
end
                      user     system      total        real
hash              0.990423   0.003412   0.993835 (  1.057612)
case-statement    1.752263   0.004531   1.756794 (  1.762030)
empty             0.380810   0.000728   0.381538 (  0.382153)

So, it’s better to use a hash when you are just retrieving some values (like in the example above). If there is additional logic to execute, a case-statement is still a way to go.

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 💎

How to create zip files on the fly w/o Tempfile

There are many articles about how to archive files from the server and send a zip-file to a client without persisting it on the server. But usually they don’t literally do it, because they use temporary files.

There is a simple way to do it without creating any file though. You just have to put files directly to Zip::OutputStream and then read from it. Btw pay attention: you must rewind the stream before reading it.

# some files objects
def download(files)
  zip_stream = Zip::OutputStream.write_buffer do |zip|
    files.each.with_index(1) do |file, index|
     # file name must be uniq in archive
      zip.put_next_entry("#{file.name}--#{index}.#{file.extension}")
      zip.write(file.read.force_encoding('utf-8'))
    end
  end
  # important - rewind the steam
  zip_stream.rewind
  send_data zip_stream.read, 
            type: 'application/zip', 
            disposition: 'attachment', 
            filename: 'files-archive.zip'
end