Code Monkey home page Code Monkey logo

interactor-example's Introduction

Interactors for better services

How the interactor gem can help solve our service object woes.

Interactor is a drop-in module that abstracts away much of the service object boilerplate. Just like with our current service pattern in Beacons, interactors define a #call method that provides the single entry point to the service. However, unlike our current services, interactors also provide a standardized approach to handling errors, responses, and composition.

Here's a rundown of the major features Interactor provides:

  • Go-style error messaging between internal services.
  • Compose services and call them in order with Organizers.
  • Share state between successive service calls via #context.
  • Hooks API to perform computations during service lifecycle.
  • Easy and consistent testing.

Examples

View the example code in /lib/service_patterns/charge_subscription_service.rb.

# Configure packages
bundle

# Run the ruby console
./bin/console
=> result = ServicePatterns::ChargeSubscriptionService.call({ email: "[email protected]" })
=> result.success? # true
=> result.amount # 50

Using an interactor from a Rails controller:

def charge
  response = ChargeSubscriptionService.call({ email: "[email protected]" })

  if response.success?
    render json: { message: "Charged #{response.amount} to card." }
  else
    render json: { errors: response.errors }
  end
end

Testing interactors:

describe ChargeSubscriptionService do
  let(:email)   { "[email protected]" }
  let(:context) { ChargeSubscriptionService.call(email: email) }

  describe "#call" do
    context "valid email" do
      let(:email) { "[email protected]" }

      it "succeeds" do
        expect(context).to be_a_success
      end

      it "charges the card" do
        expect(context.amount).to eq(50)
      end
    end

    context "invalid email" do
      let(:email) {}

      it "fails" do
        expect(context).to be_a_failure
      end

      it "provides error message" do
        expect(context.errors).to be_present
      end
    end
  end
end

interactor-example's People

Contributors

mgmarlow avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.