Skip to content

Instantly share code, notes, and snippets.

@ArturT
Created April 1, 2019 11:23
Show Gist options
  • Save ArturT/c9a5888510839cd60f17f7fa4c1889c1 to your computer and use it in GitHub Desktop.
Save ArturT/c9a5888510839cd60f17f7fa4c1889c1 to your computer and use it in GitHub Desktop.
Ruby service object examples
class OmniAuthorizer
def self.call(params)
new(SlackAPI.new).call(params)
end
def self.build_for_facebook(params)
new(FacebookAPI.new).call(params)
end
def initialize(api)
@api = api
end
def call(params)
api.create_user(params[:email])
end
private
def prepare_user(user)
# TODO
end
end
# we can user service in 2 different contextes like SlackAPI or FacebookAPI
OmniAuthorizer.call(params)
OmniAuthorizer.build_for_facebook(params)
# How to test service (more complex testing)
OmniAuthorizer.new.call(params)
let(:omni_authorizer) { instance_double(OmniAuthorizer) }
expect(OmniAuthorizer).to receive(:new).and_return(omni_authorizer)
except(omni_authorizer).to receive(:call).with(params)
# How to test service (easier testing)
OmniAuthorizer.call(params)
except(OmniAuthorizer).to receive(:call).with(params)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment