Home

References

Streaming

send_data pdfdata, filename: "foo.pdf", type: "application/pdf"
send_file Rails.root.join('public','filename.txt') [filename: '..', type: '..']

response

response.body
response.status         #=> 404
response.location       # Redirect location
response.content_type
response.charset
response.headers

response.headers["Content-Type"] = "application/pdf"

Request/response

request.host            #=> "www.example.com"
request.domain          #=> "www.example.com"
request.domain(n=2)     #=> "example.com"
request.port            #=> 80
request.protocol        #=> "http://"
request.query_string    #=> "q=duck+tales"
request.url             #=> "http://www.example.com/search?q=duck+tales"
request.fullpath        #=> "/search?q=duck+tales"

request.headers         # Returns a hash

request.format          #=> "text/html"
request.remote_ip       #=> "203.167.220.220"
request.local?          #=> true (if localhost/127.0.0.1)

request.xhr?

request.method          #=> "POST"
request.method_symbol   #=> :post
request.get?
request.post?
request.put?
request.delete?
request.head?

HTTP basic authentication

before_filter :authenticate

# Basic authentication:
def authenticate
  authenticate_or_request_with_http_basic { |u, p|
    u == "root" && p == "alpine"
  }
end

# ...or digest (hashed) authentication:
# uses the ha1 hash (username:realm:password)
def authenticate_by_digest
  realm = "Secret3000"
  users = {
    "rsc" => Digest::MD5.hexdigest("rsc:#{realm}:passwordhere")
  }

  authenticate_or_request_with_http_digest(realm) { |user|
    users[user]
  }
end

# For integration tests
def test_access
  auth = ActionController::HttpAuthentication::Basic.encode_credentials(user, pass)
  get "/notes/1.xml", nil, 'HTTP_AUTHORIZATION' => auth
end

# Token auth
is_logged_in = authenticate_with_http_token do |token, options|
  token == our_secret_token
end

request_http_token_authentication  unless is_logged_in

Filters

# Filter with callbacks
before_filter :authenticate
before_filter :authenticate, except: [:login]
before_filter :authenticate, only: [:login]
def authenticate
  redirect_to login_url unless controller.logged_in?
end

# Filter with inline
before_filter do |controller|
  redirect_to login_url unless controller.logged_in?
end

# Filter with external classes
before_filter LoginFilter
class LoginFilter
  def self.filter(controller) ...; end
end

# Filter exceptions
skip_before_filter :require_login, only: [:new, :create]

# Before/after filters
around_filter :wrap_in_transaction
def wrap_in_transaction(&blk)
  ActiveRecord::Base.transaction { yield }
end

default_url_options

# The options parameter is the hash passed in to 'url_for'
def default_url_options(options)
  {:locale => I18n.locale}
end

respond_to

respond_to do |format|
  format.html
  format.xml  { render xml: @users }
  format.json { render json: @users }
  format.js    # Will be executed by the browser
end

Special hashes

session[:user_id] = nil

flash[:notice] = "Hello"    # Gets flushed on next request
flash.keep                  # Persist flash values
flash.now[:error] = "Boo"   # Available on the same request

cookies[:hello] = "Hi"

params[:page]

# params is a combination of:
query_parameters
path_parameters
request_parameters

Common stuff

redirect_to root_url
redirect_to root_url, notice: "Good."
More

Dplyr

dplyr cheat sheet is a quick reference for dplyr that is a grammar of data manipulation, providing a consistent set of verbs that help you solve the most common data manipulation challenges.

Regex

A regular expression is a sequence of characters that specifies a search pattern.

PySpark

PySpark is an interface for Apache Spark in Python. It not only allows you to write Spark applications using Python APIs

Golang

Go is a statically typed, compiled programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson.

Watchman

Watchman exists to watch files and record when they change. It can also trigger actions (such as rebuilding assets) when matching files change.

Rollup.Js

Rollup Rollup is a module bundler for JavaScript which compiles small pieces of code into something larger and more complex, such as a library or application.

Bluebird.Js

Bluebird is a fully-featured Promise library for JavaScript. The strongest feature of Bluebird is that it allows you to "promisify" other Node modules in order to use them asynchronously. Promisify is a concept applied to callback functions.

Flow

Flow is a static type checker for your JavaScript code. It does a lot of work to make you more productive. Making you code faster, smarter, more confidently, and to a bigger scale.

Node.Js Api

Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.

C Preprocessor

Quick reference for the C macro preprocessor, which can be used independent of C/C++.

Sass

Sass is a preprocessor scripting language that is interpreted or compiled into Cascading Style Sheets. SassScript is the scripting language itself. Sass consists of two syntaxes.

Activeadmin

Active Admin is a Ruby on Rails plugin for generating administration style interfaces. It abstracts common business application patterns to make it simple for developers to implement beautiful and elegant interfaces with very little effort.

Fetch

The Fetch standard defines requests, responses, and the process that binds them: fetching.

Httpie

$ http POST http://example.com name="John" Host:example.com — JSON, cookies, files, auth, and other httpie examples.

Rspec

RSpec is a computer domain-specific language testing tool written in the programming language Ruby to test Ruby code. It is a behavior-driven development framework which is extensively used in production applications.

Saucelabs

Sauce Labs allows users to run tests in the cloud on more than 700 different browser platforms, operating systems, and device combinations.

Jasmine

Jasmine is a behavior-driven development framework for testing JavaScript code.

Sequelize

Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite and Microsoft SQL Server.

Ubuntu

Ubuntu is a Linux distribution based on Debian and composed mostly of free and open-source software. Ubuntu is officially released in three editions: Desktop, Server, and Core for Internet of things devices and robots.

Rails Models

Ruby on Rails, or Rails, is a server-side web application framework written in Ruby under the MIT License. Rail is a model–view–controller framework, providing default structures for a database, a web service, and web pages.

Git Log

Git is software for tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development.

Controllers

Ruby on Rails, or Rails, is a server-side web application framework written in Ruby under the MIT License. Rails is a model–view–controller framework, providing default structures for a database, a web service, and web pages.

Ansible Examples

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code.

Bolt Quickstart

A quick guide to getting started writing Bolt tasks

Rspec-Rails

RSpec Rails defines ten different types of specs for testing different parts of a typical Rails application.

Rsync

rsync is a utility for efficiently transferring and synchronizing files between a computer and an external hard drive and across networked computers by comparing the modification times and sizes of files.

Ledger Cli

Ledger is a command-line based double-entry bookkeeping application. Accounting data is stored in a plain text file, using a simple format, which the users prepare themselves using other tools.

Homebrew

Homebrew is a free and open-source software package management system that simplifies the installation of software on Apple's operating system macOS as well as Linux.

Bundler

Bundler provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that are needed.