Skip to content

Instantly share code, notes, and snippets.

describe Tweet do
describe "basic fetching of tweets" do
describe "(search conditions)" do
attr_reader :new_search
before do
@new_search = mock(Twitter::Search, :null_object => true)
Twitter::Search.should_receive(:new).and_return(new_search)
end
after do
Tweet.fetch_new_tweets
def interpret_time(time)
return nil if time.blank?
return time if time.kind_of?(Time) or time.kind_of?(Date)
named_time(time) || time_by_regex(time) || Time.parse(time)
end
def named_time(time)
case time.downcase
when "today" : Date.today
when "now" : Time.now
# Depends on working pdftk, gm (GraphicsMagick), and pdftotext (Poppler) commands.
# Splits a pdf into batches of N pages, creates their thumbnails and icons,
# as specified in the Job options, gets the text for every page, and merges
# it all back into a tar archive for convenient download.
#
# See <tt>examples/process_pdfs_example.rb</tt> for more information.
class ProcessPdfs < CloudCrowd::Action
# Split up a large pdf into single-page pdfs. Batch them into 'batch_size'
# chunks for processing. The double pdftk shuffle fixes the document xrefs.
@cflipse
cflipse / stubbing_with_arguments_spec.rb
Created December 14, 2011 19:28 — forked from adomokos/stubbing_with_arguments_spec.rb
Stubbing with arguments - examples used in my "(More) Specific Stubbing with RSpec" blog post
class GivesCreditToPreferredCustomers
LOOK_BACK_PERIOD = 3
def self.for_large_orders(sales_amount, added_credit)
# the has_large_purchases scope now takes two arguments
preferred_customers = Customer.has_large_purchases(sales_amount, LOOK_BACK_PERIOD)
preferred_customers.each do |customer|
customer.add_credit added_credit
end
end
end
@cflipse
cflipse / Post.rb
Last active October 1, 2019 15:56
External Validations using only ActiveModel
require 'active_model'
# Most of this is the basic boilerplate described in the docs for active_model/errors; ie, the bare minimum
# a class must have to use AM::Errors
class Post
extend ActiveModel::Naming
attr_reader :errors
attr_accessor :title, :author, :publication_date
require "active_model/lint"
require "test/unit/assertions"
shared_examples_for "ActiveModel" do
include ActiveModel::Lint::Tests
include Test::Unit::Assertions
before { @model = subject }
ActiveModel::Lint::Tests.public_instance_methods.map(&:to_s).grep(/^test/).each do |test|
@cflipse
cflipse / migration.rb
Last active August 29, 2015 14:13
daily random shuffle
class WidgetShuffleMigration
def change
change_table :widgets do |t|
# used to store the result of a rand() function, which satisifies: 0 < rand() < 1
t.float :shuffle_order, index: true
end
end
end
@cflipse
cflipse / dataset_pagination.rb
Last active August 29, 2015 14:15
pagination wrapper
require 'charlatan'
# A simple pagination wrapper that implements the handful of
# methods that Kaminari wants for displaying pagination.
#
# Currently requires implementing a `window` function in the
# relation as well. `limit().offeset()` filters are what make
# up that method.
class DatasetPagination
require 'rom/memory'
class BlenderRelation < ROM::Relation[:memory]
repository :memory
register_as :blender
forward :join
end
class PostWithTagsMapper < ROM::Mapper
#!/usr/bin/env ruby
require 'bundler/inline'
require 'json'
gemfile(:install) do
gem 'rom', '>= 2.0', github: 'rom-rb/rom', branch: 'master'
gem 'rom-http', github: 'rom-http-rb/rom-http', branch: 'master'
gem 'faraday'
end