Skip to content

Instantly share code, notes, and snippets.

View JanDintel's full-sized avatar

JanDintel

View GitHub Profile
@JanDintel
JanDintel / cron_ping.rb
Last active July 16, 2020 15:18
Perform a HTTP GET request safely in Ruby, to answer https://twitter.com/mattiasgeniar/status/1283725453065367555
require 'net/http'
def ping(endpoint)
uri = URI.parse(endpoint)
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == 'https'
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
end
{"swagger":"2.0","info":{"version":"v0","title":"Test application","description":"Documents the V0 API of the Test application.","contact":{"name":"Jan"},"license":{"name":"MIT"}},"host":"localhost:3000","basePath":"/api/v0","consumes":["application/json"],"produces":["application/json"],"paths":{"/profiles":{"get":{"description":"Finds all Profiles","operationId":"findPets","responses":{"200":{"description":"Profiles response","schema":{"type":"array","items":{"$ref":"#/definitions/Profile"}}}}}},"/profiles/{id}":{"get":{"description":"Finds a Profile","operationId":"findPetById","parameters":[{"name":"id","in":"path","description":"Pseudonym of the Profile","required":true,"type":"string"}],"responses":{"200":{"description":"Profile response","schema":{"$ref":"#/definitions/Profile"}}}}}},"definitions":{"Profile":{"required":["id","type"],"properties":{"id":{"type":"string"},"type":{"type":"string"}}}}}
@JanDintel
JanDintel / 1_before_example.rb
Last active August 29, 2015 14:06
Virtus: Allow a default value when the attribute is assigned as nil, with the :allow_nil option
require 'virtus'
class Page
include Virtus.model
attribute :title, String
attribute :font, String, :default => 'Helvetica'
attribute :header, String, :default => 'Welcome'
attribute :author, String, :default => 'Mies', :allow_nil => false
end
@JanDintel
JanDintel / Preferences.sublime-settings
Last active December 28, 2015 18:49
Sublime 3 User settings
{
"auto_complete_commit_on_tab": false,
"theme": "itg.flat.dark.sublime-theme",
"color_scheme": "Packages/Theme - itg.flat/itg.dark.tmTheme",
"itg_small_tabs": true,
"ensure_newline_at_eof_on_save": true,
"font_size": 12,
"ignored_packages":
[
"Vintage"
@JanDintel
JanDintel / factory.rb
Created November 5, 2013 12:10
Factories with `#initialize_with`
# spec/factories/factory.rb
factory :factory_name, class: Klass do
skip_create
ignore do
variable "Hello"
end
initialize_with do
@JanDintel
JanDintel / .zshrc
Created November 5, 2013 12:06
Configure oh-my-zsh ENV file with Rails aliasses
# ~/.zshrc
alias migratetestdb='rake db:migrate RAILS_ENV=test'
alias droptestdb='rake db:drop RAILS_ENV=test; rake db:create RAILS_ENV=test; rake db:migrate RAILS_ENV=test'
alias resettestdb='rake db:reset RAILS_ENV=test'
@JanDintel
JanDintel / db_cleaner.rb
Last active December 27, 2015 08:59
RSpec, SpecHelper and DatabaseCleaner (with Capybara and Postgres)
# spec/support/db_cleaner.rb
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
@JanDintel
JanDintel / infrastructure_controller.rb
Last active December 23, 2015 06:39
InfrastructureController to support Nagios health check
# app/controllers/infrastructure_controller.rb
class InfrastructureController < ActionController::Base
def health_check
@checks = { database: ActiveRecord::Migrator.current_version,
redis: redis_check,
version: %x(git log --pretty=format:'%h' -1) }
if @checks[:redis] == "Not connected"
@notification = "Error 500, Can't find Redis"
@JanDintel
JanDintel / Git aliases
Last active December 23, 2015 04:59 — forked from JobV/Git aliases
git config --global alias.co checkout
git config --global alias.st status
git config --global alias.br branch
git config --global alias.com commit
git config --global alias.pl pull
git config --global alias.ps push
@JanDintel
JanDintel / gist:6088237
Last active August 27, 2021 21:35
Using RSpec with Rails 4 PATCH method

Using Rspec with Rails 4 patch method

In Rails 4 PATCH is the new HTTP methode for an update action. You can find the details here: http://weblog.rubyonrails.org/2012/2/25/edge-rails-patch-is-the-new-primary-http-method-for-updates/

Using PATCH in your specs

As you can imagine you need to use the PATCH method to test the update action in your controller. Because of how the users controller in this example works you need to specify the id and user. This particular spec tests whether the user gets redirected if it's not logged in.

(./spec/controllers/users_controller_spec.rb)

require 'spec_helper'