Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@SabretWoW
SabretWoW / rspec_model_testing_template.rb
Last active March 7, 2024 03:56
Rails Rspec model testing skeleton & cheat sheet using rspec-rails, shoulda-matchers, shoulda-callbacks, and factory_girl_rails. Pretty much a brain dump of examples of what you can (should?) test in a model. Pick & choose what you like, and please let me know if there are any errors or new/changed features out there. Reddit comment thread: http…
# This is a skeleton for testing models including examples of validations, callbacks,
# scopes, instance & class methods, associations, and more.
# Pick and choose what you want, as all models don't NEED to be tested at this depth.
#
# I'm always eager to hear new tips & suggestions as I'm still new to testing,
# so if you have any, please share!
#
# @kyletcarlson
#
# This skeleton also assumes you're using the following gems:
@SabretWoW
SabretWoW / zappos_product_scraper.rb
Last active July 15, 2018 04:29
Small Ruby script that demonstrates how to use Mechanize to scrape some product details from an array of product URLs from Zappos.com
# http://nokogiri.org/Nokogiri/XML/Node.html#method-i-css
require 'mechanize'
require 'csv'
puts "Product Scraper!!!"
puts ' '
urls = [
"http://www.zappos.com/seavees-teva-universal-sandal-concrete",
@SabretWoW
SabretWoW / tweets_controller.rb
Last active December 21, 2015 14:58
A Ruby on Rails controller action showing how to fetch a group of Tweets/followers/friends & send them to a view. You can also create Tweets with this controller & the Twitter API, but you'll need to ensure your callback URL is correct. Follow the documentation for that.
class TweetsController < ApplicationController
before_action :create_client
def index
batch_size = 10
@twitter_handle = "dhh"
@tweets = @client.user_timeline(@twitter_handle).take(batch_size)
@friends = @client.friends(@twitter_handle).take(batch_size)
@SabretWoW
SabretWoW / ruby-open-uri-request.rb
Created December 9, 2013 17:16
4-line Ruby script that uses open-uri to fetch the contents of a URL & displays it in the console. This is the foundation for all web requests, whether to scrape a page, request a JSON response, and more.
# http://ruby-doc.org/stdlib-2.0.0/libdoc/open-uri/rdoc/OpenURI.html
require 'open-uri'
# Go fetch the contents of a URL & store them as a String
response = open('http://www.example.com').read
# "Pretty prints" the result to look like a web page instead of one long string of HTML
URI.parse(response).class
# Print the contents of the website to the console
@SabretWoW
SabretWoW / ruby_web_scraping.rb
Created December 9, 2013 22:44
A Ruby web scraping script that visits a GitHub trending repos page, scrapes the data for the 25 repos, loads them into a CSV, and then reads from the CSV & creates a hash with each repo's data.
require 'mechanize'
require 'csv'
# Load up the trending Ruby repos on GitHub from the last month.
url_to_scrape = "https://github.com/trending?l=ruby&since=monthly"
# Snag the website with Mechanize & parse it into an XML document we can query.
page = Mechanize.new.get(url_to_scrape)
# Set the name of the CSV we'll create & load from.
file = "repo_data.csv"
@SabretWoW
SabretWoW / json_response_handling_ruby.rb
Created December 11, 2013 14:20
Ruby script that uses open-uri to fetch the contents of a JSON endpoint, uses the JSON gem to parse the string into a Ruby array & prints some of the records. This is the foundation for all web API requests, so feel free to use it in the future.
# http://ruby-doc.org/stdlib-2.0.0/libdoc/open-uri/rdoc/OpenURI.html
require 'open-uri'
# https://github.com/flori/json
require 'json'
# http://stackoverflow.com/questions/9008847/what-is-difference-between-p-and-pp
require 'pp'
# Construct the URL we'll be calling
request_uri = 'http://localhost:3000/users.json'
request_query = ''
@SabretWoW
SabretWoW / ruby_github_gist_api.rb
Created December 11, 2013 23:20
A Ruby script demonstrating how to pull Gists from the GitHub Gist API.
# Gem that wraps around the Gist API
# https://github.com/sinisterchipmunk/active-gist
require 'activegist'
# GitHub Gist API documentation:
# - http://developer.github.com/v3/gists/
# Set up credentials.
ActiveGist::API.username = "Your GitHub username"
ActiveGist::API.password = "Your GitHub password"
@SabretWoW
SabretWoW / ruby_benchmark_examples.rb
Created December 14, 2013 02:59
Three examples showing three of the main methods of Ruby's Benchmark class that's used to profile your code. Benchmark.measure at the most basic, Benchmark.benchmark allows you to run trials of different code blocks, and Benchmark.bmbm that helps you establish a baseline for your tests.
# http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html
require 'benchmark'
# http://www.ruby-doc.org/stdlib-2.0.0/libdoc/bigdecimal/rdoc/BigMath.html
require 'bigdecimal/math'
# Set the number of iterations to run. The underscore here is used as a substitute for normal comma so Ruby interprets the number correctly.
iterations = 10
puts "\nCalculating pi #{iterations} times.\n\n"
@SabretWoW
SabretWoW / VimMonokai.vim
Created December 14, 2013 06:21
My Vim color scheme based on Sublime Text's default scheme: Monokai.
" Vim color file
" Converted from Textmate theme Monokai Refined using Coloration v0.3.2 (http://github.com/sickill/coloration)
set background=dark
" highlight clear
highlight Normal guibg=black guifg=white
if exists("syntax_on")
syntax reset
endif
@SabretWoW
SabretWoW / 42-things.md
Last active May 13, 2022 07:32 — forked from xdite/42-things.md
42 Sweet, sweet things Rails (and Ruby) can do. Wonderful reference.