for class Jikan # Prints the time in a way that is not awful. # # Example: # >> Jikan.time # => The time is 6:14 PM # The date is February 7, 2013 def self.time puts "The time is #{Time.now.strftime("%I:%M %p")}." puts "The date is #{Time.now.strftime("%B %d, %Y")}." end end
lib/faxly/client.rb module Faxly class Client def initialize(key:, secret:) # get a token for making requests end def send_fax(to:, body:) # make the HTTP request to send the fax end end end
should make use of action classes in lib to make the job simpler. class BackupFaxJob < ActiveJob::Base def perform(fax) pdf = FaxBackup::CreatesPDF.new(fax).create FaxBackup::SyncsToDropCube.new(pdf).sync end end
#perform is about expecting a class to create a PDF and another class to sync it. It is up to the tests of each of those classes to determine how that is handled.
of SCFM is that controllers in Rails apps can become very complex at times. SCFM is about removing the complexities of a controller and delegating them to a model.
class Completer def initialize(ticket) @ticket = ticket @project = ticket.project end def complete! return false if ticket.complete? ticket.complete! update_project_status end end end
lib require 'secure_hash_generator' require 'fax_formatter' class Fax < ActiveRecord::Base def format FaxFormatter.new( SecureHashGenerator.new.generate ) end end
lib require 'secure_hash_generator' require 'fax_formatter' class Fax < ActiveRecord::Base def format FaxFormatter.new( SecureHashGenerator.new.generate ) end end
lib require 'rails_helper' describe Faxly::Client do subject { described_class.new(creds) } describe '#send_fax' do it 'makes an HTTP request' do expect(HTTPal).to receive(:make_request).with(some_json_blob) subject.send_fax( to: '1-800-123-4567', body: 'Serious business info' ) end end end
lib This means faster tests for that file. But how much faster? Let's see with: $ time bundle exec rspec spec/path/to/spec.rb * The app has 28 required gems in the Gemfile.
lib With rails_helper: ..... Finished in 0.07432 seconds (files took 5.6 seconds to load) 5 examples, 0 failures real 0m6.598s user 0m2.962s sys 0m0.597s
lib With spec_helper: ..... Finished in 0.04742 seconds (files took 0.29089 seconds to load) 5 examples, 0 failures real 0m0.964s user 0m0.830s sys 0m0.127s
lib With rails_helper with Spring: ..... Finished in 0.05533 seconds (files took 3.17 seconds to load) 5 examples, 0 failures real 0m3.875s user 0m2.720s sys 0m0.558s