Why an Adapter?

Using an adapter class to mediate interaction with the external service is a good idea even when, like Twitter, the external service already has a Ruby gem. The adapter encapsulates logic that is specific to the interaction between your application and the service.

An adapter is useful if your code has any or all of the following qualities:

  • The external service will be accessed from multiple points in your code.

  • The interaction with the external service has logic of its own, such as authentication or type changing or common sets of options.

  • There’s a mismatch between the language or metaphor of the API and the domain terms and structures of your code.

The Twitter example doesn’t have the first quality, at least not yet. You do ...

Get Rails 5 Test Prescriptions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.