Youtube

Youtube video in popup modal

It is simple iframe similar to this solution just with stop video after modal is closed with Esc

<div class="modal fade" id="videoModal" tabindex="-1" role="dialog" aria-labelledby="videoModal" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-body">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <div>
          <iframe width="100%" height="350" src=""></iframe>
        </div>
      </div>
    </div>
  </div>
</div>

 <a href="#" class="btn btn-default" data-toggle="modal" data-target="#videoModal" data-theVideo="http://www.youtube.com/embed/loFtozxZG0s" >VIDEO</a>

<script>
$('[data-theVideo]').click(function () {
  var theModal = $(this).data( "target" ),
  videoSRC = $(this).attr( "data-theVideo" ), 
  videoSRCauto = videoSRC+"?autoplay=1" ;
  $(theModal+' iframe').attr('src', videoSRCauto);
  $(theModal+' button.close').click(function () {
      $(theModal+' iframe').attr('src', videoSRC);
  });
});

$('#videoModal').on('hidden.bs.modal', function() {
  $(this).find('iframe').attr('src', null);
});
</script>

Google pdf preview

If you want to preview pdf or image file, you can use Google

$('#viewerBox').attr('src','https://docs.google.com/viewer?embedded=true&url=<%= application.resume.media.url %>' );

or you can use directly pdf.js

yt gem access youtube api. Creators http://fullscreen.github.io/yt/. Private or non existing video url raise exceptions so you need to rescue Yt::Errors::NoItems, Yt::Errors::RequestError. Video could be public but not accessible (because of deactived account).

Gmail API

https://pramodbshinde.wordpress.com/2014/12/07/gmail-api-in-ruby-on-rails-a-piece-of-cake/ http://stackoverflow.com/questions/26005675/sending-html-email-using-gmail-api-in-ruby https://developers.google.com/gmail/api/guides/sending https://developers.google.com/api-client-library/ruby/apis/gmail/v1

Someone can break to your account simple by adding his email as recovery email. Than he can easily change password.

Gmail shortcuts

  • ? to open keyboard shortcut help
  • Ctrl+Enter to send email
  • j and k to older and newer conversation
  • o to open conversation, u to back to threadlist
  • g+a go to all mail, g+t to sent emails, g+i to inbox
  • c to compose
  • / to search

ReCaptcha

Register on https://www.google.com/recaptcha and you can use in in javascript <div class="g-recaptcha" data-sitekey="6LdrgFQUAAAAALvyQvT3fpoagmnb-ik9f73Y0Zaz"></div> but on rails and devise follow https://github.com/plataformatec/devise/wiki/How-To:-Use-Recaptcha-with-Devise and gem https://github.com/ambethia/recaptcha

Example is https://github.com/duleorlovic/premesti.se

# Gemfile
# captcha on contact form
gem 'recaptcha'

# config/locales/en.yml
  recaptcha:
    errors:
      verification_failed: reCAPTCHA verification failed, please try again.

# app/form_objects/contact_form.rb
class ContactForm
  include ActiveModel::Model
  include Recaptcha::Adapters::ControllerMethods

  attr_accessor :email, :text, :g_recaptcha_response, :current_user, :remote_ip
  validates_format_of :email, with: Devise.email_regexp
  validates :text, :email, presence: true

  def save
    verify_recaptcha model: self, response: g_recaptcha_response
    return false if errors.present?
    return false unless valid?

    _send_notification
    true
  end

  def request
    OpenStruct.new remote_ip: remote_ip
  end

  def env
    nil
  end

  def _send_notification
    Notify.message("contact_form #{email} @ #{Time.zone.now}", email, text, remote_ip, current_user)
  end
end

# app/controllers/pages_controller.rb
  def contact
    @contact_form = ContactForm.new(
      email: current_user&.email
    )
  end

  def submit_contact
    @contact_form = ContactForm.new(
      email: current_user&.email || params[:contact_form][:email],
      text: params[:contact_form][:text],
      g_recaptcha_response: params['g-recaptcha-response'],
      current_user: current_user,
      remote_ip: request.remote_ip,
    )
    if @contact_form.save
      flash.now[:notice] = t('contact_thanks')
      contact
    else
      flash.now[:alert] = @contact_form.errors.full_messages.join(', ')
    end
    render :contact
  end

