All Projects → stitchfix → Stitches

stitchfix / Stitches

Licence: mit
Create a Microservice in Rails with minimal ceremony

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Stitches

multi-tenancy-devise
mtdevise adds basecamp style user logins to your ruby on rails application.
Stars: ✭ 27 (-92.72%)
Mutual labels:  ruby-gem, gem, rails-engine
churnalizer
Analyze your Ruby app for Churn vs Complexity
Stars: ✭ 17 (-95.42%)
Mutual labels:  ruby-gem, gem
exception hunter
Crash reporting engine to hunt down bugs 🐞
Stars: ✭ 78 (-78.98%)
Mutual labels:  gem, rails-engine
make model searchable
Adds simlpe search functionality to models
Stars: ✭ 27 (-92.72%)
Mutual labels:  ruby-gem, gem
slackify
Build Slackbot on Rails using Slack Event API
Stars: ✭ 20 (-94.61%)
Mutual labels:  gem, rails-engine
pixitar
🧝 Pixitar is an avatar generation library written in Ruby.
Stars: ✭ 20 (-94.61%)
Mutual labels:  ruby-gem, gem
syobocal
Simle gem for Syboi Calendar
Stars: ✭ 13 (-96.5%)
Mutual labels:  ruby-gem, gem
Unimidi
Realtime MIDI IO for Ruby
Stars: ✭ 229 (-38.27%)
Mutual labels:  ruby-gem, gem
webpay rails
WebpayRails is an easy solution for integrate Transbank Webpay in Rails applications
Stars: ✭ 16 (-95.69%)
Mutual labels:  gem, rails-engine
rails cursor pagination
Add cursor pagination to your ActiveRecord backed application
Stars: ✭ 21 (-94.34%)
Mutual labels:  ruby-gem, gem
filtered
Filters ActiveRecord queries in a nice way
Stars: ✭ 28 (-92.45%)
Mutual labels:  ruby-gem, gem
memo wise
The wise choice for Ruby memoization
Stars: ✭ 486 (+31%)
Mutual labels:  ruby-gem, gem
Tabler Rubygem
Rubygem for https://tabler.github.io
Stars: ✭ 77 (-79.25%)
Mutual labels:  rails-engine, gem
grape-jwt-authentication
A reusable Grape JWT authentication concern
Stars: ✭ 31 (-91.64%)
Mutual labels:  ruby-gem, gem
Api Fuzzer
API Fuzzer which allows to fuzz request attributes using common pentesting techniques and lists vulnerabilities
Stars: ✭ 238 (-35.85%)
Mutual labels:  ruby-gem, gem
log-symbols
A ruby 💎gem💎 for generating log symbols
Stars: ✭ 14 (-96.23%)
Mutual labels:  ruby-gem, gem
Devise masquerade
Extension for devise, enable login as functionality. Add link to the masquerade_path(resource) and use it.
Stars: ✭ 380 (+2.43%)
Mutual labels:  ruby-gem, gem
Motion
Reactive frontend UI components for Rails in pure Ruby
Stars: ✭ 498 (+34.23%)
Mutual labels:  ruby-gem, gem
jsonapi-serializer-formats
💎 Gem to enrich jsonapi-serializer with multiple formats
Stars: ✭ 20 (-94.61%)
Mutual labels:  ruby-gem, gem
rspec n
A ruby gem that runs RSpec N times.
Stars: ✭ 37 (-90.03%)
Mutual labels:  ruby-gem, gem

Create Microservices in Rails by pretty much just writing regular Rails code.

build status

This gem provides:

  • transparent API key authentication.
  • router-level API version based on headers.
  • a way to document your microservice endpoints via acceptance tests.
  • structured errors, buildable from invalid Active Records, Exceptions, or by hand.

This, plus much of what you get from Rails already, means you can create a microservice Rails application by just writing the same Rails code you write today. Instead of rendering web views, you render JSON (which is built into Rails).

To install

Add to your Gemfile:

gem 'stitches'

Then:

bundle install

Then, set it up:

> bin/rails generate stitches:api
> bundle exec rake db:migrate

Upgrading from an older version

  • When upgrading to version 4.0.0 you may now take advantage of an in-memory cache

You can enabled it like so

Stitches.configure do |config|
  config.max_cache_ttl = 5  # seconds
  config.max_cache_size = 100  # how many keys to cache
end
  • If you have a version lower than 3.3.0, you need to run two generators, one of which creates a new database migration on your api_clients table:

    > bin/rails generate stitches:add_enabled_to_api_clients
    > bin/rails generate stitches:add_deprecation
    
  • If you have a version lower than 3.6.0, you need to run one generator:

    > bin/rails generate stitches:add_deprecation
    

Example Microservice Endpoint

Suppose we wish to allow our consumers to create Widgets

class Api::V1::WidgetsController < ApiController
  def create
    widget = Widget.create(widget_params)
    if widget.valid?
      head 201
    else
      render json: {
        errors: Stitches::Errors.from_active_record_object(widget)
      }, status: 422
    end
  end

private

  def widget_params
    params.require(:widget).permit(:name, :type, :sub_type)
  end
end

If you think there's nothing special about this—you are correct. This is the vanillaest of vanilla Rails controllers, with a few notable exceptions:

  • We aren't checking content type. A stitches-based microservice always uses JSON and refuses to route requests for non-JSON to you, so there's zero need to use respond_to and friends.
  • The error-building is structured and reliable.
  • This is an authenticated request. No request without proper authentication will be routed here, so you don't have to worry about it in your code.
  • This is a versioned request. While the URL will not contain v1 in it, the Accept header will require a version and get routed here. If you make a V2, it's just a new controller and this concern is handled at the routing layer.

All this means that the Rails skills of you and your team can be directly applied to building microservices. You don't have to make a bunch of boring decisions about auth, versioning, or content-types. It also means you can start deploying and creating microservices with little friction. No need to deal with a complex DSL or new programming language to get yourselves going with Microservices.

More Info

See the wiki for how to setup stitches.

  • Stitches Features include:
    • Authorization via API key
    • Versioned requests via HTTP content types
    • Structured Errors
    • ISO 8601-formatted dates
    • Deprecation using the Sunset header
    • An optional ApiKey cache to allow mostly DB free APIs
  • The Generator sets up some code in your app, so you can start writing APIs using vanilla Rails idioms:
    • a "ping" controller that can validate your app is working
    • version routing based on content-type (requests for V2 use the same URL, but are serviced by a different controller)
    • An ApiClient Active Record
    • Acceptance tests that can produce API documentation as they test your app.
  • Stitches provides testing support

API Key Caching

Since version 4.0.0, stitches now has the ability to cache API keys in memory for a configurable amount of time. This may be an improvement for some applications.

You must configure the API Cache for it be used.

Stitches.configure do |config|
  config.max_cache_ttl = 5  # seconds
  config.max_cache_size = 100  # how many keys to cache
end

Your cache size should be larger then the number of consumer keys your service has.

Developing

Although Stitches.configuration is global, do not depend directly on that in your logic. Instead, allow all classes to receive a configuration object in their constructor. This makes the classes easier to deal with and change, without incurring much of a real cost to development. Global symbols suck, but are convenient. This is how you make the most of it.

Also, the integration test does a lot of "testing the implementation", but since Rails generators are notorious for silently failing with a successful result, we have to make sure that the various inject_into_file calls are actually working. Do not do any fancy refactors here, just keep it up to date.

Releases

See the release process for open source gems in the Stitch Fix engineering wiki under technical topics.


Provided with love by your friends at Stitch Fix Engineering

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].