lundi 30 novembre 2020

Could not run the `identify` command. Please install ImageMagick

I am using Paperclip and getting this error on the deployed site, not on my local server. And that's why it is becoming more difficult for me to get out of this.

A Paperclip::Errors::CommandNotFoundError occurred in images#update:
enter code hereCould not run the `identify` command. Please install ImageMagick.

I have tried these solutions but was not able to fix this (For ubuntu):

  1. Ran apt-get install imagemagick
  2. I don't use brew. So, won't be able to use brew install imagemagick
  3. Added and deployed these lines to paperclip_options.yml

:image_magick_path: '/opt/ImageMagick/bin' :command_path: '/opt/ImageMagick/bin'

Can anyone suggest how to resolve this error?

vendredi 27 novembre 2020

View action should show same response which I get on form submission

I have registration form , once I have submit that form , I was getting some response. I want to show same response on view action. Currently, I have registration page which have two tabs, one is for registration form and other is for post register activity form.where second tab is disabled till I have submitted the registration form. I am able to submit a form and getting response as well, now I want show same response on view action, How should I achieve that in rails web project ? How should my routes work in that case ?

Match request URL params with rails routes via recognize_path

I want to check if any incoming request actually exists in routes.rb as an entry via recognize_path like this

def path_exists?(path)
  Rails.application.routes.recognize_path(path)
  true
  rescue
   false
end

This works for most URLs but if the URL has a parameter, then it fails to recognize it.

Example:

In routes.rb, say I have an entry like this

put "foo/bar/:param" => "foo#bar"

and the incoming request URL is of the form /foo/bar/5

Then the function recognize_path doesn't recognize the URL as the value of the parameter is 5 but it is matched with :param

How do I match requests that have parameters to their entries in routes.rb using recognize_path or any other similar function?

Reason

I'm filtering out malicious attempts to hit the server using random URLs like /dump.sql.tar.gz, /conf.xz, etc using the gem rack::attack and throttling requests whose URLs are not a part of routes.rb. Hence the above problem.

Ruby on Rails - Criteria - Mongoid - where condition with 2 by 2 columns

I'm working with RoR + MongoDB (Mongoid) and I need to apply a filter using or condition by 2 columns.

I saw some recommendations like this one but didn't work.

query = query.where(name: /#{attributes[:name]}/i).or(query.where(email: /#{attributes[:email]}/i))

If I use this one, looks like the or will be applied for all conditions:

query = query.where(name: /#{attributes[:name]}/i).or(email: /#{attributes[:email]}/i)

Anyone has any suggestion?

jeudi 26 novembre 2020

Get this "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" combination for ruby

I have this java code for encryption and decryption, which I want to change/convert to Ruby code. I looked up in OpenSSL gem but dint find the "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" combination available for ruby. How do I implement it?

public class EncryptDecryptService {
    
    public String encryptRequestObject(RequestObject requestObject) throws UnsupportedEncodingException, FileNotFoundException, CertificateException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
        PublicKey publicKey = getPublicKey(requestObject.getKeyFilename());
        byte[] message = requestObject.getString().getBytes("UTF-8");
        byte[] secret = encrypt(publicKey, message);
        return Base64.encodeBase64String(secret);
    }
    
    public String decryptRequestObject(RequestObject requestObject) throws UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
        PrivateKey privateKey = getPrivateKey(requestObject.getKeyFilename(), requestObject.getKeyPassword());
        byte[] cipherText = Base64.decodeBase64(requestObject.getString());
        byte[] decrypted = decrypt(privateKey, cipherText);
        return new String(decrypted, "UTF-8");
    }
    
    private PublicKey getPublicKey(String filename) throws FileNotFoundException, CertificateException {
        FileInputStream fin = new FileInputStream(filename);
        CertificateFactory factory = CertificateFactory.getInstance("X.509");
        X509Certificate certificate = (X509Certificate) factory.generateCertificate(fin);
        PublicKey publicKey = certificate.getPublicKey();
        return publicKey;
    }
    
    private PrivateKey getPrivateKey(String filename, String password) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException {
        FileInputStream fin = new FileInputStream(filename);
        KeyStore ks = KeyStore.getInstance("pkcs12");
        ks.load(fin, password.toCharArray());
        String str = ks.aliases().nextElement();
        PrivateKey privateKey = (PrivateKey) ks.getKey(str, password.toCharArray());
        return privateKey;
    }
    
    private byte[] encrypt(PublicKey key, byte[] plainText) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(plainText);
    }
    
    private byte[] decrypt(PrivateKey key, byte[] cipherText) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
        cipher.init(Cipher.DECRYPT_MODE, key);
        return cipher.doFinal(cipherText);
    }
}

mercredi 25 novembre 2020

Find records have count of association > n

class PurchaseOrder < ApplicationRecord
  has_many :purchase_order_materials 
end 
class PurchaseOrderMaterial < ApplicationRecord
  belongs_to :purchase_order
  belongs_to :material
end

I have this query:

PurchaseOrderMaterial
  .includes(:purchase_order, :material)
  .where("purchase_orders.provider_id IS NULL AND purchase_orders.status = ? 
          AND (purchase_orders.is_waiting != ? 
               OR purchase_orders.is_waiting IS NULL)", 
         PurchaseOrder.possibleStatus.first, true)

All i want it's filter results to purchase_order.purchase_order_materials.count > 1. Some way to achieve it?

mardi 24 novembre 2020

Weird time.beginning_of_month rails 3 behavior

I am working on this query:

Student.where(created_at: time.beginning_of_month..time). Let's say the time here is: `15/10/2020 00:00:00`

I thought this is supposed to return the created students from 1/10/2020 00:00:00 to 15/10/2020 00:00:00 because time.beginning_of_month returns 1/10/2020 00:00:00

However, it returns me the created students from 30/09/2020 22:00:00 to 15/10/2020 00:00:00

I have checked with other months and I get the same results. Somehow, it always returns me the records starting from the last day of the previous month, not the first day of the current month

I am using Rails 3.2.2 and Ruby 2.1.5