# app/assets/javascripts/window_functions.coffee
window.enableRecaptchaButtons = (e) ->
  console.log 'enableRecaptchaButtons'
  $('[data-recaptcha-button]').prop('disabled', false)

# config/initializers/recaptcha.rb
Recaptcha.configure do |config|
  config.site_key = Rails.application.secrets.google_recaptcha_site_key
  config.secret_key = Rails.application.secrets.google_recaptcha_secret_key
end

# config/secrets.yml
  # Google recaptcha https://www.google.com/u/0/recaptcha/admin/
  google_recaptcha_site_key: <%= ENV['GOOGLE_RECAPTCHA_SITE_KEY'] %>
  google_recaptcha_secret_key: <%= ENV['GOOGLE_RECAPTCHA_SECRET_KEY'] %>

Use Checkbox v2 (Invisible is similar, just you need to trigger using js and use callback to receive results). V3 is for scoring.

It is not possible to edit configuration using api https://stackoverflow.com/questions/38197959/how-to-add-redirect-uris-programmatically so use selenium script to update redirect URIs. You can use 50 domain with one key.

I had an error when using STMP

  555 5.5.2 Syntax error. u25sm6671019wml.4 - gsmtp
  /home/orlovic/.rbenv/versions/2.6.3/lib/ruby/2.6.0/net/smtp.rb:969:in `check_response'

and the issue was because email was not properly formated (it used latin letters) for example orlović@email.com

GCS Google Cloud Storage

You need to create Service account keys for communication between server and google API keyfile.json. Private key id and private key are secret properties so you need to put them in credentials

# rails credentials:edit
gcs:
  private_key_id: 23e0,,,
  private_key: -----BEGIN PRIVATE KEY-----\nMIIEvgIBADA,,,

Other values can be in code but copy from your json file

# config/storage.yml
google:
  service: GCS
  project: move-index
  bucket: move-index
  credentials:
    type: service_account
    project_id: move-index
    private_key_id: <%= Rails.application.credentials.dig(:gcs, :private_key_id) %>
    private_key: <%= Rails.application.credentials.dig(:gcs, :private_key) %>
    client_email: [email protected],
    client_id: 123...,
    auth_uri: https://accounts.google.com/o/oauth2/auth,
    token_uri: https://oauth2.googleapis.com/token,
    auth_provider_x509_cert_url: https://www.googleapis.com/oauth2/v1/certs,
    client_x509_cert_url: https://www.googleapis.com/robot/v1/metadata/x509/starting-account-y150mz5jlqxf%40....iam.gserviceaccount.com

Credentials are explained here https://developers.google.com/accounts/docs/application-default-credentials so on development it is enough to export GOOGLE_APPLICATION_CREDENTIALS with the key or the json file

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"

or you can pass file path like

key_file   = "path/to/service-account.json"
storage = Google::Cloud::Storage.new project: project_id, keyfile: key_file

or in ruby, you can find example for each API https://github.com/googleapis/google-cloud-ruby/blob/master/google-cloud-translate/AUTHENTICATION.md

require "google/cloud/translate"

Google::Cloud::Translate.configure do |config|
  config.credentials = "path/to/keyfile.json"

  # or content
  config.credentials = Rails.application.credentials.google_cloud_credentials
end
# or for V2
ENV["TRANSLATE_CREDENTIALS"] = Rails.application.credentials.google_cloud_credentials.to_json

Google Translate API

https://console.cloud.google.com/apis/library/translate.googleapis.com https://github.com/googleapis/google-cloud-ruby/tree/master/google-cloud-translate https://googleapis.dev/ruby/google-cloud-translate-v3/latest/index.html

require "google/cloud/translate/v3"

client = ::Google::Cloud::Translate::V3::TranslationService::Client.new
request = my_create_request
response = client.translate_text request

(do not know how to create request https://googleapis.dev/ruby/google-cloud-translate-v3/latest/Google/Cloud/Translate/V3/TranslateTextRequest.html

Google::Cloud::Translate::V3::TranslateTextRequest.new contents: ['home'], source_language_code: 'en', target_language_code: 'sr'

)

so I still use v2 https://googleapis.dev/ruby/google-cloud-translate-v2/latest/index.html

require "google/cloud/translate/v2"

client = Google::Cloud::Translate::V2.new

translation = client.translate "Hello world!", to: "la"